Breaking up long equations in TeXForm
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}
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}])
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.
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:
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.
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.