Multi-line Equation Problem in Latex

  1. Hello,

    I have an equation like this one:

    [tex]f(x)=1+g(x)[/tex]

    where [tex]g(x)[/tex] is a very long term, and I have to split it into two halfs, but I want the second half be exactly under the '+' sign, not under the '=' sign. How can I do that?

    Thanks in advance
     
  2. jcsd
  3. I think there is some information on this in "The Not So Short Guide to [tex]\LaTeXe[/tex]." You should be able to google it. I don't remember exactly, but I think you need to use some & to create something like tabs?

    Anyway, I'm pretty sure it's in there (Chapter 4?).

    -Kerry

    EDIT: Actually it's "The Not So Short Introduction to [tex]\LaTeXe[/tex]." It's available here: http://tobi.oetiker.ch/lshort/lshort.pdf
     
    Last edited: Jul 16, 2009
  4. sylas

    sylas 1,750
    Science Advisor

    There are quite a number of ways to do things like this, but the "align*" environment makes it easy to do exactly what you describe. Click the following to see code:
    [tex]\begin{align*}
    f(x) = 1 & + a + bx + cx^2 \\
    & + dx^3 + ex^4
    \end{align*}[/tex]​
     
    Last edited: Jul 16, 2009
  5. D H

    Staff: Mentor

  6. Yes, but I need this inside a split enviroment. When I use split alone it is ok, but when I use split inside split, or align inside split enviroment, the equation inside the inner enviroment becomes above and under the '+' sign, and not in the same line. This is exactly my problem.

    Regards
     
  7. minger

    minger 1,498
    Science Advisor

    Once your inside the split environment you can simply use ampersands as alignment points.
    Code (Text):

    \begin{equation}
    \begin{split}
    f(x) = 1 &+ \\
     &g(x)
    \end{split}
    \end{equation}
     
    [tex]
    \begin{equation}
    \begin{split}
    f(x) = 1 &+ \\
    &g(x)
    \end{split}
    \end{equation}
    [/tex]
     
  8. Thank you all for replying, but may be I did not make my point clear, so I will give an example:

    [tex]\begin{equation}\begin{split}f(x)=&1+g(x)\\
    =&1+\text{Expansion of g(x)}
    \end{split}
    \end{equation}[/tex]

    the expansion of g(x) spans more than one line, so I want to split it inside a split enviroment. I hope that my point is clear now.

    Regards
     
  9. When I write equations, I like to use a split environment nested within an align environment. To do multiline equations, I do stuff like this:

    Code (Text):

    \begin{align}\begin{split}
        &\rho^s \sum_{k=0}^\infty k(k-1)a_k\rho^{k-2} + 2(s+1)\rho^{s-1}\sum_{k=0}^\infty k a_k\rho^{k-1} - \rho^s \sum_{k=0}^\infty k a_k\rho^{k-1} \newln + (\eta - 1 - s)\rho^{s-1}\sum_{k=0}^\infty a_k\rho^k + [s(s+1) - l(l+1)]\rho^{s-2} \sum_{k=0}^\infty a_k\rho^k = 0
    \end{split}\end{align}
     
    where I have defined the \newln command as
    Code (Text):

    \newcommand{\newln}{\\&\quad\quad{}}
     
    The spacing can be adjusted by putting spacing after the & character. If you need to put a new line inside a parenthesis block ( eg. \left( \right) ), I have another command, \parenthnewln

    Code (Text):

    \newcommand{\parenthnewln}{\right.\\&\left.\quad\quad{}}
     
    Again, the spacing can be adjusted.

    [tex]
    \newcommand{\newln}{\\&\quad\quad{}}
    \begin{align}\begin{split}
    &\rho^s \sum_{k=0}^\infty k(k-1)a_k\rho^{k-2} + 2(s+1)\rho^{s-1}\sum_{k=0}^\infty k a_k\rho^{k-1} - \rho^s \sum_{k=0}^\infty k a_k\rho^{k-1} \newln + (\eta - 1 - s)\rho^{s-1}\sum_{k=0}^\infty a_k\rho^k + [s(s+1) - l(l+1)]\rho^{s-2} \sum_{k=0}^\infty a_k\rho^k = 0
    \end{split}\end{align}
    [/tex]

    To fit your example:

    [tex]
    \newcommand{\parenthnewln}{\right.\\&\left.\quad\quad{}}
    \begin{align}\begin{split}
    f(x) &= 1 + g(x)\\
    &= 1 + \left( 1 + x + \frac{x^2}{2} + \parenthnewln + \frac{x^3}{3!} + \ldots \parenthnewln + \frac{x^n}{n!} + \ldots \right)
    \end{split}\end{align}
    [/tex]
     
  10. minger

    minger 1,498
    Science Advisor

    I think I got you. In certain math packages, more than one alignment character "&" is allowed, as long as the same amount are used each line. In this case, you could put one at the equals sign and one at the plus sign . I'm not sure how its treated here, but you could do something like

    Code (Text):

    \begin{equation}
    \begin{split}
    f(x) &= 1 +& g(x) \\
     &=1 +& sin(x) + cos(x) + \cdots \\
     & +&tan(x)
    \end{split}
    \end{equation}
     
    After preview, its apparent that it is not allowed with the physics forums packages. So, try the other posters suggestion of the align environment, or just break it up into two equations.

    Edit: Wait so just to clarify, this isn't what you want?
    [tex]
    \begin{equation}
    \begin{split}
    f(x) = 1 +& g(x) \\
    =1 +& sin(x) + cos(x) + \cdots \\
    +&tan(x)
    \end{split}
    \end{equation}
    [/tex]
     
  11. Thank you both Fenn and minger. Both codes are working well. But I want to ask: in the code that minger gave, the '&' character was not used to align the '=' characters. How is that?

    Regards
     
  12. That alignment example looks very nice in the forums, minger. Which math package allows you to do that? When I use only amsmath in my document, my compiler complains about the multiple alignment tabs per line.
     
  13. Try this one Fenn:

    Code (Text):
    \begin{equation}
    \begin{split}
    f(x)=1+&g(x)\\
    =1+&x+x^2+\dots\\
    +&x^n
    \end{split}
    \end{equation}
    Regards
     
  14. minger

    minger 1,498
    Science Advisor

    It's because I got lucky. Now, there are math packages which allow multiple alignement tabs (I think they're called) per line, as I mentioned, as long as they are the same number per line.

    In this case, I simply got lucky because on each line before the + sign, there is only a 1 and an equals sign. It lines up by default really.
     
  15. Ooh, pretty. Yes, that looks very nice. With my suggestion earlier of defining new commands, it is fairly straightforward to split the equation when it is within parenthesis:

    Code (Text):

    \newcommand{\parenthnewln}[1]{\right.\\#1&\left.{}}
    \begin{equation}
    \begin{split}
    f(x)=1+&g(x)\\
    =1+&\left(x+x^2+\dots
    \parenthnewln{+}x^n+\ldots\right)
    \end{split}
    \end{equation}
     
    [tex]
    \newcommand{\parenthnewln}[1]{\right.\\#1&\left.{}}
    \begin{equation}
    \begin{split}
    f(x)=1+&g(x)\\
    =1+&\left(x+x^2+\dots
    \parenthnewln{+}x^n+\ldots\right)
    \end{split}
    \end{equation}
    [/tex]
     
  16. We are learning something new each time. Thank you all guys.
     
  17. One thing I'd like to learn is how to number multiline equations with subindices like (a) (b) (c).

    Something like

    [tex]
    \begin{align}
    I = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}
    \end{align}

    \end{align}
    \begin{align}
    f &= ma\\
    E &= mc^2\\
    \pi &\text{ is exactly three!}
    \end{align}
    [/tex]

    What I would like to see in this example, is the first line numbered (1), as it is now, and then the group of three numbered (2a), (2b), (2c).

    Does anyone have any suggestions that would accomplish this? I've noticed that the split environment clusters the equations together into one equation number, but that really only helps if I am defining a multi-line equation like where this topic started.
     
  18. Alright, I found my answer, in case anyone is looking. It's the subequations environment from the amsmath package.

    [tex]
    \begin{align}
    I = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}
    \end{align}

    \begin{subequations}\begin{align}
    f &= ma\\
    E &= mc^2\\
    \pi &\text{ is exactly three!}
    \end{align}\end{subequations}
    [/tex]
     
  19. It is nice Fenn, thank you. I may re-consider the numbering of certain equations based on this.

    Regards
     
  20. I'm now using them for clusters of similar equations, so my index numbers don't get too outrageous.

    [tex]
    \begin{subequations}\begin{align}
    Y_1^{+1}(\theta,\phi) &= -\sqrt{\frac{3}{8\pi}}\sin\theta e^{i\phi} \\
    Y_1^0(\theta,\phi) &= \sqrt{\frac{3}{4\pi}}\cos\theta \\
    Y_1^{-1}(\theta,\phi) &= \sqrt{\frac{3}{8\pi}}\sin\theta e^{-i\phi}
    \end{align}\end{subequations}
    [/tex]
     
  21. minger

    minger 1,498
    Science Advisor

    [tex]
    \begin{subequations}
    \begin{align}
    \frac{u(x_0+\Delta x)-u(x_0)}{\Delta x} &=\frac{du}{dx} + \frac{\Delta x}{2}\frac{d^2 u}{dx^2} +\cdots \\
    \frac{u(x_0)-u(x_0-\Delta x)}{\Delta x} &=\frac{du}{dx} - \frac{\Delta x}{2}\frac{d^2 u}{dx^2} +\cdots \\
    \frac{u(x_0+\Delta x)-u(x_0-\Delta x)}{\Delta x} &= \frac{du}{dx} + \frac{\Delta x^2}{6}\frac{d^3 u}{dx^3}+ \cdots
    \end{align}
    \end{subequations}
    [/tex]

    Nice!! big fan!
     
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook