MathGroup Archive 2012

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

Search the Archive

Re: How Can I Make A String Variable With Styled Text?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128781] Re: How Can I Make A String Variable With Styled Text?
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Sun, 25 Nov 2012 05:00:11 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20121124072927.0CAF4698C@smc.vnet.net>

If you apply a style then the resultant will not be a string.

$Post =.

boldFirstSentence[str_String] :=
  Module[
   {pos = StringPosition[str, "." | "!" | "?"], p},
   If [Length[pos] == 0, str,
    p = pos[[1, 1]];
    StringForm["``" <> StringDrop[str, p],
     Style[StringTake[str, p], Bold]]]];

myString = "This is boldface.  The rest is not.";

boldFirstSentence[myString]

StringForm["``  The rest is not.",
Style["This is boldface.", Bold]]

Or you can have it applied automatically to all string output

$Post = If[Head[#] === String, boldFirstSentence[#], #] &;

myString

StringForm["``  The rest is not.",
Style["This is boldface.", Bold]]


Bob Hanlon


On Sat, Nov 24, 2012 at 2:29 AM, Gregory Lypny
<gregory.lypny at videotron.ca> wrote:
> Hello everyone,
>
> I have a string variable
>
> myString = "This is boldface.  The rest is not."
>
> When I execute myString, I want the first sentence to appear in bold type.  Selecting the sentence and formatting it as bold when I create the string does not work.  StringForm will do the trick, as in
>
>         myString = StringForm["`` The rest is not.", Style["This is boldface", Bold]]
>
> but, of course, the Head of myString will no longer be String.  Is there any other way to do this?
>
> Gregory
>



  • Prev by Date: Re: How to use a function "Manipulate" to SmoothHistogram3D
  • Next by Date: Manually tell Mathematica how to evaluate integrals
  • Previous by thread: How Can I Make A String Variable With Styled Text?
  • Next by thread: Re: How Can I Make A String Variable With Styled Text?