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"


    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?


  • 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.

3 Answers 3


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.

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:

SetOptions[Convert`TeX`BoxesToTeX, "BoxRules" -> mytexrules];

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" *)
  • 1
    $\begingroup$ 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)? $\endgroup$
    – arnd
    May 8, 2014 at 8:05
  • $\begingroup$ @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. $\endgroup$
    – Michael E2
    May 8, 2014 at 12:21

Regarding your second question

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 *)
System`Convert`TeXFormDump`maketex["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


(* Convert`TeX`BoxesToTeX[MakeBoxes[x, TraditionalForm], BoxRules -> 
{alpha :> System`Convert`TeXFormDump`MakeTeX[\[Alpha]], beta :> 
System`Convert`TeXFormDump`MakeTeX[\[Beta]], gamma :> 
System`Convert`TeXFormDump`MakeTeX[\[Gamma]], ...

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

(* {"Convert`TeX`BoxesToTeX", "Convert`TeX`ExpressionToTeX",
"Convert`TeX`TeXToBoxes", "Convert`TeX`TeXToExpression"} *)

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


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 Convert`TeX`MakeTeXRule and Convert`TeX`maketex. 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):


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


c1 \Biggl\{
    \biggr\} c2

Side note: I am the author of this package.

  • $\begingroup$ 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."). $\endgroup$
    – Kvothe
    Apr 8, 2019 at 16:07
  • $\begingroup$ @Kvothe Newlines are controlled by "BodySeparator" and "DelimSeparator" options. As to documentation, try evaluating RestartPacletManager[], or restarting Mathematica after package installation. $\endgroup$
    – jkuczm
    Apr 8, 2019 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.