6

I am copying large formulas from a sympy session into Texmaker. These are along the lines of :

\documentclass[]{article}
\usepackage{amsmath}
\begin{document}
$$X''(x) = \left(- 2 C_{1} \sqrt{\lambda} \sin{\left (\sqrt{\lambda} x \right )} -
 C_{1} \lambda \cos{\left (\sqrt{\lambda} x \right )} + 
 C_{1} \cos{\left (\sqrt{\lambda} x \right )} +  
 2 C_{2} \sqrt{\lambda} \cos{\left (\sqrt{\lambda} x \right )} -
 C_{2} \lambda \sin{\left (\sqrt{\lambda} x \right )} + 
 C_{2} \sin{\left (\sqrt{\lambda} x \right )}\right) e^{x}$$
\end{document}

Needless to say they overflow the page and this is not what I want. I want to see the whole formula beginning to end. I tried to break it up using the environment suggested by a previous question but this did no good:

\begin{equation*}
\begin{split}
X''(x) = \left(- 2 C_{1} \sqrt{\lambda} \sin{\left (\sqrt{\lambda} x \right )} -
 C_{1} \lambda \cos{\left (\sqrt{\lambda} x \right )} \\
+ C_{1} \cos{\left (\sqrt{\lambda} x \right )} +  2 C_{2} \sqrt{\lambda} \cos{\left (\sqrt{\lambda} x \right )} - \\
C_{2} \lambda \sin{\left (\sqrt{\lambda} x \right )} + C_{2} \sin{\left (\sqrt{\lambda} x \right )}\right) e^{x}\
\end{split}
\end{equation*}

The above threw about 20 errors on the line with \end{split}. This is strange because the following:

\begin{equation*}
\begin{split}
F = a+b+c- \\ & d+e+f+d- \\
  e+f+g \
\end{split}
\end{equation*}

Worked just fine. Somebody please help with this. I have been searching for a solution about 2 hours now with no success. A real pain....

user32882
  • 1,594
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Aug 01 '17 at 16:33
  • Allright, the first example is now fully compilable. The others are part of the same document so they should work within the same environment – user32882 Aug 01 '17 at 16:40
  • 1
    i haven't tried to test your example, but it looks like it is overloaded with \left and \right. that's the usual source of (la)tex complaints. remember that the number of \lefts must equal the number of \rights in each "cell" of an equation subdivided by & and \\. once that's okay, nasty messages become much less frequent. – barbara beeton Aug 01 '17 at 16:48
  • 1
    this is probably a duplicate of https://tex.stackexchange.com/a/358073/1090 – David Carlisle Aug 01 '17 at 17:04

4 Answers4

10

If you need something you can simply throw the sympy-produced equations into, then you can try breqn package. It handles all \lefts and \rights automatically, but, as expected, it will not do wonders and the output will never be as good as a manually tuned one. It also has some clashes which you have to take care of.

\documentclass{article}
\usepackage{amsmath}
\usepackage{breqn}
\begin{document}

\begin{dmath*}
X''(x) = \left(- 2 C_{1} \sqrt{\lambda} \sin{\left (\sqrt{\lambda} x \right )} -
 C_{1} \lambda \cos{\left (\sqrt{\lambda} x \right )} + 
 C_{1} \cos{\left (\sqrt{\lambda} x \right )} +  
 2 C_{2} \sqrt{\lambda} \cos{\left (\sqrt{\lambda} x \right )} -
 C_{2} \lambda \sin{\left (\sqrt{\lambda} x \right )} + 
 C_{2} \sin{\left (\sqrt{\lambda} x \right )}\right) e^{x}
\end{dmath*}

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
8

Some comments:

  • The macros \sin and \cos don't take arguments. Writing \sin{...} doesn't generate an error, but it doesn't generate anything good either (and it does create code clutter).

  • Get rid of all \left and \right sizing instructions. They're not needed, and the outermost pair is going to create a syntax error once you introduce a line break.

  • Load the amsmath package and use an align* environment to typeset the equation over two lines.

  • I'd give more prominence to the e^{x} term, by placing it first rather than dead-last.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
X''(x) &= e^{x} \bigl[ - 2 C_{1} \sqrt{\lambda} \sin (\sqrt{\lambda} x) 
          - C_{1} \lambda \cos (\sqrt{\lambda} x) 
          + C_{1} \cos (\sqrt{\lambda} x) \\
  &\qquad +  2 C_{2} \sqrt{\lambda} \cos(\sqrt{\lambda} x) 
          - C_{2} \lambda \sin(\sqrt{\lambda} x) 
          + C_{2} \sin(\sqrt{\lambda} x)\bigr]
\end{align*}
\end{document} 
Mico
  • 506,678
  • Yes this works. I was kind of hoping I wouldn't have to tinker with the formulas once I import them since the Tex code is automatically generated by sympy. Could you please provide a solution where the multiline environment can handle the formula as it is? The idea is not to mess with it since this is only one formula of maybe 30 or 40 more... – user32882 Aug 01 '17 at 16:53
  • 1
    @user32882 - Sorry, but simply throwing the sympy-produced equation into a multline* environment and inserting a line break after the third additive term is just not going to work, precisely because of the proliferation of completely unnecessary \left and \right directives. In particular, the presence of the outermost pair of left and \right directives will throw an error once the line-break is introduced, as I already mentioned in the second bullet point of my answer. – Mico Aug 01 '17 at 17:00
  • If you just want to drop a \\ in, then you can try \left.\\\right.. It won't look great, but it may compile. – Teepeemm Aug 01 '17 at 21:22
  • Your answer is thorough and the method proposed is robust. I can tell that it offers more flexibility in terms of line continuation for different types of equations and mathematical derivations. However I have opted for the "quick and dirty" solution proposed by @AboAmmar since it fulfills my current need. You see I am not generating a document to be read by the public. In this case I am using Texmaker as a substitute for hand derivations. Thanks for the time and effort you put in your answer though. I do appreciate it. – user32882 Aug 02 '17 at 04:50
