Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: pattern match and formatting challenge

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68977] Re: [mg68931] pattern match and formatting challenge
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Sat, 26 Aug 2006 02:04:01 -0400 (EDT)
  • References: <200608231116.HAA25112@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

here is one that also handles superscipts and subscripts (as well as
the rowboxes from the previous example)

superScriptAndSubscriptPatternObject=SuperscriptBox|SubscriptBox;

boxesPatternObject=
    Alternatives@@
      ToExpression/@Cases[Names["System`*"],x_/;StringMatchQ[x,___~~"Box"]];

nonRowBoxesPatternObject=Module[{x},DeleteCases[boxesPatternObject,RowBox]];

nonRowSuperscriptOrSubscriptBoxesPatternObject=
    DeleteCases[nonRowBoxesPatternObject,
      superScriptAndSubscriptPatternObject];

stringFormattableQ[expr_]:=
    Module[{subXpr,sewingTag},
      And[FreeQ[ToBoxes[expr],nonRowSuperscriptOrSubscriptBoxesPatternObject],
        Sequence@@
          Flatten@Reap[
                ToBoxes[
                    expr]/.(superScriptAndSubscriptPatternObject)[
                      subxpr__]\[RuleDelayed]
                    Sow[FreeQ[{subxpr},nonRowBoxesPatternObject],sewingTag],
                sewingTag][[2]]]];

docBookSubscript[expr__String]:={expr}[[1]]<>
      StringJoin@@Function["<subscript>"<>#<>"</subscript>"]/@Rest@{expr};

docBookSuperscript[expr__String]:={expr}[[1]]<>
      StringJoin@@Function["<superscript>"<>#<>"</superscript>"]/@Rest@{expr};

toString[expr_?stringFormattableQ]:=
  Module[{boxExpr=ToBoxes[expr],box},
    Block[{SuperscriptBox=docBookSuperscript,SubscriptBox=docBookSubscript},
      ToString@DisplayForm@boxExpr]]

toString[_]:=Abort[];

In[10]:=
toString[{1+Subscript[a,z],z^a}]//FullForm

Out[10]//FullForm=
"{1 + a<subscript>z</subscript>, z<superscript>a</superscript>}"

In[11]:=
toString[{1+Subscript[a,z],z^a}/2]//FullForm

Out[11]=
$Aborted

On 8/23/06, Chris Chiasson <chris at chiasson.name> wrote:
> I thought you might enjoy a pattern matching challenge and helping me
> out at the same time. :-)
>
> The challenge is, detect any expression that can be formatted as only
> a linear sequence of unnested superscripts and subscripts. For bonus
> "points", cause the expression to be formatted as mentioned here:
>
> http://www.docbook.org/tdg5/en/html/equation.html
>
> AFAIK, I have an expression that will do this, but only for a subset
> of the possible expressions:
>
> nonRowBoxBoxesPatternObject=Module[{x},Alternatives@@DeleteCases[ToExpression/@
>         Cases[Names["System`*"],x_/;StringMatchQ[x,___~~"Box"]],RowBox]];
>
> stringFormattableQ[expr_]:=FreeQ[ToBoxes[expr],nonRowBoxBoxesPatternObject];
>
> If stringFormattableQ is true, then the formatting is done by via
> ToString[expr].
>
> --
> http://chris.chiasson.name/
>
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: Matrix Multiplication (with a twist)
  • Next by Date: Re: Graph inside another graph
  • Previous by thread: pattern match and formatting challenge
  • Next by thread: Where Style Sheets are located