8
$\begingroup$

Please consider :

aboveBox[info_, colors_] := 
 Graphics[{colors, EdgeForm[Thick], Rectangle[{0, 0}, {26, 3}], 
   Text[Style[info, 18, Bold, Black, TextAlignment -> Center], 
        {26, 3}/2]}, ImageSize -> 300]

aboveBox[] output

Now I would like to have a second bit of text aside, with a different style.

I know I could use 2 Text[]s, but I then struggle with the alignment.

Is there a way to have two bits of text with different Style within the same Text[]

Example of desired output :

aboveBox[] with text in different styles

$\endgroup$
  • $\begingroup$ Row[{Style["subject ",Black,10],Style["AK6",Pink,18]}] $\endgroup$ – Mike Honeychurch Feb 8 '12 at 21:26
17
$\begingroup$

You could use Row to build up the text to be shown:

aboveBox[info_, colors_] := 
 Graphics[{colors, EdgeForm[Thick], Rectangle[{0, 0}, {26, 3}], 
   Text[Row[{Style["subject", 12, Bold, Black, 
       TextAlignment -> Center], 
      Style[info, 18, Bold, Red, TextAlignment -> Center]}], {26, 3}/
     2]}, ImageSize -> 300]

aboveBox["AK6", LightBlue]

enter image description here

$\endgroup$
  • $\begingroup$ I think you can do without TextAlignment -> Center. $\endgroup$ – Mr.Wizard Feb 8 '12 at 22:25
  • 3
    $\begingroup$ @Spartacus Probably. It was present in the original code, and I just didn't bother to remove it. $\endgroup$ – Brett Champion Feb 8 '12 at 22:31
9
$\begingroup$

Another possibility which may even give you help for the next time is, to look how Mathematica represents the two-colored Text you want. So go into a notebook and format the text as you like it with the font-menu. For instance

enter image description here

Now, while still being in this cell you press Ctrl+Shift+E and you get (I commented the unimportant stuff out):

 (*Cell[BoxData[*)
 RowBox[{
  StyleBox["subject",
   FontVariations->{"Underline"->True},
   FontColor->GrayLevel[0]], 
  StyleBox[":",
   FontVariations->{"Underline"->True},
   FontColor->GrayLevel[0]], 
  StyleBox["AK6",
   FontWeight->"Bold",
   FontColor->RGBColor[1, 0, 1]]}] (*], 
 "Input",
 CellChangeTimes->{{3.5377269466224403`*^9, 3.537726946623592*^9}}] *)

These are some of the low-level boxes used by Mathematica to set any expression you use in the front-end. The good thing is, you can use this directly in your code if you wrap this with DisplayForm

aboveBox[info_, colors_] := 
 Graphics[{colors, EdgeForm[Thick], Rectangle[{0, 0}, {26, 3}], Text[
    DisplayForm@
     RowBox[{StyleBox["subject", 
        FontVariations -> {"Underline" -> True}, 
        FontColor -> GrayLevel[0]], 
       StyleBox[":", FontVariations -> {"Underline" -> True}, 
        FontColor -> GrayLevel[0]], 
       StyleBox[info, FontWeight -> "Bold", 
        FontColor -> RGBColor[1, 0, 1]]}]
    , {26, 3}/2]}, ImageSize -> 300]

aboveBox["DrX", LightBlue]

enter image description here

Btw: Don't forget Inset which can be used for this purpose too. Since Inset can handle general objects you want to place inside a graphics, it seems it is more general and may become handy sometime.

$\endgroup$
  • $\begingroup$ Boxes... Ugh... $\endgroup$ – Brett Champion Feb 8 '12 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.