How can I produce the following equations with Latex? Note the alignment of terms in the linear equations and the "hints" to the right. I have tried using the ordinary cases
by could not get the result I wanted without having \
all over the place.
[Tex/LaTex] Equation alignment – linear equations
equationshorizontal alignmentvertical alignment
Related Solutions
I have encountered this situation a lot and the following method can be adapted to almost all cases. Similar to Barbara's solution, this involves deciding what is the largest text and using \makebox
to set the type into a box equal to the width of the largest string.
If also inserted a few \phantoms
to get exact alignment of the cases.
\documentclass{article}
\usepackage{amstext}
\newcommand*{\LongestText}{fermentum fringilla mauris }%
\newlength{\LargestSize}%
\settowidth{\LargestSize}{\LongestText}%
\newcommand*{\MakeBox}[1]{\makebox[\LargestSize][l]{#1}}%
\newcommand*{\MakeBoxText}[1]{\text{\MakeBox{#1}}}%
\begin{document}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit:
\begin{equation}
f(x) = \left\{
\begin{array}{l l}
1\phantom{-} & \MakeBoxText{ nisl justo, hendrerit} \\
0\phantom{-} & \MakeBoxText{ sagittis condimentum}
\end{array} \right.
\end{equation}
\item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:
\begin{equation}
g(x) = \left\{
\begin{array}{l l}
-1 & \MakeBoxText{ fermentum fringilla mauris eget} \\
\phantom{-}0 & \MakeBoxText{ gravida ipsum vitae}
\end{array} \right.
\end{equation}
\end{itemize}
\end{document}
Update: 2011-09-23: In order to be able to handle a general case you can use the technique shown above to each element of the function separately:
- the function name
- the case values
- the case text
Then each portion gets places in a fixed width box and everything lines up exactly even with more complicated expressions:
Below, I have three macros which are to be used for each of the portions. I have chosen a [r]
alignment for the function name, and a [l]
alignment for the case values and case text.
\documentclass{article}
\usepackage{amsmath}
\newcommand*{\LongestName}{\ensuremath{h(x)+g(x)}}% function name
\newcommand*{\LongestValue}{\ensuremath{-1}}% function value
\newcommand*{\LongestText}{fermentum fringilla mauris }%
\newlength{\LargestNameSize}%
\newlength{\LargestValueSize}%
\newlength{\LargestTextSize}%
\settowidth{\LargestNameSize}{\LongestName}%
\settowidth{\LargestValueSize}{\LongestValue}%
\settowidth{\LargestTextSize}{\LongestText}%
% Choose alignment of the various elements here: [r], [l] or [c]
\newcommand*{\MakeBoxName}[1]{{\makebox[\LargestNameSize][r]{\ensuremath{#1}}}}%
\newcommand*{\MakeBoxValue}[1]{\ensuremath{\makebox[\LargestValueSize][l]{\ensuremath{#1}}}}%
\newcommand*{\MakeBoxText}[1]{\makebox[\LargestTextSize][l]{#1}}%
\begin{document}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit:
\begin{equation}
\MakeBoxName{f(x)} = \left\{
\begin{array}{l l}
\MakeBoxValue{1} & \MakeBoxText{ nisl justo, hendrerit} \\
\MakeBoxValue{0} & \MakeBoxText{ sagittis condimentum}
\end{array} \right.
\end{equation}
\item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:
\begin{equation}
\MakeBoxName{g(x)} = \left\{
\begin{array}{l l}
\MakeBoxValue{-1} & \MakeBoxText{ fermentum fringilla mauris eget}\\
\MakeBoxValue{\phantom{-}0} & \MakeBoxText{ gravida ipsum vitae}
\end{array} \right.
\end{equation}
\item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:
\begin{equation}
\MakeBoxName{h(x)+g(x)} = \left\{
\begin{array}{l l}
\MakeBoxValue{\sin x} & \MakeBoxText{ fermentum fringilla mauris eget} \\
\MakeBoxValue{0} & \MakeBoxText{ gravida ipsum vitae}
\end{array} \right.
\end{equation}
\end{itemize}
\end{document}
Not sure how you prefer to have the function values aligned, but this is how I have been aligning them. You can remove the \phantom{-}
and and adjust the alignments as desired.
If you want the single equation number to be centered vertically on the group of aligned equations, it's best to use a single equation
environment containing a split
environment.
A couple of additional suggestions: (1) Use the macro \invexpsqrt{\gamma_D}
consistently (the second line doesn't contain them in your MWE). (2) Don't use auto-sized parentheses and bracket as they're a bit too large for the job at hand; use \Bigl
and \Bigr
instead.
With these adjustments in place, there's enough room to place the equation number right where you want it to be.
\documentclass{article}
\usepackage{amsmath}
\newcommand{\invexpsqrt}[1]{#1^{-\frac{1}{2}}}
\begin{document}
\begin{equation}
\begin{split}
\rho_{out}
&= \exp \Bigl[ -\tfrac{1}{2} \Bigl( x \cdot \gamma \cdot x + x' \cdot \gamma \cdot x' \Bigr) + x \cdot \beta \cdot x' \Bigr] \\
&= \exp \Bigl[-\tfrac{1}{2} \Bigl( V \invexpsqrt{\gamma_D} y V^T \gamma_D V V^T \invexpsqrt{\gamma_D} y + V \invexpsqrt{\gamma_D} y' V^T \gamma_D V V^T \invexpsqrt{\gamma_D} y' \Bigr) \\
&\qquad \qquad + V \invexpsqrt{\gamma_D} y \beta V^T \invexpsqrt{\gamma_D} y' \Bigr] \\
&= \exp \Bigl[ -\tfrac{1}{2} \Bigl( y \cdot y + y' \cdot y' \Bigr) + y \cdot \Bigl( \invexpsqrt{\gamma_D} V \beta V^T \invexpsqrt{\gamma_D} \Bigr) \cdot y' \Bigr] \\
&= \exp \Bigl[ -\tfrac{1}{2} \Bigl( y \cdot y + y' \cdot y' \Bigr) + y \cdot \beta' \cdot y' \Bigr]
\end{split}
\end{equation}
\end{document}
Best Answer
You can also use the systeme package: