# Mathematica equations to LaTeX but equation is too long, how to split it?

I am new user of LaTeX. I have a problem of splitting long equation which I generate by Mathematica. I tried to use \\, multiline, and split commands but it gives error. Is there way to export equation from Mathematica written in multiline because the equation is too long?

My equation is

$\left(\text{xg} \text{Cos}\left[\alpha _2+\theta _1\right]+(g+\text{yg}) \text{Sin}\left[\alpha _2+\theta _1\right]\right) m_2 R_2+\left(\text{Cos}\left[\alpha _c+\theta _1\right] \text{Cos}\left[\theta _1+\omega _c\right] d_c k_d+\left(\text{xg} \text{Cos}\left[\alpha _c+\theta _1\right]+(g+\text{yg}) \text{Sin}\left[\alpha _c+\theta _1\right]\right) m_d-\text{Cos}\left[\alpha _c+\theta _1\right] \text{Sin}\left[\alpha _1\right] k_d R_1\right) R_c<\left(d \text{Cos}\left[\omega -\alpha _2\right] m_2 R_2+\text{Cos}\left[\alpha _c-\omega _c\right] d_c m_d R_c\right) \left(\theta _1'\right){}^2+\left(\text{IG2}+d \text{Sin}\left[\omega -\alpha _2\right] m_2 R_2-\text{Sin}\left[\alpha _c-\omega _c\right] d_c m_d R_c\right) \theta _1''$

-
Welcome to TeX.SX! Can you extend that to an minimal working example (MWE), also including the version that does not work and your error message? –  Ronny Mar 5 '14 at 15:08
Could you post the Mathematica expression itself? i.e. in Mathematica own expression? –  Nasser Mar 6 '14 at 0:23

I've done the following with your equation:

• I've gotten rid of all \left and \right statements in favor of the following bracketing system: Innermost: square brackets, size default (small); next: round parentheses, size \big; outermost (just one instance): curly braces, size \big. The explicit sizing instructions are needed because the material enclosed in the \left ... \right pairs isn't big; hence the "fences" (brackets, parentheses, and braces) will all have the same minimum size unless explicit sizing instructions are provided.

• Using the multline* environment of the amsmath package, I've broken up the expression into four lines, placing the < symbol at the start of the third line.

• Replaced all \text{Sin} and \text{Cos} instances with \sin and \cos, respectively.

\documentclass{article}
\usepackage[margin=1.5in]{geometry} %choose margins for your document
\usepackage{amsmath}
\begin{document}
\begin{multline*}
\bigl(\text{xg} \cos[\alpha_2+\theta_1]
+(g+\text{yg}) \sin[\alpha_2+\theta_1]\bigr) m_2 R_2
+\bigl\{\cos[\alpha_c+\theta_1] \cos[\theta_1+\omega_c] d_c k_d\\
+\bigl(\text{xg} \cos[\alpha_c+\theta_1]
+(g+\text{yg}) \sin[\alpha_c+\theta_1]\bigr) m_d
-\cos[\alpha_c+\theta_1] \sin[\alpha_1] k_d R_1\bigr\} R_c\\
<\bigl(d \cos[\omega -\alpha_2] m_2 R_2
+\cos[\alpha_c-\omega_c] d_c m_d R_c\bigr)(\theta_1')^2\\
+\bigl(\text{IG2}+d \sin[\omega -\alpha_2] m_2 R_2
-\sin[\alpha_c-\omega_c] d_c m_d R_c\bigr) \theta_1''
\end{multline*}
\end{document}


Addendum: As @egreg has remarked in a comment, it's more common to use round parentheses as the innermost fences, square brackets for the mid-rank fences, and curly braces for the outermost fences. Switching the order of parentheses and brackets in the preceding examples produces the following look:

\documentclass{article}
\usepackage[margin=1.5in]{geometry} %choose margins for your document
\usepackage{amsmath
\begin{document}
\begin{multline*}
\bigl[\text{xg} \cos(\alpha_2+\theta_1)
+(g+\text{yg}) \sin(\alpha_2+\theta_1)\bigl] m_2 R_2
+\bigl\{\cos(\alpha_c+\theta_1) \cos(\theta_1+\omega_c) d_c k_d\\
+\bigl[\text{xg} \cos(\alpha_c+\theta_1)
+(g+\text{yg}) \sin(\alpha_c+\theta_1)\bigl] m_d
-\cos(\alpha_c+\theta_1) \sin(\alpha_1) k_d R_1\bigr\} R_c\\
<\bigl[d \cos(\omega -\alpha_2) m_2 R_2
+\cos(\alpha_c-\omega_c) d_c m_d R_c\bigl](\theta_1')^2\\
+\bigl[\text{IG2}+d \sin(\omega -\alpha_2) m_2 R_2
-\sin(\alpha_c-\omega_c) d_c m_d R_c\bigl] \theta_1''
\end{multline*}
\end{document}

-
Usually [] mark the external group; it's quite funny to see sin[x+y] –  egreg Mar 5 '14 at 16:58
That might be (cf. OP post) due to Mathematica, where functions have their arguments in [] and are written with a capital letter, e.g. Cos[a] calls cosine in Mathematica. –  Ronny Mar 5 '14 at 17:03
@egreg -- I've provided an addendum to the answer, in which I switch the order of round parentheses and square brackets (while keeping curly braces to denote the outermost group). The result is indeed quite a bit easier on the eye! –  Mico Mar 5 '14 at 18:09
I'd appreciate to all for improvement in the equation. However I would like to know weather there is any command which directly transfer Mathematica code into LaTex considering all the improvement done above??? –  Sanjeev Prajapati Mar 7 '14 at 15:37
@SanjeevPrajapati - Thanks! I'm afraid I don't use Mathematica in my own work and hence don't know how to answer your follow-up question. Nor is your follow-up question -- which, by the way, is a very good one! -- going to be seen much if it's posted as a comment to an existing answer. May I suggest you post a separate, new question that asks for hints on how one might program Mathematic so that it generates more-appealing LaTeX code? –  Mico Mar 7 '14 at 15:46

this uses multline (not multiline), and changes one pair of parentheses that would have to be split between lines to \bigl( and \bigr).

as harish kumar points out, this should use \sin and \cos rather than \text, but i didn't change those. also, \mathrm would be better than \text for "xg" and similar, since \text follows the surrounding text style, which would be italic in a theorem environment. (i didn't change those either.)

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{multline*}
\left(\text{xg} \text{Cos}\left[\alpha _2+\theta _1\right]+(g+\text{yg})
\text{Sin}\left[\alpha _2+\theta _1\right]\right) m_2 R_2\\
+\bigl(\text{Cos}\left[\alpha_c+\theta _1\right]
\text{Cos}\left[\theta _1+\omega_c\right] d_c k_d\\
+\left(\text{xg} \text{Cos}\left[\alpha _c+\theta _1\right]+(g+\text{yg})
\text{Sin}\left[\alpha_c+\theta _1\right]\right) m_d\\
-\text{Cos}\left[\alpha _c+\theta _1\right]
\text{Sin}\left[\alpha _1\right] k_d R_1\bigr)R_c\\
<\left(d \text{Cos}\left[\omega-\alpha_2\right] m_2 R_2
+\text{Cos}\left[\alpha _c-\omega _c\right] d_c m_d R_c\right)
\left(\theta _1'\right){}^2\\
+\left(\text{IG2}+d \text{Sin}\left[\omega-\alpha _2\right] m_2 R_2
-\text{Sin}\left[\alpha _c-\omega _c\right] d_c m_d R_c\right) \theta _1''
\end{multline*}
\end{document}

-

I removed all \left and \right as they seem to be un-necessary. Also, \text{Cos} and \text{Sin} looked odd to me and I changed. Further, many parenthesis and brackets may be reduced to make the equation less cluttered (this is an assignment left)

\documentclass[draft]{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
&(\text{xg} \cos[\alpha _2+\theta _1]+(g+\text{yg}) \sin[\alpha _2+\theta _1]) m_2 R_2
+(\cos[\alpha_c+\theta _1] \cos[\theta _1+\omega _c] d_c k_d\\
&\quad +(\text{xg} \cos[\alpha _c+\theta _1]+(g+\text{yg}) \sin[\alpha
_c+\theta _1]) m_d-\cos[\alpha _c+\theta _1] \sin[\alpha _1] k_d R_1) R_c\\
&\quad <(d \cos[\omega-\alpha _2] m_2 R_2+\cos[\alpha _c-\omega _c] d_c m_d R_c) (\theta _1'){}^2\\
&\qquad + (\text{IG2}+d \sin[\omega-\alpha _2] m_2 R_2-\sin[\alpha _c-\omega _c] d_c m_d R_c) \theta _1''
\end{align*}
\end{document}


-
You may want to move the + symbol from the end of the third line to the start of the final line -- and indent the final line by, say, \qquad instead of just \quad. It may also be a good idea to provide some fence-sizing instructions by hand to help guide the reader's eye; you are right, though, in that the \left and \right directives aren't doing anything useful since don't increase the sizes of the associated fences for the equation at hand. –  Mico Mar 5 '14 at 18:14
@Mico Thanks. I made some changes. The parenthesis adjustment, as I told in answer, is a home work for the OP :) –  Harish Kumar Mar 5 '14 at 23:24

I find that if things are aligned that they are easier to read, especially for the case where there is significant repetition:

## Notes:

• I defined macros for \xg, \yg and \IG with \mathrm as per @barbara beeton's suggestion. However as they are macros they are easier to change.
• To get proper alignment of the \cos and \sin, I define macros to do account for the case that the \cos is slightly wider than the \sin. Unfortunately, this still necessitated an additional tweak of \,, but I am not sure exactly why that was necessary.
• The \Sub macro to to take into account the fact that the subscript 2 is slightly wider than the subscript c.
• Several \hphantoms were used to align things. Could use alignat* environment as well but that would also require the use of \lap family of macros.
• The square braces, [], have been moved to the exterior with round parenthesis, (), used for internal grouping. Also, a \Big[, \Big] was used to clearly identify the grouping that is across two lines.

## Code:

\documentclass{article}
\usepackage{calc}

\newcommand{\xg}{\mathrm{xg}}%
\newcommand{\yg}{\mathrm{yg}}%
\newcommand{\IG}{\mathrm{IG2}}%
\newcommand{\Sin}[1]{\makebox[\widthof{$\cos (#1)$}][r]{$\sin (#1)$}}%
\newcommand{\Sub}[1]{\makebox[\widthof{$\scriptstyle2$}][l]{$\scriptstyle#1$}}%

\usepackage{amsmath}
\begin{document}
\begin{align*}
&\big[ \xg \cos(\alpha _2+\theta _1)+(g+\yg) \sin(\alpha _2+\theta _1) \big] m_2 R_2
+\Big[\cos(\alpha_c +\theta _1) \cos(\theta _1+\omega _c) d_c k_d\\
{}+{}&\big[ \xg \cos(\alpha_{\Sub{c}}+\theta _1)+(g+\yg) \sin(\alpha_c+\theta _1) \big] m_d \hphantom{R_2}
-\hphantom{\Big(}\cos(\alpha _c+\theta _1) \sin(\alpha _1 \hphantom{{}+\omega _c}) k_d R_1\Big] R_c\\
&\quad <\hphantom{{}+{}} \big[ \hphantom{\IG+{}} d \cos(\omega-\alpha _2) m_2 R_2 + \cos(\alpha_c-\omega _c) d_c m_d R_c \big] (\theta _1'){}^2\\
&\quad \hphantom{{}<{}} + \big[\IG + d \,\Sin{\omega-\alpha _2} m_2 R_2 - \Sin{\alpha_c-\omega _c} d_c m_d R_c \big] \hphantom{(} \theta _1''
\end{align*}
\end{document}

-

Here is one way of doing it:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
&\hphantom{{}<} (\mathrm{xg}\cos[\alpha_{2} + \theta_{1}] + (g + \mathrm{yg})\sin[\alpha_{2} + \theta_{1}]) m_{2} R_{2}\\
&\hphantom{{}<} + (\cos[\alpha_{c} + \theta_{1}] \cos[\theta_{1} + \omega_{c}] d_{c} k_{d}\\
&\hphantom{{}< + (} + (\mathrm{xg}\cos[\alpha_{c} + \theta_{1}] + (g + \mathrm{yg})\sin[\alpha_{c} + \theta_{1}]) m_{d}\\
&\hphantom{{}< + (} - \cos[\alpha_{c} + \theta_{1}] \sin[\alpha_{1}] k_{d} R_{1}) R_{c}\\
&< (d \cos[\omega - \alpha_{2}] m_{2} R_{2} + \cos[\alpha_{c} - \omega_{c}] d_{c} m_{d} R_{c}) (\theta_{1}')^{2}\\
&\hphantom{{}<} + (\mathrm{IG2} + d \sin[\omega - \alpha_{2}] m_{2} R_{2} - \sin[\alpha_{c} - \omega_{c}] d_{c} m_{d} R_{c}) \theta_{1}''
\end{align*}

\end{document}


Notice that sine and cosine are mathematical operators, so they should be typeset using \sin and \cos.

-