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/
- References:
- pattern match and formatting challenge
- From: "Chris Chiasson" <chris@chiasson.name>
- pattern match and formatting challenge