Style["First Line", Bold, FontSize -> 20], 
       {Background -> White, FrameStyle -> None}], 
     {0, 0.2}], 
       Style["Second", Italic, FontSize -> 20], 
       {Background -> White, FrameStyle -> None}], 
     {0, 0}]}]

Produces the following result:

enter image description here

I am trying to get this result using \n, something like:

       Style["First Line\n Second Line", Bold, Italic, FontSize -> 20], 
       {Background -> White, FrameStyle -> None}], 
     {0, 0.2}]}]

But I get

enter image description here

I want to have more than two lines with different font formatting. Any help would be greatly appreciated.

  • 2
    $\begingroup$ Exactly what formatting options do you to want to be able to change from line to line? $\endgroup$ – m_goldberg Jan 26 '17 at 19:45
  • $\begingroup$ @m_goldberg "\n" was inserted to write new line not to change the format of the text. $\endgroup$ – ramesh Jan 28 '17 at 4:04

Expecting that "\n" can be used to indicate a change in style is like expecting to win a lottery without buying a ticket. "\n" has no special meaning to Style or Text; it's just another character.

However, something that might work for you can be built by defining a couple of functions, one to style the text lines and the other to make them into an aligned block.

align[Right] = {1, 0};
align[Center] = {0, 0} ;(* default *)
align[Left] = {-1, 0};

textLine[text_String, opts : OptionsPattern[]] :=
      Style[text, FilterRules[{opts}, Options[Style]]], 
      Background -> White, FrameStyle -> None], Null, Null]

You can give textLine any option that Style will accept. That provides a lot of flexibility in the formatting of a line of text.

    lines : {Text[__] ..},
    xy : {_?NumericQ, _?NumericQ} : {0, 0}, 
    align : {_, _} : align[Center]] :=
  Module[{dxy = {0, vSpace}},
      ReplacePart[#1, {2 -> #2, 3 -> #3}] &,
       Table[xy + i dxy, {i, 0, Length[lines] - 1}],
       ConstantArray[align, Length[lines]]}]]

Here is how these functions would be used to produce two lines of text, the first having bold text and second having italic text. Also, a different font is specified for each line.

opts1 = 
  {FontFamily -> "Bookman Old Style", FontWeight -> Bold, FontSize -> 20};
opts2 = 
  {FontFamily -> "New Century Schoolbook", FontSlant -> Italic, FontSize -> 20};
txt = 
    {textLine["The quick brown fox ...", opts1], 
     textLine["... jumped over the lazy dog", opts2]}, 
    -.3, {0, 0}, align[Left]];


  • 2
    $\begingroup$ Every time you don't buy a ticket, you win the lottery. $\endgroup$ – Jens Jan 27 '17 at 3:51
  • $\begingroup$ I guess, I was wrong. I was thinking simpler answer. $\endgroup$ – ramesh Jan 27 '17 at 4:45
  • $\begingroup$ @ramesh. Considering how much flexibility it provides, I think it's pretty simple. $\endgroup$ – m_goldberg Jan 27 '17 at 5:29
  • $\begingroup$ @M_goldberg Thank you for your time and response. $\endgroup$ – ramesh Jan 27 '17 at 21:31

You may use StringSplit,Style and Column.

styleLines[lines_String, spec : OptionsPattern[{Style, Column}] ..] :=
  Style[Sequence @@ #] & /@ 
   Transpose@{StringSplit[lines, "\n"], Cases[{spec}, _List]},
  DeleteCases[{spec}, _List]

styleLines takes the string followed by a list of Style options for each line. Finally, options for Column trail afterwards.

styleLines["First Line\nSecond Line\nThird Line",
 {FontWeight -> Bold},
 {FontSlant -> Italic},
 {FontColor -> Blue, FontSize -> 10},
 BaseStyle -> {FontSize -> 20},
 Alignment -> Center,
 Background -> LightBlue]

Mathematica graphics

There is not too much added for syntax checking as I'm not certain how to isolate the OptionsPatterns individually for Style and Column in the function pattern. May be someone will chime in on how to do this. In any case stick to the order of list of line styles followed by column options and it will be fine.

Hope this helps.

  • $\begingroup$ I guess, I was wrong. I was thinking simpler answer. $\endgroup$ – ramesh Jan 27 '17 at 4:45

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.