5

Just remove all \left and \right commands. They are not only the cause for the error (you cannot have \left in one row and the matching \right in another), but they make for too big parentheses.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
\begin{split}
X''(x) = (- 2 C_{1} \sqrt{\lambda} \sin{ (\sqrt{\lambda} x  )} -
 C_{1} \lambda \cos{ (\sqrt{\lambda} x  )} \\
+ C_{1} \cos{ (\sqrt{\lambda} x  )} +  2 C_{2} \sqrt{\lambda} \cos{ (\sqrt{\lambda} x  )} - \\
C_{2} \lambda \sin{ (\sqrt{\lambda} x  )} + C_{2} \sin{ (\sqrt{\lambda} x  )}) e^{x}\
\end{split}
\end{equation*}

\end{document}

enter image description here

Now, let's improve this display.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
\begin{split}
X''(x) &= e^x\bigl(
          - 2C_{1}\sqrt{\lambda}\sin(\sqrt{\lambda}\,x) - C_{1}\lambda\cos(\sqrt{\lambda}\,x) \\
  &\qquad + C_{1}\cos(\sqrt{\lambda}\,x) + 2C_{2}\sqrt{\lambda}\cos(\sqrt{\lambda}\,x) \\
  &\qquad - C_{2}\lambda\sin(\sqrt{\lambda}\,x) + C_{2}\sin(\sqrt{\lambda}x)
\bigr)
\end{split}
\end{equation*}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Please see comment on answer below. I'm kind of hoping I won't have to modify the formula at all since it is automatically generated by sympy. Is there any environment that can handle the \left( and \right) parentheses? – user32882 Aug 01 '17 at 16:56
  • 1
    @user32882 If sympy generates \left and \right, then it's at fault. – egreg Aug 01 '17 at 17:33
4

It can fit on two lines. Here are two other solutions:

\documentclass{article}
\usepackage{mathtools}
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.3pt}

\begin{document}

\begin{multline*}
  X''(x) = e^x\bigl(
  - 2C_{1}\sqrt{\lambda }\sin(\sqrt{\lambda }\,x) - C_{1}\lambda \cos(\sqrt{\lambda }\,x)
  + C_{1}\cos(\sqrt{\lambda }\,x) \\+ 2C_{2}\sqrt{\lambda }\cos(\sqrt{\lambda }\,x) - C_{2}\lambda \sin(\sqrt{\lambda }\,x) + C_{2}\sin(\sqrt{\lambda }x)
  \bigr)
\end{multline*}

\begin{align*}
  X''(x)= e^x\bigl(\mkern-4mu
   & -\mkern-4mu 2C_{1}\sqrt{\lambda }\sin(\sqrt{\lambda }\,x) - C_{1}\lambda \cos(\sqrt{\lambda }\,x) +C_{1}\cos(\sqrt{\lambda }\,x) \\
   & + 2C_{2}\sqrt{\lambda }\cos(\sqrt{\lambda }\,x) -C_{2}\lambda \sin(\sqrt{\lambda }\,x) + C_{2}\sin(\sqrt{\lambda }x) \bigr)
\end{align*}

\end{document}

enter image description here

Added:

Following @Mico's suggestion, we also may have this variant of the second alignment, with a simpler code:

\begin{align*}
  X''(x)= e^x\bigl(
  -2 & C_{1}\sqrt{\lambda }\sin(\sqrt{\lambda }\,x) - C_{1}\lambda \cos(\sqrt{\lambda }\,x) +C_{1}\cos(\sqrt{\lambda }\,x) \\
  {} + 2&C_{2}\sqrt{\lambda }\cos(\sqrt{\lambda }\,x) -C_{2}\lambda \sin(\sqrt{\lambda }\,x) + C_{2}\sin(\sqrt{\lambda }x) \bigr)
\end{align*}

enter image description here

GuM
  • 21,558
Bernard
  • 271,350
  • The \mkern-4mu & -\mkern-4mu 2C_{1} construct in the first row seems like more work than it should be; of course, the construct is needed because of the choice of where to place the & alignment point. How about - 2&C_{1} in the first row and {}+ 2&C_{2} at the start of the second? – Mico Aug 01 '17 at 20:35
  • I tried it first but there was a problem (don't remember exactly what, because I tried quite a few combinations for alignment). Does it yield the same result for you? – Bernard Aug 01 '17 at 20:44
  • +1. :-) The latest version is also what I was toying with (but didn't end up writing in my answer). – Mico Aug 01 '17 at 21:19
  • I gave you full credit. Note the second version has alignment both at the +/– signs and at the last (normalsize) closing parentheses, whereas this one has alignment of 2C₁/2C₂. – Bernard Aug 01 '17 at 21:39