17
$\begingroup$

If I have a horrifically long expression and output it to TeX using TeXForm, I get a long equation which needs to be split up into multiple lines. But splitting it up manually can be quite cumbersome. Is there a solution to this within Mathematica, i.e., a way to output an equation in multiline format?

I'm sure I'd need to put in some input about the size of the page, but that's fine.

$\endgroup$
2
  • 1
    $\begingroup$ There is no answer to this, since it is impossible to do what you want, without manual editing of the Latex code generated, and using various Latex packages and methods as needed such as breqn, mathtools and its splitfrac, or amsmath and split env, and many other modification to the generated code. This is a Latex issue and nothing that Mathematica can do about it. If you have Latex code as \frac{very very long line}{x} then there is nothing but manual edit that can break it, including manual splitting if needed. The answer to your question is: There is no solution to this within Mathematica $\endgroup$
    – Nasser
    Jan 11, 2014 at 7:24
  • $\begingroup$ I think there's no general solution to your question, especially if the splitting has to be done for a publication where clarity is of utmost importance. So, I think that to achieve better results, when one is going to split an equation, he or she should firstly ask him- or herself: can I rewrite that equation in a shorter form by grouping terms into new definitions? Which parts of the equation are more important for what I want to explain to the reader? In this way, one can organize the splitting so that the most significant terms clearly stand out, and readers will probably appreciate. $\endgroup$ Jul 19, 2014 at 19:49

5 Answers 5

7
$\begingroup$

I don't think Mathematica can do it. But on the other hand, it can be done on the LaTeX side using the package breqn as

\documentclass{revtex4}
\usepackage{breqn}
\begin{document}
\begin{dmath}
    R = -g{}^a{}^b g {}^c{}^d \partial_c\partial_dg{}_a{}_b
     + g{}^a{}^b g {}^c{}^d \partial_b\partial_dg{}_a{}_c
     + \frac{3}{4} g {}^a{}^b g {}^c{}^d g {}^e{}^f \partial_eg{}_a{}_c
    \partial_fg{}_b{}_d
     - \frac{1}{2} g {}^a{}^b g {}^c{}^d g {}^e{}^f \partial_fg{}_a{}_c
    \partial_dg{}_b{}_e
     - g{}^a{}^b g {}^c{}^d g {}^e{}^f \partial_dg{}_a{}_c
    \partial_fg{}_b{}_e
     - \frac{1}{4} g {}^a{}^b g {}^c{}^d g {}^e{}^f \partial_eg{}_a{}_b
    \partial_fg{}_c{}_d
     + g{}^a{}^b g {}^c{}^d g {}^e{}^f \partial_dg{}_a{}_b
    \partial_fg{}_c{}_e
\end{dmath}
\end{document}
$\endgroup$
2
  • $\begingroup$ Nice! The time I spent breaking equations by hand ! $\endgroup$
    – chris
    Jul 19, 2014 at 19:07
  • $\begingroup$ That is awesome! This would have certainly been useful for the first two papers of my PhD... $\endgroup$
    – Adam
    Jul 20, 2014 at 16:29
4
$\begingroup$

The only way to fit a long equation, that I can think of, without additional work as I mentioned in the comment above, is to simply reduce the size of the latex math font to use for this one equation.

It is easy to modify the font size in Latex. One command can do it. Hence you'd need to edit the Latex file, but you have to do that any way to add the standard Latex headers to build the TexForm expression, and it is only one command.

Here is an example:

Make a very long equation that will overflow in Latex since it is not breakable automatically by Latex:

Remove["Global`*"]
res = (Sum[Sin[a x], {a, 1, 15}])/(Sum[Cos[b x], {b, 1, 15}])

Mathematica graphics

Export it to Latex as is first:

SetDirectory[NotebookDirectory[]];
Export["res.tex", TeXForm[res], "Text"];

Open the file res.tex, and add the Latex \begin{document}.... needed to compile it around the code exported by Mathematica. This is the result

\documentclass[12pt]{article}
\begin{document}

\begin{equation}% this below was generated by Mathematica export

