MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Text Output

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106728] Re: [mg106679] Text Output
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 22 Jan 2010 05:36:40 -0500 (EST)
  • References: <201001210950.EAA16124@smc.vnet.net>

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@makeText[text];
makeBox[text_TextCell] := makeBox@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@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 at 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];
>
>
>



  • References:
  • Prev by Date: Re: What is the difference Between MakeBoxes and ToBoxes
  • Next by Date: Re: NotebookGet/Read/EvaluateSelection Issues
  • Previous by thread: Text Output
  • Next by thread: Re: Text Output