# Alignment within cases

I'm trying to align the if's in the following MWE, but also keep the (n times) alignment intact. Applying the mechanism from Multiple = alignment and from Aligning conditions in cases environment, I still could not get it to work.

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{amsmath}

\begin{document}
\begin{equation*}
ns =
\begin{cases}
\quad s + s + ... + s  & \text{ (n times) if } n > 0 \\
\quad (-s) + (-s) + ...+ (-s)  & \text{ (n times) if }  n < 0
\end{cases}
\end{equation*}
\end{document}


Incidentally is there a way to remove the hideous \quad's ? (Does one question per question apply here?:)

You can use \hphantom in the second line to move the text over, or alternatively \hfill, as Mico mentions in a comment.

What's the purpose of the \quads anyway?

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{amsmath}
\begin{document}
\begin{equation*}
ns =
\begin{cases}
s + s + \dots + s  & \text{ ($n$ times) if } n > 0 \\
0 & \text{\hphantom{ ($n$ times)} if } n = 0  \\
(-s) + (-s) + \dots + (-s)  & \text{ ($n$ times) if }  n < 0
\end{cases}
\end{equation*}
\end{document}


If you want to change the alignment for the columns in cases, one possibility is to define a new environment with the help of mathtools, as described in Right aligned first column in a cases environment For example, to have the first column centered, and the second right aligned :

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{mathtools}
\makeatletter
\hfil$\m@th\displaystyle{##}$\hfil}{\hfil$\m@th\displaystyle{##}$}{\lbrace}{.}
\makeatother
\begin{document}
\begin{equation*}
ns =
\begin{crcases}
s + s + \dots + s  & \text{ ($n$ times) if } n > 0 \\
0 & \text{ if } n = 0  \\
(-s) + (-s) + \dots + (-s)  & \text{ ($n$ times) if }  n < 0
\end{crcases}
\end{equation*}
\end{document}


• The \quad's were to align the 0 near the center of the equations above and below. Apr 24, 2017 at 21:25
• ... should be \dots Apr 24, 2017 at 21:30
• In your first example, consider writing \hfill \text{if $n = 0$} instead of \text{\hphantom{ ($n$ times)} if } n = 0.
– Mico
Apr 24, 2017 at 21:30

With aligned (from amsmath package):

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{amsmath}

\begin{document}
ns = \left\{\begin{aligned}\quad & s + s + \dotsm + s & \text{ (n times) if } n & > 0 \\ & 0 & \text{ if } n & = 0 \\ & (-s) + (-s) + \dotsm + (-s) & \text{ (n times) if } n & < 0 \end{aligned}\right.
\end{document}

• +1 but I would say that \quad is excessively large. Also the n inside \text need to be in math mode. Apr 25, 2017 at 7:15
• @AndrewSwann, quad is left from OP MWE, I will reduce this distance. Similarly about n, I will correct this too. Apr 25, 2017 at 7:41

There should be a wider separation from “(n times)” to “if” than from the formulas and “(n times)”, in my opinion.

The 0 should be left flush, like the other two terms. I also fixed the math error (it should be |n| in the third line).

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{amsmath}

\begin{document}

\begin{equation*}
ns =
\begin{cases}
\begin{alignedat}{3}
&s + s + \dots + s          &\quad \text{($n$ times)}   &\qquad& \text{if $n > 0$} \\
&0                          &\quad                      &\qquad& \text{if $n = 0$} \\
&(-s) + (-s) + \dots + (-s) &\quad \text{($|n|$ times)} &\qquad& \text{if $n < 0$}
\end{alignedat}
\end{cases}
\end{equation*}

\end{document}


And with simple array:

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{amsmath}
\begin{document}

\begin{equation*}
ns = \left \{
\begin{array}{lrl}
s + s + \dots + s          & (n \text{ times})   & \text{ if } n > 0 \\
0                          &                     & \text{ if } n = 0  \\
(-s) + (-s) + \dots + (-s) & (|n| \text{ times}) & \text{ if } n < 0
\end{array} \right.
\end{equation*}

\end{document}


• \right. Very clever! It makes this mechanism very powerful. Apr 25, 2017 at 0:06

With empheq, eqparbox and alignat*, which allows for several alignment points, and full control on the spacing between columns. I also propose another layout (better in my opinion) which requires onlycases:

\documentclass[12pt,a4paper]{article}
\usepackage[fleqn]{empheq}
\usepackage{eqparbox}
\newcommand\eqmathbox[2][M]{\eqmakebox[#1]{$\displaystyle#2$}}
\usepackage[showframe]{geometry}

\begin{document}

\begin{empheq}[left = {ns=\empheqlbrace\,}]{alignat*=2}
&\eqmathbox{s + s + \dots + s} & \text{ (n times) } & \text{if } n > 0 \\
& \eqmathbox{0} & & \text{if } n = 0 \\
& (-s) + (-s) + \dots+ (-s) & \qquad\text{ (n times) } & \text{if } n < 0
\end{empheq}

\begin{equation*}
ns =
\begin{cases}
\eqmathbox[C]{\,s + s + \dots + s} \text{\quad (n times)} & \text{ if } n > 0 \\
\eqmathbox[C]{\,0} & \text{ if } n = 0 \\
\, (-s) + (-s) + \dots + (-s) \text{\quad (n times)} & \text{ if } n < 0
\end{cases}
\end{equation*}

\end{document}
`