\frac{\sin (x)+\sin (2 x)+\sin (3 x)+\sin (4 x)+\sin (5 x)+\sin (6 x)+\sin (7 x)+\sin (8 x)+\sin (9 x)+\sin (10 x)+\sin (11 x)+\sin (12 x)+\sin (13 x)+\sin (14 x)+\sin (15 x)}{\cos (x)+\cos (2 x)+\cos (3 x)+\cos (4 x)+\cos (5 x)+\cos (6 x)+\cos (7 x)+\cos (8 x)+\cos (9 x)+\cos (10 x)+\cos (11 x)+\cos (12 x)+\cos (13 x)+\cos (14 x)+\cos (15 x)}

\end{equation}
\end{document}

Compile it and looking at the pdf, it overflows as expected:

>pdflatex res.tex 
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.

Mathematica graphics

Now, do the same again, but make the math font \scriptscriptstyle, this requires just adding this one command as follows

\documentclass[12pt]{article}
\begin{document}

\begin{equation}
{\scriptscriptstyle \frac{\sin (x)+\sin (2 x)+\sin (3 x)+\sin (4 x)+\sin (5 x)+\sin (6 x)+\sin (7 x)+\sin (8 x)+\sin (9 x)+\sin (10 x)+\sin (11 x)+\sin (12 x)+\sin (13 x)+\sin (14 x)+\sin (15 x)}{\cos (x)+\cos (2 x)+\cos (3 x)+\cos (4 x)+\cos (5 x)+\cos (6 x)+\cos (7 x)+\cos (8 x)+\cos (9 x)+\cos (10 x)+\cos (11 x)+\cos (12 x)+\cos (13 x)+\cos (14 x)+\cos (15 x)}
}
\end{equation}
\end{document}

Now compile again:

Mathematica graphics

Now it fits. There are other way to change the math font. You can make it even smaller if needed. Since it is in PDF, that is not an issue, as one can always increase the magnification inside PDF to see the whole equation, but now at least, it will not be cut off as before.

If you want one command to reduce the font size of all the math in the document, without having to do it for each equation, then \DeclareMathSizes can be used, here is an example:

\documentclass[12pt]{article}
\usepackage{amsmath}
\DeclareMathSizes{12}{5}{5}{5}
\begin{document}

\begin{equation}% this below was generated by Mathematica export
\frac{\sin (x)+\sin (2 x)+\sin (3 x)+\sin (4 x)+\sin (5 x)+\sin (6 x)+\sin (7 x)+\sin (8 x)+\sin (9 x)+\sin (10 x)+\sin (11 x)+\sin (12 x)+\sin (13 x)+\sin (14 x)+\sin (15 x)}{\cos (x)+\cos (2 x)+\cos (3 x)+\cos (4 x)+\cos (5 x)+\cos (6 x)+\cos (7 x)+\cos (8 x)+\cos (9 x)+\cos (10 x)+\cos (11 x)+\cos (12 x)+\cos (13 x)+\cos (14 x)+\cos (15 x)}
\end{equation}
\end{document}

update

Just wanted to add one another thought:

I would consider bypassing this whole approach, and simply export those large equations directly to pdf from Mathematica.

When exporting to pdf from Mathematica, this problem goes way. Then from Latex use \usepackage{pdfpages} to insert those pdf page into the pdf being build by pdflatex from the main Latex document.

Sure, the layout would not look exactly the same as the other Latex pages, and it depends what you are doing. If this is just a report and you want to include some equations, this would work. If this was an official paper or such, this might not work. But I thought to mention this as a possible way.

$\endgroup$
2
  • 1
    $\begingroup$ Nasser, I doubt you´d get away with the tiny equations for a journal or similar publication. $\endgroup$
    – Yves Klett
    Jan 11, 2014 at 11:40
  • 2
    $\begingroup$ In such a case it would be preferable to treat the numerator/denominator as wrappable paragraphs (minipages), but that is more of a \LaTeX question (packages such as breqn may be of help too). $\endgroup$
    – A.G.
    Jan 11, 2014 at 16:19
2
$\begingroup$

