# Aligning space characters with non-space characters with alignat

I have the code

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, amsthm}

\begin{document}
\begin{alignat*}{3}
2^1 &= 2 \pmod{21}, &\qquad 2^5 &= 32   &= 11 \pmod{21},\\
2^2 &= 4 \pmod{21}, &\qquad 2^6 &= 64   &= 1  \pmod{21}.\\
\end{alignat*}
\end{document}


but it's producing this:

How do I get those equals on the right to be lined up?

• Welcome to TeX.SX! I get an error when compiling your document, not even producing the output. You have too many &s for your 2 alignment points. – TeXnician May 3 '18 at 5:29
• Thank you! So that number after the alignat command should be a 4? I'll test that out. – Pro Q May 3 '18 at 5:34
• No, it should be 3: count your & characters add 1 and divide that by 2 (rounding up). – TeXnician May 3 '18 at 5:35
• @TeXnician Okay, so it looks like changing it to a 4 got rid of the errors, but still didn't fix the alignment. I'll edit the question. – Pro Q May 3 '18 at 5:35
• Correct, but at least it's compilable now. Are you only interested in an alignat solution? – TeXnician May 3 '18 at 5:36

An alignat solution, even if I would use an ordinary tabular or array, see here.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, amsthm}

\begin{document}

\begin{alignat*}{4}
2^0 &= 1 \pmod{21},&\qquad  2^4 & &  & = {} &  16    &  \pmod{21},\\
2^1 &= 2 \pmod{21}, &  2^5 &= 32 & & =   & 11  &   \pmod{21},\\
2^2 &= 4 \pmod{21}, &  2^6 &= 64 & & =  & 1  &  \pmod{21}.\\
2^3 &= 8 \pmod{21},
\end{alignat*}

\end{document}


• Thank you! What do the double &&s do? – Pro Q May 3 '18 at 6:38
• @ProQ They change the left/right alignment, see the link in my answer. – CarLaTeX May 3 '18 at 6:47
• Shouldn't there be an odd number of ampersands in each row (twice the number of columns – 1)? – Bernard May 3 '18 at 8:19
• @Bernard Please correct it! I did it on a hurry and now I can't, thank you! – CarLaTeX May 3 '18 at 8:22
• @CarlaTeX: È fatto! – Bernard May 3 '18 at 9:47

Here, I use a \tabularCenterstack, which has column-alignment features similar to tabular, while providing math spacing features across tabs, similar to align.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, amsthm, tabstackengine}
\TABstackMath
\begin{document}
\TABbinary
\setstackgap{L}{1.2\baselineskip}
\setstacktabulargap{0pt}
$\tabularCenterstack{rlrlcrl}{ 2^0 &= 1 \pmod{21}, &\qquad 2^4 & &=& 16 &\pmod{21},\\ 2^1 &= 2 \pmod{21}, &\qquad 2^5 &= 32 &=& 11 &\pmod{21},\\ 2^2 &= 4 \pmod{21}, &\qquad 2^6 &= 64 &=& 1 &\pmod{21}.\\ 2^3 &= 8 \pmod{21}, & & & & & }$
\end{document}