From: Chris Degnen on 21 Jan 2010 04:50 Hi all, Can anyone figure out a way to put bold or italics within justified text. Below is an example of a text segment as used in my application. "Lamh Dearg Abu" should be in italics. text = Graphics[{White, Rectangle[{0, 0}, {590, 52}], Black, Inset[ TextCell[ "He said and then lifted he in his rude great brawny \ strengthy hands the medher of dark strong foamy ale and, \ uttering his tribal slogan Lamh Dearg Abu, he drank to the \ undoing of his foes, a race of mighty valorous heroes, rulers \ of the waves, who sit on thrones of alabaster silent as the \ deathless gods.", LineSpacing -> {0, 16}, TextJustification -> 1], {0, 0}, {Left, Bottom}, {590, Automatic}]}, PlotRange -> {{0, 590}, {0, 52}}, BaseStyle -> {FontFamily -> "Franklin Gothic Book", FontWeight -> "Plain", FontSize -> 13}]; box = Graphics[{LightGray, Rectangle[{0, 0}, {629, 90}], Inset[text, {314.5, 45}, {Center, Center}, {590, 52}]}, PlotRange -> {{0, 629}, {0, 90}}, ImageSize -> 629]; Print[box];
From: Leonid Shifrin on 22 Jan 2010 05:36 Hi Chris, I would first split your text embedding in Graphics procedure into 3 parts, so they can be modified independently: Clear[makeText]; makeText[text_String] := TextCell[text, LineSpacing -> {0, 16}, TextJustification -> 1]; Clear[makeTextInGraphics]; makeTextInGraphics[textcell_] := Graphics[{White, Rectangle[{0, 0}, {590, 52}], Black, Inset[textcell, {0, 0}, {Left, Bottom}, {590, Automatic}]}, PlotRange -> {{0, 590}, {0, 52}}, BaseStyle -> {FontFamily -> "Franklin Gothic Book", FontWeight -> "Plain", FontSize -> 13}]; Clear[makeBox]; makeBox[text_String] := makeBox(a)makeText[text]; makeBox[text_TextCell] := makeBox(a)makeTextInGraphics[text]; makeBox[text_Graphics] := Graphics[{LightGray, Rectangle[{0, 0}, {629, 90}], Inset[text, {314.5, 45}, {Center, Center}, {590, 52}]}, PlotRange -> {{0, 629}, {0, 90}}, ImageSize -> 629]; To produce a text, you then call <makeBox> on your text string. The following functionality then seems to do what you want, at least in a simple case when the parts of the string you want to make in italics can be represented as simple string patterns without wildcards: Clear[makeItalic]; makeItalic[text_String, pattern_String] := StringSplit[text, pattern :> StyleBox[pattern, FontSlant -> "Italic"]]; makeItalic[tdata : {(_String | _StyleBox) ..}, pattern_String] := Flatten(a)Replace[tdata, x_String :> makeItalic[x, pattern], 1]; Clear[makeItalicInCell]; makeItalicInCell[text_String, pattern_String, params___] := Cell[TextData[makeItalic[text, pattern]], "Text", params]; makeItalicInCell[TextCell[text_String, opts___?OptionQ], pattern_String] := makeItalicInCell[text, pattern, opts]; makeItalicInCell[ Cell[TextData[tdata : {(_String | _StyleBox) ..}], params___], pattern_String] := Cell[TextData[makeItalic[tdata, pattern]], params]; Clear[replaceByItalics]; replaceByItalics[text_, pattern_String] := text /. x : (_TextCell | _Cell) :> makeItalicInCell[x, pattern]; The code does account (hopefully) for a possibility to apply the transformation to italic font several times. You can execute the following calls for example: box = makeBox[ "He said and then lifted he in his rude great brawny strengthy \ hands the medher of dark strong foamy ale and, uttering his tribal \ slogan Lamh Dearg Abu, he drank to the undoing of his foes, a race of \ mighty valorous heroes, rulers of the waves, who sit on thrones of \ alabaster silent as the deathless gods."] itbox = replaceByItalics[box, "Lamh Dearg Abu"] itbox1 = replaceByItalics[itbox, "of"] itbox2 = replaceByItalics[itbox, "he"] where the last 3 calls will change all occurrences of "Lamh Dearg Abu", "of" and "he" respectively by their version in italic. The procedure certainly can be made more general with some more effort, but this can be one possible starting point. Hope this helps. Regards, Leonid On Thu, Jan 21, 2010 at 12:50 PM, Chris Degnen <degnen(a)cwgsy.net> wrote: > Hi all, > > Can anyone figure out a way to put bold or italics within justified > text. Below is an example of a text segment as used in my > application. "Lamh Dearg Abu" should be in italics. > > text = Graphics[{White, Rectangle[{0, 0}, {590, 52}], Black, > Inset[ > TextCell[ > "He said and then lifted he in his rude great brawny \ > strengthy hands the medher of dark strong foamy ale and, \ > uttering his tribal slogan Lamh Dearg Abu, he drank to the \ > undoing of his foes, a race of mighty valorous heroes, rulers \ > of the waves, who sit on thrones of alabaster silent as the \ > deathless gods.", > LineSpacing -> {0, 16}, TextJustification -> 1], {0, 0}, {Left, > Bottom}, {590, Automatic}]}, PlotRange -> {{0, 590}, {0, 52}}, > BaseStyle -> {FontFamily -> "Franklin Gothic Book", > FontWeight -> "Plain", FontSize -> 13}]; > box = Graphics[{LightGray, Rectangle[{0, 0}, {629, 90}], > Inset[text, {314.5, 45}, {Center, Center}, {590, 52}]}, > PlotRange -> {{0, 629}, {0, 90}}, ImageSize -> 629]; > Print[box]; > > >
From: Simon on 22 Jan 2010 05:41 Hi, just break your String up using a Row, then apply Style to the parts where you need it: Row@{"He said... ", Style["Lamh Dearg Abu", Italic], ", he drank..."} Simon On Jan 21, 5:50 pm, Chris Degnen <deg...(a)cwgsy.net> wrote: > Hi all, > > Can anyone figure out a way to put bold or italics within justified > text. Below is an example of a text segment as used in my > application. "Lamh Dearg Abu" should be in italics. > > text = Graphics[{White, Rectangle[{0, 0}, {590, 52}], Black, > Inset[ > TextCell[ > "He said and then lifted he in his rude great brawny \ > strengthy hands the medher of dark strong foamy ale and, \ > uttering his tribal slogan Lamh Dearg Abu, he drank to the \ > undoing of his foes, a race of mighty valorous heroes, rulers \ > of the waves, who sit on thrones of alabaster silent as the \ > deathless gods.", > LineSpacing -> {0, 16}, TextJustification -> 1], {0, 0}, {Lef= t, > Bottom}, {590, Automatic}]}, PlotRange -> {{0, 590}, {0, 52}}= , > BaseStyle -> {FontFamily -> "Franklin Gothic Book", > FontWeight -> "Plain", FontSize -> 13}]; > box = Graphics[{LightGray, Rectangle[{0, 0}, {629, 90}], > Inset[text, {314.5, 45}, {Center, Center}, {590, 52}]}, > PlotRange -> {{0, 629}, {0, 90}}, ImageSize -> 629]; > Print[box];
|
Pages: 1 Prev: Astrophysical Data Selection Next: Washington DC Area Mathematica Special Interest Group |