aligning math equations in Latex - latex

I am trying to align the following equations around the equal sign. What can I do? Thanks! I am using the AMSMath package
$$m\ddot{x}_{1}-K(x_{2}+x_{12})+C(\dot{x}_{12}+\dot{x}_{2})+2C\dot{x}_{1} = fx_{1}$$
$$m\ddot{x}_{2}-K(x_{3}+x_{1})+C(\dot{x}_{1}+\dot{x}_{3})+2C\dot{x}_{2} = fx_{2}$$
$$m\ddot{x}_{3}-K(x_{4}+x_{2})+C(\dot{x}_{2}+\dot{x}_{4})+2C\dot{x}_{3} = fx_{3}$$

\begin{align}
m\ddot{x}_{1}-K(x_{2}+x_{12})+C(\dot{x}_{12}+\dot{x}_{2})+2C\dot{x}_{1} &= fx_{1} \\
m\ddot{x}_{2}-K(x_{3}+x_{1})+C(\dot{x}_{1}+\dot{x}_{3})+2C\dot{x}_{2} &= fx_{2} \\
m\ddot{x}_{3}-K(x_{4}+x_{2})+C(\dot{x}_{2}+\dot{x}_{4})+2C\dot{x}_{3} &= fx_{3}
\end{align}
See the user's guide.

To make a long story short... the following idea of using eqnarray is kind of obvious, but turns out to be a really bad idea. eqnarray brings lots of issues which are solved in amsmath and related packages. Read Lars Madsen: Avoid eqnarray for the details.
\begin{eqnarray*} %% Do avoid eqnarray if possible.
x_1 & = & 1 \\
x_2 & = & 2 \\
x_3 & = & 3
\end{eqnarray*}
(Answer revised with information from Arthur Reutenauer, Thomas and the Madsen paper linked to by Thomas.)

There are several packages to do what you want, one of the most widely used is the AMS' align environment from the amsmath package.
Simplified extract from amsldoc.pdf:
\begin{align}
x&=y \\
x’&=y’ \\
x+x’&=y+y’
\end{align}
The equations are aligned around the symbols that follow & (the equals signs, in this case).

Related

Using MathJax I need a solution for \ensuremath

