Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
Row[{Style["subject ",Black,10],Style["AK6",Pink,18]}] –  Mike Honeychurch Feb 8 '12 at 21:26

2 Answers 2

up vote 17 down vote accepted

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

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

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.

share|improve this answer
    
Boxes... Ugh... –  Brett Champion Feb 8 '12 at 22:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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