Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: How Can I Make A String Variable With Styled Text?
Replies: 1   Last Post: Nov 25, 2012 5:00 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Bob Hanlon

Posts: 906
Registered: 10/29/11
Re: How Can I Make A String Variable With Styled Text?
Posted: Nov 25, 2012 5:00 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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@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
>





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.