# Aligning equations in align

I wanted something aligned like the following:

All equations are aligned at $\to$, while the top two equations are aligned at countingUp. The middle two equations are aligned at counter. The last equation is center-aligned, like gather.

Here is the base for what I have:

\begin{align*}
countingUp \wedge counter < 100 &\to [counter \leftarrow counter + 1] \\
\neg countingUp \wedge counter > 0 &\to [counter \leftarrow counter - 1] \\
counter = 100 &\to \neg countingUp \\
counter = 0 &\to countingUp \\
0 \leq~&counter \leq 100
\end{align*}


Use alignat* to choose the alignment points manually. A little trickery is needed for the last line:

\documentclass{article}

\begin{document}

\begin{alignat*}{2}
\mathit{countingUp} \wedge \mathit{counter} &< 100 &&\to [\mathit{counter} \leftarrow \mathit{counter} + 1] \\
\neg \mathit{countingUp} \wedge \mathit{counter} &> 0 &&\to [\mathit{counter} \leftarrow \mathit{counter} - 1] \\
\mathit{counter} &= 100 &&\to \neg \mathit{countingUp} \\
\mathit{counter} &= 0 &&\to \mathit{countingUp} \\
0 &\leq \mathrlap{\mathit{counter} \leq 100}
\end{alignat*}

\end{document}


• Did not know about the mathtools package. Thank you. – gust Mar 1 at 16:01
• @gust mathtools is needed for the \mathrlap macro. You can obtain the same with amsmath alone with \makebox. – campa Mar 1 at 17:55

You could use gather* and the first four lines in a nested align*, but the result would not be pretty.

You can make the last row to be half before the arrow and half after it.

In the code, \tv stands for “textual variable”. Using a semantic command will allow to change the typographical realization of all these objects in one swoop by modifying just the definition.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\tv}[1]{\mathit{#1}}

\begin{document}

\begin{align*}
\tv{countingUp} \wedge \tv{counter} < 100
&\to [\tv{counter} \leftarrow \tv{counter} + 1] \\
\neg \tv{countingUp} \wedge \tv{counter} > 0
&\to [\tv{counter} \leftarrow \tv{counter} - 1] \\
\tv{counter} = 100
&\to \neg \tv{countingUp} \\
\tv{counter} = 0
&\to \tv{countingUp} \\
&\makebox[0.5\width][r]{$\hphantom{\to{}}0 \leq counter \leq 100$}
\end{align*}

\end{document}


I'd consider left alignment, though.

\begin{align*}
& \tv{countingUp} \wedge \tv{counter} < 100 \to [\tv{counter} \leftarrow \tv{counter} + 1] \\
& \neg \tv{countingUp} \wedge \tv{counter} > 0 \to [\tv{counter} \leftarrow \tv{counter} - 1] \\
& \tv{counter} = 100 \to \neg \tv{countingUp} \\
& \tv{counter} = 0 \to \tv{countingUp} \\
& 0 \leq counter \leq 100
\end{align*}