# First line with bold text and the second line with Italic text

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


Produces the following result:

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

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


But I get

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

• Exactly what formatting options do you to want to be able to change from line to line? Jan 26, 2017 at 19:45
• @m_goldberg "\n" was inserted to write new line not to change the format of the text. Jan 28, 2017 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[]] :=
Text[
Framed[
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.

textBlock[
lines : {Text[__] ..},
vSpace_?NumericQ,
xy : {_?NumericQ, _?NumericQ} : {0, 0},
align : {_, _} : align[Center]] :=
Module[{dxy = {0, vSpace}},
ReplacePart[#1, {2 -> #2, 3 -> #3}] &,
{lines,
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 =
textBlock[
{textLine["The quick brown fox ...", opts1],
textLine["... jumped over the lazy dog", opts2]},
-.3, {0, 0}, align[Left]];
Graphics[txt]


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

You may use StringSplit,Style and Column.

styleLines[lines_String, spec : OptionsPattern[{Style, Column}] ..] :=
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]


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.

• I guess, I was wrong. I was thinking simpler answer. Jan 27, 2017 at 4:45