Math & ScienceFormatting a long equation.

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
timppapoika
Posts: 10
Joined: Mon Mar 30, 2009 9:59 am

Formatting a long equation.

Post by timppapoika »

I've end up with a monster of an equation and now I'll need to chop it into pieces as well as use brackets.

Here's what I tried, the brackets are the problem and the over-all look will be messy. Is there a way to make it tidy, basically so that the new lines begin where the first equals sign ends?

Code: Select all

\documentclass[a4paper,11pt]{article}

\frenchspacing
\usepackage[finnish]{babel}
\usepackage[applemac]{inputenc}
\usepackage[pdftex]{graphicx}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\begin{document}

\begin{gather}
\Delta c_s=
\left ( \left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta W
+\left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta m_1
+\left |\frac{c_w(W+m_1)}{m_s(T_2-T_0)}\right |\Delta T_1
\\ 
\nonumber     %line break, no eq numbering
+\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_2
+\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s^2(T_2-T_0)}\right |\Delta T_2
\\ 
\nonumber     %line break, no eq numbering
+\left |\frac{c_w(T_0-T_1)(m_s(T_2-T_0)+(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_0
+\left |c_w\right |\Delta m_2 \right ) s. 
\label{deltacs}
\end{gather}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Formatting a long equation.

Post by Stefan Kottwitz »

Hi,

because \left,\right brackets spanning over several lines will cause a problem you could use \bigg, \Bigg etc. instead. If you want alignment, use the align environment instead of gather, for example:

Code: Select all

\begin{align}
    \Delta c_s&=
    \bigg ( \left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta W
    +\left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta m_1
    +\left |\frac{c_w(W+m_1)}{m_s(T_2-T_0)}\right |\Delta T_1
    \\
    \nonumber     %line break, no eq numbering
    &\quad+\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_2
    +\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s^2(T_2-T_0)}\right |\Delta T_2
    \\
    \nonumber     %line break, no eq numbering
    &\quad+\left |\frac{c_w(T_0-T_1)(m_s(T_2-T_0)+(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_0
    +\left |c_w\right |\Delta m_2 \bigg ) s.
    \label{deltacs}
    \end{align}
Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Formatting a long equation.

Post by localghost »

Perhaps another approach might be the better one.

Code: Select all

\begin{equation}\label{eqn:deltacs}
  \begin{aligned}
    \Delta c_s&=
    \left( \left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta W
      +\left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta m_1
      +\left |\frac{c_w(W+m_1)}{m_s(T_2-T_0)}\right |\Delta T_1\right.
    \\
    &+\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_2
      +\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s^2(T_2-T_0)}\right |\Delta T_2
    \\
    &+\left.\left |\frac{c_w(T_0-T_1)(m_s(T_2-T_0)+(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_0
      +\left |c_w\right |\Delta m_2 \right) s.
  \end{aligned}
\end{equation}
Note that you have to insert invisible delimiters in the according lines in order not to run into troubles with the scaled parenthesis at the beginning and the end.


Best regards
Thorsten¹
timppapoika
Posts: 10
Joined: Mon Mar 30, 2009 9:59 am

Re: Formatting a long equation.

Post by timppapoika »

Thanks for the suggestions, both of them worked perfectly. Now the problem is the equation number, though. In Stefan's version the number is a bit too high, and Thorsten's version too low. Is there a way to get it vertically in the middle? I've tried placing the nonumber command after different lines, but it doesn't do anything.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Formatting a long equation.

Post by localghost »

timppapoika wrote:[...] In Stefan's version the number is a bit too high, and Thorsten's version too low. Is there a way to get it vertically in the middle? [...]
My approach should place the number exactly in the middle. Provide a minimal working example (MWE) that shows another behaviour.
timppapoika
Posts: 10
Joined: Mon Mar 30, 2009 9:59 am

Formatting a long equation.

Post by timppapoika »

Here it is:

Code: Select all

\documentclass[a4paper,11pt]{article}

\usepackage[finnish]{babel}
\usepackage[latin1]{inputenc}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}

\begin{document}

\begin{equation}\label{eqn:deltacs}
  \begin{aligned}
    \Delta c_s&=
    \left( \left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta W
      +\left |\frac{c_w(T_0-T_1)}{m_s(T_2-T_0)}\right |\Delta m_1
      +\left |\frac{c_w(W+m_1)}{m_s(T_2-T_0)}\right |\Delta T_1\right.
    \\
    &+\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_2
      +\left |\frac{c_w(W+m_1)(T_0-T_1)}{m_s^2(T_2-T_0)}\right |\Delta T_2
    \\
    &+\left.\left |\frac{c_w(T_0-T_1)(m_s(T_2-T_0)+(T_0-T_1)}{m_s(T_2-T_0)^2}\right |\Delta T_0
      +\left |c_w\right |\Delta m_2 \right) s.
  \end{aligned}
\end{equation}

\end{document}
This produces the number in the lower right-hand corner.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Formatting a long equation.

Post by Stefan Kottwitz »

Hi,
timppapoika wrote: In Stefan's version the number is a bit too high, and Thorsten's version too low. Is there a way to get it vertically in the middle?
the equation is too wide. Try to write it shorter, or perhaps use a smaller font for that equation, then the tag would be placed in the middle, for example:

Code: Select all

\begingroup
\small
\begin{equation}
...
\end{equation}
\endgroup
Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Formatting a long equation.

Post by localghost »

This behaviour is caused by the type area. There's not enough space to put the number to the right of the equation. Hence the number is put below at the right margin. Increase the line width and things will work fine. The geometry package will help you to set up an appropriate type area.
timppapoika
Posts: 10
Joined: Mon Mar 30, 2009 9:59 am

Re: Formatting a long equation.

Post by timppapoika »

Ah. As simple as that. Thanks a bunch guys!
Post Reply