5

Consider this MWE, modified from Aligning conditions in cases environment:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \left\{\begin{alignedat}{3}
    & mx^2 +nx +1, &\text{if } & x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &\text{if } -1 < {}&x < 1 \\
    & 3x^2 - (m+n)x, &\text{otherwise} &
  \end{alignedat}\right.
\]
\end{document}

It results with:

test.png

I'd like to have both the ifs and otherwise aligned; and the xs aligned.

Can that be done with {aligned}? If so, where am I going wrong?

sdaau
  • 17,079

3 Answers3

5

You need to use && to get the conditions left aligned. To get the x aligned as well, in this case I would recommend using an \hphantom:

enter image description here

Notes:

  • You need to use {-1} for the second case so that the - is treated as a unary operator instead of a binary operator.

To answer your questions from the comments:

  • Each & provides a right/left alignment point. That is, text before the & is right aligned and the text after the & is l aligned. So the first & aligns the f(x) = { to the right and the the subsequent expressions to the left. Then, the subsequent text (the beginning of the condition) is desired to be l aligned. That means that we need &&. The first & would give a right alignment, -- the second & ensures that we have a left alignment.
  • Using the \hphantom was an easy way to get the desired alignment. Sure it could be done with a & but that would have necessitated a use of a \lap type of macros due to the otherwise text. Note that the inequality expressions overlap the otherwise text.

Here is the results of the other two approaches:

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\noindent
Recommended approach: use a \verb|\hphantom{}|: 
\[
      f(x) = \left\{\begin{alignedat}{3}
        & mx^2 +nx +1,                            &&\text{if } \hphantom{-1 <{}}   x \le -1 \\
        & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } {-1} < x < 1 \\
        & 3x^2 - (m+n)x,                          &&\text{otherwise} 
      \end{alignedat}\right.
    \]
Use additional \verb|&| instead of \verb|\hphantom{}|:
\[
  f(x) = \left\{\begin{alignedat}{4}
    & mx^2 +nx +1,                            &&\text{if } &      &&  &x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } & {-1} &&{}< {}&x < 1 \\
    & 3x^2 - (m+n)x,                          &&\text{otherwise} 
  \end{alignedat}\right.
\]
With \verb|mathllap|:
\[
  f(x) = \left\{\begin{alignedat}{4}
    & mx^2 +nx +1,                            &&\text{if } &      &&  &x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  &&\text{if } & {-1} &&{}< {}&x < 1 \\
    & 3x^2 - (m+n)x,                          &&\text{\rlap{otherwise}} 
  \end{alignedat}\right.
\]
\end{document}
Peter Grill
  • 223,288
  • Many thanks, @PeterGrill - that looks freat, will accept as soon as the timer expires. Btw, why is it necessary to insert the phantom space; I'd think that ampersand & is enough to specify a column? And why are the double && required? Thanks again - cheers! – sdaau Feb 20 '15 at 20:07
  • @sdaau: Have updated the answer to explain the double && and show the complexities of just using a & for the alignment. Let me know if that makes sense. – Peter Grill Feb 21 '15 at 03:09
5

An alternative would be to use the cases environment. It needs less ampersands here, at the cost of the x alignment, but I personally think it's more elegant so.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \begin{cases}
    mx^2 +nx +1, &\text{if }  x \le -1 \\
    2m e^{|x|-1} + \sin \pi x - 3n,  &\text{if } -1 < x < 1 \\
    3x^2 - (m+n)x,  &\text{otherwise}
  \end{cases}
\]
\end{document}

enter image description here

Franck Pastor
  • 18,756
1

you can use this

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \left\{\begin{alignedat}{3}
    & mx^2 +nx +1,                             &&\text{if }         &    & x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad   &&\text{if }         &-1< & x < 1 \\
    & 3x^2 - (m+n)x,                           &&\text{otherwise}   &&
  \end{alignedat}\right.
\]
\end{document}

EDIT: image:

test.png

edit (by bbeeton): an adjusted image:

output of adjusted code

the difference here is in the spacing around the left-hand < sign in the second line. this can be accomplished in either of two ways:

  • ... &-1<{} & x < 1
  • by taking advantage of the fact that the x is the same width in both lines, and entering them as

    & mx^2 +nx +1,                             &&\text{if }         &     x &\le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad   &&\text{if }         & -1< x &< 1 \\
    

proper spacing around signs of operation and relation is guaranteed only if the aligning & precedes the sign.

touhami
  • 19,520
  • Thanks @touhami - edited your answer, and added an image; this indeed aligns both if and x, the spacing looks a bit too much ... Cheers! – sdaau Feb 20 '15 at 20:17
  • 1
    @sdaau thanks, i edited my answer to correct {alignedat}{5} to {alignedat}{3} – touhami Feb 20 '15 at 20:19
  • Thanks @touhami - but that still doesn't change the large space between if and -1 on the second line which could have been shorter, that's what I meant... – sdaau Feb 20 '15 at 20:23
  • 1
    bad spacing after the first < in the second line. that shouldn't happen with the coding shown. – barbara beeton Feb 20 '15 at 20:24
  • Thanks, @barbarabeeton - I built the latest code as of now, and the spacing is still shown: http://i.stack.imgur.com/9G6Qe.png ; I've indicated what I mean. UPS; wait - that is actually because of the otherwise being too big - it seems as expected to me now... – sdaau Feb 20 '15 at 20:35
  • @sdaau -- that's not what i meant -- the spaces around the less-than signs in the second line should be uniform, and they're not. you could take advantage of the fact that the x in both lines at that point is always the same width, and move the & signs to (in the first line) just before the \le, and (in the second line) to & < 1 and that would fix the spacing. &s should always (if feasible) go before signs of operation or relation to ensure proper spacing. – barbara beeton Feb 20 '15 at 21:34
  • @barbarabeeton you can edit my answer. – touhami Feb 20 '15 at 21:38