# TeXForm and large brackets (\Biggl[ etc)

## 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\{<arg>\Biggr\}, bracket2[arg_] as \biggl<arg>\biggr] 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

1. The above does not work if I include TeXForm in the Format definition. I think I read somewhere that the TeXForm is not distributed to the recursive calls of Format during formatting. The implementation above leads to StandardForm being cluttered by the strings as well. Is there a way to restrict this formatting to TeXForm?

2. 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 \left and \right because 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.

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 ConvertTeXExpressionToTeX, which in turn calls ConvertTeXBoxesToTeX on the box form returned by MakeBoxes[expr, TraditionalForm]. ConvertTeXBoxesToTeX 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], "]"}];


Here is the rule for rewriting biggBox:

mytexrules = {biggBox[left_String, arg_, right_String] :>
"\\biggl" <> left <> ConvertTeXBoxesToTeX[arg] <> "\\biggr" <> right};
TeXForm[1 + x](* initializes SystemConvertTeXFormDump$GreekWords *) (* x+1 *)  One of the annoying features of TeXForm is that ConvertTeXExpressionToTeX overrides the default "BoxRules" of ConvertTeXBoxesToTeX by setting it equal to SystemConvertTeXFormDump$GreekWords, so you have to hook into it to get TeXForm to use mytexrules:

If[! MatchQ[oldGreekWords, _List],
oldGreekWords = SystemConvertTeXFormDump$GreekWords]; If[MatchQ[oldGreekWords, _List], SystemConvertTeXFormDump$GreekWords = Join[mytexrules, oldGreekWords],
"Warning: SystemConvertTeXFormDump$GreekWords not initialized" ]; 1 + biggbracket[(x + y)/2] // TeXForm (* \biggl[\frac{x+y}{2}\biggr]+1 *)  One can use ConvertTeXBoxesToTeX and ConvertTeXExpressionToTeX directly: Unprotect[ConvertTeXBoxesToTeX]; SetOptions[ConvertTeXBoxesToTeX, "BoxRules" -> mytexrules]; Protect[ConvertTeXBoxesToTeX]; ConvertTeXBoxesToTeX[MakeBoxes[1 + biggbracket[(x + y)/2], TraditionalForm]] (* "\\biggl[\\frac{x+y}{2}\\biggr]+1" *)  If you use ConvertTeXExpressionToTeX, you do not have to overwrite SystemConvertTeXFormDump$GreekWords; instead you can pass mytexrules directly:

SystemConvertTeXFormDump\$GreekWords = oldGreekWords;
ConvertTeXExpressionToTeX[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:

ConvertTeXExpressionToTeX[1 + biggbracket[(x + y)/2]]


TeXForm::unspt: TeXForm of biggBox[[,FractionBox[RowBox[{x,+,y}],2],]] is not supported. >>

(* "+1" *)

• Thank you for your elaborate answer. It's a pity that one has to dive so far into the guts of Mathematica for something seemingly so simple. Through your answer I learned that one can remove the ReadProtected attribute and look at the internals. But how did you get started (removing this attribute did not work for TeXForm)? – arnd May 8 '14 at 8:05
• @amd You're welcome. I started with ?**TeX*, which shows all things with TeX in its name. The context and the two -ToTex functions seemed natural starting points. – Michael E2 May 8 '14 at 12:21

What I could not achieve as well was outputting raw LaTeX commands

and to give you further insight, let me give you another possibility. To create a special LaTeX representation for a certain symbol you can simply call

TeXForm[1]; (* initialise definitions *)
SystemConvertTeXFormDumpmaketex["foo"] = "\\foo"


and now the following should work:

Expand[(x + y + foo)^3] // TeXForm
(* \foo^3+3 \foo^2 x+3 \foo^2 y+3 \foo x^2+6 \foo x y+3 \foo y^2+x^3+3 x^2 y+3 x y^2+y^3 *)


When you ask how you can discover such internals then let me give a short way for this example here. First of all I tried to find out what role MakeBoxes played in the conversion to TeX and therefore, I blocked its definition and looked what happens

Block[{MakeBoxes},
ToString[TeXForm[x]]
]

(* ConvertTeXBoxesToTeX[MakeBoxes[x, TraditionalForm], BoxRules ->
{alpha :> SystemConvertTeXFormDumpMakeTeX[\[Alpha]], beta :>
SystemConvertTeXFormDumpMakeTeX[\[Beta]], gamma :>
SystemConvertTeXFormDumpMakeTeX[\[Gamma]], ...
*)


Now you know that a function BoxesToTeX is involved and you know the context ConvertTeX of it. The next step is to look at

Names["ConvertTeX*"]
(* {"ConvertTeXBoxesToTeX", "ConvertTeXExpressionToTeX",
"ConvertTeXTeXToBoxes", "ConvertTeXTeXToExpression"} *)


and you instantly find ExpressionToTeX which Michael used. I was digging then deeper in BoxesToTeX and used the Spelunk function from here.

Get["https://raw.githubusercontent.com/szhorvat/Spelunking/master/Spelunking.m"]
Spelunk["ConvertTeXBoxesToTeX"]


Looking at the logic of this functions shows most of the details how the conversion to TeX happens and it reveals that you should look at ConvertTeXMakeTeXRule and ConvertTeXmaketex. The last is basically a long list of conversion rules which is applied consisting of simple rules of the form

maketex["\[Dagger]"]="\\dagger "


and of complex rules which recursively walk through the boxes and turn them into TeX code.

If one doesn't mind using external package. There's TeXUtilities package.

Using it we can easily assign formatting to brackets (and other symbols if that's needed):

Needs["TeXUtilities"]

Format[bracket1[arg_], TeXForm] := TeXDelimited["\\Biggl\\{", arg, "\\Biggr\\}"]
Format[bracket2[arg_], TeXForm] := TeXDelimited["\\biggl\\{", arg, "\\biggr\\}"]

Format[c1, TeXForm] = TeXVerbatim["c1"];
Format[c2, TeXForm] = TeXVerbatim["c2"];


After this assignment expression from question (without the ellipsis) in TeXForm:

c1*bracket1[a + b + c2*bracket2[d + e + f]] // HoldForm // TeXForm


outputs:

c1 \Biggl\{
a+b+\biggl\{
d+e+f
\biggr\} c2
\Biggr\}


Side note: I am the author of this package.

• Any way to get the output of TeXDelimited without the new lines? (Also I did the automatic installation but something isn't quite right with the help files. I can't access them properly and it says "paclet:TeXUtilities/tutorial/TeXUtilitiesOverview" could not be found."). – Kvothe Apr 8 '19 at 16:07
• @Kvothe Newlines are controlled by "BodySeparator" and "DelimSeparator" options. As to documentation, try evaluating RestartPacletManager[]`, or restarting Mathematica after package installation. – jkuczm Apr 8 '19 at 19:46