MathGroup Archive 2003

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

Search the Archive

Re: Copying and Pasting Output that has StyleForm

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40811] Re: Copying and Pasting Output that has StyleForm
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Sat, 19 Apr 2003 22:59:44 -0400 (EDT)
  • Organization: University of Washington
  • References: <b7iri2$jv6$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi David,

After a bit of playing around, I think I've come up with an acceptable way
to keep the color information.

I think what is happening is that by default (and I don't believe there is a
way to change this default, so default is not really the best word)
Mathematica automatically strips out StyleBoxes from input. In fact, even
before MakeExpression gets the input, StyleBoxes will be removed, unless
they are wrapped in TagBoxes (perhaps other boxes as well).

Now, there is an option StripWrapperBoxes which is supposed to control how
wrapper boxes like StyleBoxes are handled, which when set to False shouldn't
remove StyleBoxes from TagBoxes. However, setting this option to True or
False seems to have no effect on things, so that approach looks like a dead
end. This means we will have to get our fingers dirty delving into the inner
workings of MakeExpression.

It would be great if there were some documentation on the internal rules for
MakeExpression, but I know of no way to get access to this information. This
means I need to use a rather heavy handed approach to modifying
MakeExpression. The first thing I did was to find out what kind of
information MakeExpression is getting. So, I defined the following rule for
MakeExpression:

MakeExpression[a__] /; preprocess := Block[{preprocess},
    Print[Hold[a]];
    MakeExpression[a]]

This doesn't quite do everything I wanted, since in general Mathematica's
MakeExpression will typically call MakeExpression again on it's input, and
with the above modification only the input to the first MakeExpression will
be printed, but I couldn't think of a way to capture all of the calls to
MakeExpression. At any rate, this modification was enough for me to get a
handle on what's going on.

The input Mathematica is giving to MakeExpression is in the form of
TagBox[StyleBox[_,FontColor->RGBColor[__]], tag_]. So, the information we
need to keep the colors is in the boxform part of the TagBox expression
given to MakeExpression. What we need to do is to change this input so that
it is exactly the same as the initial input given when defining the color. I
chose to massage this input so that it is in the form of a StyleForm
expression again. Of course, it needs to be in a StyleForm expression with
RowBoxes and strings in the form MakeExpression expects. A function which
does this follows:

Clear[MakeExpression]
MakeExpression[a__] /; preprocess :=
  Block[{preprocess},
    MakeExpression @@ (Hold[a] /.
          TagBox[StyleBox[b_, FontColor -> RGBColor[c__]], _] :>
            RowBox[{"StyleForm", "[",
                RowBox[{b, ",", ToString[FontColor -> RGBColor[c]]}],
"]"}])]

preprocess = True;

Actually, there are many ways to to create an expression for MakeExpression
to use, feel free to change it if you like.

There is a problem with the above approach, since I don't use the tag
information at all when processing the expression. A TageBox has two
arguments, the first argument being a boxform and the second argument being
the tag. If a TagBox has information in the tag which differs from the
boxform, that information will be lost. There should be a way to use the tag
information instead of the boxform information, but I didn't investigate
this approach.

I hope this works for you. If not, I would be happy to discuss any problems
that arise.

Carl Woll
Physics Dept
U of Washington

"David Park" <djmp at earthlink.net> wrote in message
news:b7iri2$jv6$1 at smc.vnet.net...
> If I define the following function
>
> Needs["Graphics`Colors`"]
> red[symbol_] := StyleForm[symbol, FontColor -> Red]
>
> and then write
>
> f[red@i]
>
> I obtain output that has a red i as an argument. Its FullForm is
>
> f[StyleForm[i, Rule[FontColor, RGBColor[1.`, 0.`, 0.`]]]]
>
> But if I copy and paste the output with the red i, the pasted copy has a
red
> i, but as soon as I evaluate it the StyleForm is lost.
>
> pasted copy with a red i
> %//FullForm
> f[i]
> f[i]
>
> Why is the StyleForm lost? Is this an unavoidable feature or a bug? It
> certainly would be nice if copying and evaluating didn't lose the color
> information. Is there a way around it?
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>
>




  • Prev by Date: Re: curve fitting
  • Next by Date: Loading Packages On Startup
  • Previous by thread: Re: Copying and Pasting Output that has StyleForm
  • Next by thread: Change in Help Browser Behavior with HideCategories/Show Categories