I'm using a MathJax in several projects and it generally works like a charm.
Today, however, I ran into a problem during the translation of a part of an existing LaTeX document with the following align-environment:
\begin{align}
& (\lambda x.(\lambda y.(\lambda z.xyz)))abc \\
= & \text{\{ impliciete toepassing expliciet maken \}} \\
& (((\lambda x.(\lambda y.(\lambda z.xyz)))a)b)c \\
= & \text{\{ \ensuremath{\beta}-reductie, substitutie van \ensuremath{x}door \ensuremath{a}\}} \\
& ((\lambda y.(\lambda z.ayz))b)c \\
= & \{\text{\ensuremath{\beta}-reductie, \ensuremath{y\,:=b}}\} \\
& (\lambda z.abz)c \\
= & \text{\{ \ensuremath{\beta}-reductie, \ensuremath{z\,:=c}} \\
& abc \\ \boxed{} \end{align}
The result rendered with LaTeX is this (sorry for the Dutch text ;-):
The align-environment is essentially a math context, so if you want text, you need to enclose that text with \text{...}. But when you need again math symbols within that text, you escape the text context by enclosing the maths with \ensuremath{...}.
And MathJax renders it like:
That Mathjax centers everything doesn't matter, that is something I can handle with CSS. But the rendering of \ensuremath is a problem. Clearly MathJax doesn't support \ensuremath, but I can't think of a workable workaround where I can use math symbols in a text-context.
Ideally I'ld like to have a solution using an alternative LaTeX construction (hence the cross listing)
Any ideas?
You should use $...$ or \(...\) in place of \ensuremath{...}. This seems more natural to me even in LaTeX itself (as \ensuremath is really designed for use within macros that might be used inside both text- and math-modes).
So you can do
\begin{align}
& (\lambda x.(\lambda y.(\lambda z.xyz)))abc \\
= & \{\text{ impliciete toepassing expliciet maken }\} \\
& (((\lambda x.(\lambda y.(\lambda z.xyz)))a)b)c \\
= & \{\text{ $\beta$-reductie, substitutie van $x$door $a$ }\} \\
& ((\lambda y.(\lambda z.ayz))b)c \\
= & \{\text{ $\beta$-reductie, $y:=b$ }\} \\
& (\lambda z.abz)c \\
= & \{\text{ $\beta$-reductie, $z:=c$ }\} \\
& abc \\ \boxed{} \end{align}
I also put the \{ and \} outside the \text{}, though you can do them inside if you prefer.
The centering is probably due to CSS on your page, as MathJax left-justifies these by default.

Aligning a cycle in LaTeX

I want to create a cycle like this in LaTeX:
http://upload.wikimedia.org/math/0/c/8/0c814b6d3ade0dfd8ea4408af6e07ac6.png
Instead of numbers I want to have b1,b2,...,b9 and m1,m2,...,m9.This is the code I use:
\pi_0 = \binom{b_1\ b_2\ b_3\ b_4\ b_5\ b_6\ b_7\ b_8\ b_9}{m_1\ m_2\ m_3\ m_4\ m_5\ m_6\ m_7\ m_8\ m_9}
But it does not produced the desired result, as the b-s and m-s are not aligned (I want for them to be exactly one above the other):
Could you help me, please?
Try using the array environment to produce a matrix:
\pi_0 = \left(
\begin{array}{cccc}
b_1 & b_2 & b_3 & b_4 \\
m_1 & m_2 & m_3 & m_4
\end{array}
\right)
By the way, there is a separate StackExchange site for LaTeX.

Error while writing multi-valued function in LateX

I am creating a document in LateX and the following multivalued function has been giving me trouble for a while.
The Latex code for the above as I gave is
$\[delta \tau_{i,j}^{k}$ = $\left\{$
\begin{array}{l l}
\frac{1}{L_{k}} & \quad \mbox{if ant k travels on edge \textit{i,j}} \\
0 & \quad \mbox{otherwise}
\end{array} \right. \]
While compiling it gives me the following error
! LaTeX Error: Bad math environment delimiter.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.52 $\[
delta \tau_{i,j}^{k}$ = $\left\{$
?
Any help on fixing this error would be much appreciated.
aschepler is right--it's the [ inside the $.
You may also want to consider the cases environment, which I think is easier for this sort of thing. For you example, you'd have:
\[
\Delta\tau_{i,j}^k=
\begin{cases}
1/L_k & \text{if ant $k$ travels along edge $i,j$} \\
0 & \text{otherwise}
\end{cases}
\]
You can't use both $ $ and \[ \] like that. $ is for in-line equations (fit within a paragraph of text), so you should probably use just \[ \] around your equation and remove all the $s.

How can I have linebreaks in my long LaTeX equations?

My equation is very long. How do I get it to continue on the next line rather than go off the page?
If your equation does not fit on a single line, then the multline (note that that's multline without an "i", not "multiline") environment probably is what you need:
\begin{multline}
first part of the equation \\
= second part of the equation
\end{multline}
If you also need some alignment respect to the first part, you can use split:
\begin{equation}
\begin{split}
first part &= second part #1 \\
&= second part #2
\end{split}
\end{equation}
Both environments require the amsmath package.
See also aligned as pointed out in an answer below.
Not yet mentioned here, another choice is environment aligned, again from package amsmath:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{aligned}
A & = B + C\\
& = D + E + F\\
& = G
\end{aligned}
\end{equation}
\end{document}
This outputs:
Without configuring your math environment to clip, you could force a new line with two backslashes in a sequence like this:
Bla Bla \\ Bla Bla in another line
The problem with this is that you will need to determine where a line is likely to end and force to always have a line break there. With equations, rather than text, I prefer this manual way.
You could also use \\* to prevent a new page from being started.
If it is inline equation, then use \allowbreak. Use it like:
$x_1,x_2,x_3,\allowbreak x_4,x_5$.
Latex will break equation in this place only if necessary.
There are a couple ways you can deal with this. First, and perhaps best, is to rework your equation so that it is not so long; it is likely unreadable if it is that long.
If it must be so, check out the AMS Short Math Guide for some ways to handle it. (on the second page)
Personally, I'd use an align environment, so that the breaking and alignment can be precisely controlled. e.g.
\begin{align*}
x&+y+\dots+\dots+x_100000000\\
&+x_100000001+\dots+\dots
\end{align*}
which would line up the first plus signs of each line... but obviously, you can set the alignments wherever you like.
I think I usually used eqnarray or something. It lets you say
\begin{eqnarray*}
x &=& blah blah blah \\
& & more blah blah blah \\
& & even more blah blah
\end{eqnarray*}
and it will be aligned by the & &... As pkaeding mentioned, it's hard to read, but when you've got an equation thats that long, it's gonna be hard to read no matter what... (The * makes it not have an equation number, IIRC)
I used the \begin{matrix}
\begin{equation}
\begin{matrix}
line_1 \\
line_2 \\
line_3
\end{matrix}
\end{equation}
multline is best to use. Instead, you can use dmath, split as well.
Here is an example:
\begin{multline}
{\text {\bf \emph {T(u)}}} ={ \alpha *}{\frac{\sum_{i=1}^{\text{\bf \emph {I(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {I(u)}}}} \\
+{ \beta *}{\frac{\sum_{i=1}^{\text{\bf \emph {$I_h$(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {$I_h$(u)}}}}
\end{multline}
This worked for me while using mathtools package.
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{multlined}
first term \\
second term
\end{multlined}
\end{equation}
\end{document}
Use eqnarray and \nonumber
example:
\begin{eqnarray}
sample = R(s,\pi(s),s') + \gamma V^{\pi} (s') \nonumber \\
\label{eq:temporal-difference}
V^{\pi}_{k+1}(s) = (1-\alpha)V^{\pi}(s) - \alpha[sample]
\end{eqnarray}
SIMPLE ANSWER HERE
\begin{equation}
\begin{split}
equation \\
here
\end{split}
\end{equation}
To solve this issue, I used the array environment inside the equation environment like this:
\begin{equation}
\begin{array}{r c l}
first Term&=&Second Term\\
&=&Third Term
\end{array}
\end{equation}
You do not need any extra package to do this:
\begin{equation}
\begin{gathered}
first formula\\
second formula
\end{gathered}
\end{equation}

Collapsible minibox environment in LaTeX

I'm trying to learn LaTeX, currently because otherwise, my professors will be nearly unable to read my homework assignments. I've come across something I want to do, but don't seem to be able to, ie. I have searched google (possibly with a poor keyword set) and not found a solution.
The specific case is as follows: I want to put an ams flalign environment inside a box and have multiple such environments side by side. I have achieved this using minipage, but minipage asks for a width. I would like to use the smallest width in which the flalign environment fits. I realize that I can set the width to 0pt, but I can't help wondering if there's something that is intended to do this.
Also, should I be using minipage? Is there another command I don't know?
Thanks for your reply.
EDIT:
An attempted clarification as to what I want to do. I want equations which are standard, known, given, etc. and short on the left. To the right of those, I want relevant derived equations (and maybe their derivations. Further right, I want actual calculations plugged in.
I feel like what I want is a tabular environment with 3 columns, but I don't think I can put an equation environment in a tabular environment.
This looks like what I want when I render it.
\begin{minipage}[t]{0pt}
\begin{flalign*}
\sigma & = F / A&\\
A & = \pi \left(d/2\right)^2&\\
\epsilon &= \frac{\sigma}{E}&\\
\epsilon_{trans} &= - \nu \epsilon_{longi}& \\
\epsilon &= \frac{\Delta l}{l}&\\
l &= \left( \epsilon + 1 \right) \times l_0&
\end{flalign*}
\end{minipage}
\hspace*{0pt}
\begin{minipage}[t]{0pt}
\begin{flalign*}
d & = \unit[1.8]{mm} = \unit[1.8\e{-3}]{m} &\\
F_T & = \unit[1300]{N}&\\
E_{\text{stainless steel}}&=\unit[193\e9]{Pa}&\\
l_0 & = \unit[.2530]{m}&\\
\nu & = .33&\\
\sigma &= \frac{\unit[1300]{N}}{\pi \times \unit[3.24\e{-6}]{m^2}}&&= \boxed{\unit[127.7\e6]{Pa}}\\
&&&=\boxed{\unit[18,524]{psi}}\\
\epsilon &= \frac{\unit[127.7\e6]{Pa}}{\unit[193\e9]{Pa}} &&= \boxed{6.6\e{-2}}\\
\epsilon_{trans} &= -.33 \times 6.6\e{-2} &&=\boxed{-2.2\e{-2}}\\
l &= \left( 6.6\e{-2} + 1 \right) \times \unit[.2530]{m} &&= \boxed{\unit[.2797]{m}}
\end{flalign*}
\end{minipage}
I'm not sure exactly what you're trying to achieve, but amsmath's align* environment might do what you want (without resorting to minipages):
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
x&=y & X&=Y & a&=b+c & mn&=ab\\
x’&=y’ & X’&=Y’ & a’&=b & m'n'&=a'b'\\
x+x’&=y+y’ & X+X’&=Y+Y’ & a’b&=c’b & m'&=a'
\end{align*}
\end{document}
As to your minipage question: it requires a width because TeX needs to know where to break the lines. If you don't want the line-breaking algorithm to be used, you probably don't want a minipage.
Edit:
If you want multiple columns and don't care about the vertical alignment of material across the columns, that can be obtained easily enough with the multicols package:
\documentclass{article}
\usepackage{multicols}
\usepackage{lipsum}% just for some example text
\begin{document}
% The * version allows the columns to have ragged bottoms.
% The argument 2 is the number of columns.
\begin{multicols*}{2}
\lipsum[1]% one paragraph of Lorem ipsum.. filler text
\vfil% fills the remainder of the column with white space
\columnbreak% force a column break
\lipsum[2]% another paragraph of text
\vfil% fills the remainder of the column with white space
\end{multicols*}
\end{document}
You might find something to help you in the empheq and mathtools packages. empheq allows you to box equations and mathtools should provide some useful environments for stacking them horizontally.

Resources