Goal
I am trying to export a very long expression like
expr = c1*bracket1[a+b+c2*bracket2[d+e+f...]]+...
to LaTeX using TeXForm.
bracket1[arg_] should end up as \Biggl\{, bracket2[arg_] as \biggl and so on.
What I have
What I have managed so far is defining a format for bracket1 and bracket2 that adds text before and after the argument:
Format[bracket1[arg_]] := Row[{"beginbracket1",arg,"endbracket1"}]
Format[bracket2[arg_]] := Row[{"beginbracket2",arg,"endbracket2"}]
This renders expr (without the ellipsis) as
In[1] := expr//TeXForm
Out[1] := \text{c1} \text{beginbracket1}a+b+\text{c2} \text{beginbracket2}d+e+f\text{endbracket2}\text{endbracket1}
What I have not / Questions
The above does not work if I include
TeXFormin theFormatdefinition. I think I read somewhere that theTeXFormis not distributed to the recursive calls ofFormatduring formatting. The implementation above leads toStandardFormbeing cluttered by the strings as well. Is there a way to restrict this formatting to TeXForm?What I could not achieve as well was outputting raw LaTeX commands:
Format[foo,TeXForm] := "\\foo"produces
In[1] := foo//TeXForm
Out[1] := "\text{$\backslash \backslash $foo}"and using
[\Backslash]instead of\\only reduces the number of$\backslash$to one. Is there a way to define raw TeX commands?
Remarks
- I would like to avoid using
\leftand\rightbecause of the line breaks which are necessary due to the length of the expressions. - I would be content with a solution which allows me use the different sizes of LaTeX brackets (and braces and parentheses) without the raw TeX commands. It would be nice to know whether raw TeX is possible at all though.
Answer
Caveat: Since this uses hidden, undocumented functions, it will probably break at some point in the future. Also, I do not have any knowledge of how these functions work, except guesses from observed behavior. Some information is available via Information.
Under the hood of TexForm is Convert`TeX`ExpressionToTeX, which in turn calls Convert`TeX`BoxesToTeX on the box form returned by MakeBoxes[expr, TraditionalForm]. Convert`TeX`BoxesToTeX has an option "BoxRules" which will rewrite matching parts of the box expression to TeX. You can use this to do what you want, if you make a special box form to represent your "\bigg" stuff. I call it biggBox, although technically it will not be recognized as a box.
Below defines a bigg bracket. Bigg parentheses etc. can be defined as well.
ClearAll[biggbracket];
biggbracket /: MakeBoxes[biggbracket[expr_], StandardForm] :=
RowBox[{"[", MakeBoxes[expr, StandardForm], "]"}];
biggbracket /: MakeBoxes[biggbracket[expr_], TraditionalForm] :=
RowBox[{biggBox["[", MakeBoxes[expr, TraditionalForm], "]"]}];
Here is the rule for rewriting biggBox:
mytexrules = {biggBox[left_String, arg_, right_String] :>
"\\biggl" <> left <> Convert`TeX`BoxesToTeX[arg] <> "\\biggr" <> right};
TeXForm[1 + x](* initializes System`Convert`TeXFormDump`$GreekWords *)
(* x+1 *)
One of the annoying features of TeXForm is that Convert`TeX`ExpressionToTeX overrides the default "BoxRules" of Convert`TeX`BoxesToTeX by setting it equal to System`Convert`TeXFormDump`$GreekWords, so you have to hook into it to get TeXForm to use mytexrules:
If[! MatchQ[oldGreekWords, _List],
oldGreekWords = System`Convert`TeXFormDump`$GreekWords];
If[MatchQ[oldGreekWords, _List],
System`Convert`TeXFormDump`$GreekWords = Join[mytexrules, oldGreekWords],
"Warning: System`Convert`TeXFormDump`$GreekWords not initialized"
];
1 + biggbracket[(x + y)/2] // TeXForm
(* \biggl[\frac{x+y}{2}\biggr]+1 *)
One can use Convert`TeX`BoxesToTeX and Convert`TeX`ExpressionToTeX directly:
Unprotect[Convert`TeX`BoxesToTeX];
SetOptions[Convert`TeX`BoxesToTeX, "BoxRules" -> mytexrules];
Protect[Convert`TeX`BoxesToTeX];
Convert`TeX`BoxesToTeX[MakeBoxes[1 + biggbracket[(x + y)/2], TraditionalForm]]
(* "\\biggl[\\frac{x+y}{2}\\biggr]+1" *)
If you use Convert`TeX`ExpressionToTeX, you do not have to overwrite System`Convert`TeXFormDump`$GreekWords; instead you can pass mytexrules directly:
System`Convert`TeXFormDump`$GreekWords = oldGreekWords;
Convert`TeX`ExpressionToTeX[1 + biggbracket[(x + y)/2], "BoxRules" -> mytexrules]
(* "\\biggl[\\frac{x+y}{2}\\biggr]+1" *)
Here is what happens if you do not specify "BoxRules" -> mytexrules:
Convert`TeX`ExpressionToTeX[1 + biggbracket[(x + y)/2]]
TeXForm::unspt: TeXForm of biggBox[[,FractionBox[RowBox[{x,+,y}],2],]] is not supported. >>
(* "+1" *)
Comments
Post a Comment