tags:

views:

1075

answers:

5

I am trying to add an equation in a new line. The problem is that the equation is too long for the line, and I need to break it manually. Otherwise, it just overlaps to the right column, or to the right margins (and looks ugly...). Is there a way LaTeX can brake the equation for me, so it seems nice?

I'm attaching my latex code:

\begin{align*}
f(n)-f(0) &= A(n)-B(n)-C(n)-D(n)\cdot d-\left(A(0)-B(0)-C(0)-D(0)\cdot d\right) \\
          &= A(n)-0-X-D(n)\cdot d-\left(0-0-0-0\right) \\
          &= A(n)-X-D(n)\cdot d
\end{align*}

The problematic line is the first line, which is too long.

+1  A: 

As far as I know, this is not possible. When working inside a display, you are responsible for line breaks. How to line break, and how to continue on the next line in case of brackets, is a tough question for humans (check, for instance, the relevant section in Grätzer, "Math into LaTeX"), let alone for a computer.

Example: when you break the first line after \left(, you need a \right. at the end, and \left. at the beginning of the next line (otherwise you'll get an error). Moreover, you'd want the beginning of the next line to be further right than the bracket produced by \left(

Martijn
I'm not sure that I agree with that. Latex does succeed to place words and sentences within margins quite nicely. +1 for the begin and end split tip.
Anna
breqn actually manages to solve the problem quite well. And if/when it does go wrong, you can always break manually to suite.
Will Robertson
A: 

The standard approach I've used in the past is an eqnarray. See for example this page.

ire_and_curses
eqnarray seems nice. I'm not sure if it is better than split or not, and why.
Anna
Do not use `eqnarray`! http://www.tug.org/pracjourn/2006-4/madsen
Will Robertson
+2  A: 

I usually prefer to handle this by using the amsmath package and using the split structure. There are a bunch of useful structures in there for splitting equations across lines, but that's usually the simplest to use.

Many TeX installations will already have the package, but you can also get it from the AMS website.

Stephen Canon
Thanks for the link to the ams website. It has some jems inside.
Anna
A: 

LaTeX intentionally does not break formulas across lines, as they are very hard to read if the line breaks are not in "sensible" places. So you must break formulas manually (using \).

sleske
+2  A: 

The breqn package is designed to split long equations automatically. It works very well in the majority of situations, but it's not as mature as the amsmath package. Here's how you'd write your example equation:

\documentclass{article}
\usepackage{breqn}
\begin{document}
\begin{dmath}
f(n)-f(0) = A(n)-B(n)-C(n)-D(n)\cdot d-\left(A(0)-B(0)-C(0)-D(0)\cdot d\right)
          = A(n)-0-X-D(n)\cdot d-\left(0-0-0-0\right)
          = A(n)-X-D(n)\cdot d
\end{dmath}
\end{document}

Note there is no markup for alignment or newlines, but the output looks essentially the same as if you used align.

Will Robertson
Wow! That one works really really good!This fits the whole idea of Latex, of not needing to worry if your code is in a multi-column small font format, or a uni-column large font one. Thanks!
Anna
After playing around with it a little more, I see that it is not the panacea, and I'll need to use the split environment sometimes also. But this is nice anyway.
Anna