I have been troubled by this as well (this isn't an answer by the way). The most promising thing is to wrap the expression in a row of some width smaller than your document's. This works fine in terms of formatting within mathematica (with the definitions of @Nasser's answer):

Row[{res}, ImageSize -> 300]

but sadly TeXForm doesn't read the formatting and breaks if you try to evaluate it on the above. I can see why this is so but I still think there should be the option to minimally read a formatted output into something with \\s in the TeXForm of the output so if someone knows how to implement or can point me to the direction of enriching TeXForm with an option to retain formatting that would be a really great feature.

The other thing you can do is look at your expression at different levels and export parts of it at a time. This is easy if you only have, say, a sum of many terms:

exp = (Sum[Sin[a x], {a, 1, 35}])

where you can more or less define a threshold of terms per line that you want and do some fiddling with the string form of the output:

    ToString@With[{thres = 3},
  Table[TeXForm@exp[[i ;; Min[i + thres, Length[exp]]]], {i, 1, 
    Length[exp], thres}]]
    (*out*){TeXForm[Sin[x] + Sin[2 x] + Sin[3 x] + Sin[4 x]], TeXForm[
 Sin[4 x] + Sin[5 x] + Sin[6 x] + Sin[7 x]], TeXForm[
 Sin[7 x] + Sin[8 x] + Sin[9 x] + Sin[10 x]], TeXForm[
 Sin[10 x] + Sin[11 x] + Sin[12 x] + Sin[13 x]], TeXForm[
 Sin[13 x] + Sin[14 x] + Sin[15 x] + Sin[16 x]], TeXForm[
 Sin[16 x] + Sin[17 x] + Sin[18 x] + Sin[19 x]], TeXForm[
 Sin[19 x] + Sin[20 x] + Sin[21 x] + Sin[22 x]], TeXForm[
 Sin[22 x] + Sin[23 x] + Sin[24 x] + Sin[25 x]], TeXForm[
 Sin[25 x] + Sin[26 x] + Sin[27 x] + Sin[28 x]], TeXForm[
 Sin[28 x] + Sin[29 x] + Sin[30 x] + Sin[31 x]], TeXForm[
 Sin[31 x] + Sin[32 x] + Sin[33 x] + Sin[34 x]], TeXForm[
 Sin[34 x] + Sin[35 x]]}

so here you can do ToString@% and then play with StringReplace. This again is not satisfactory as it's more likely that if you have an expression with many terms you won't have something as simple as a sum and trying to make the above work in a general setting will probably take more time than you doing the editing in $\LaTeX.$

In short, I don't have an answer either but I'd love to see something better from more experienced users.

$\endgroup$
0
$\begingroup$

I just had the same question - and one thing that helped me with Mathematica was the Formula Manipulation help site which shows functions like Simplify, Expand, Factor, Reduce, Collect, Together, ... etc.

Maybe it's possible to shrink your equation - or at least Expand or Factor out something to be able to make it a multiline equation.

$\endgroup$
0
$\begingroup$

Adjusting the width of the notebook's window before exporting the whole notebook as TeX will do what you want in some cases. The notebook should by default have the option to wrap at window width. It's interesting to see how adjusting the width differently gives different results for the same document in LaTeX.

a = NotebookOpen[c]; SetOptions[a, WindowSize -> {72*6, 400}]; Export[filename, a, "TeX", "FullDocument" -> False]; NotebookClose[a];

If you had a notebook with filename "c" then you could use this code to save a LaTeX document. By adjusting the window size in mathematica, you should see that the latex output wraps accordingly. In my case I was formatting Input cells which had very long commands, and they stretched off the page until I resized the window before export.

With a little bit of experimenting, I've noticed that some cells will wrap while others won't. For instance, Input cells and Text cells conform to the window, but Output cells don't, but you can easily convert the output cells to Text cells before exporting, and that seems to work.

NotebookPut[Get[my_notebook]/."Output"->"Text"]

$\endgroup$
1
  • 2
    $\begingroup$ There is a now-deleted answer that basically suggests this. The comments to it suggest that it only changes the way the LaTeX code displays, not how the document renders. Can you comment on this? $\endgroup$
    – b3m2a1
    Feb 26, 2019 at 5:09

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.