0

I found this question, but that seems to specifically apply to center based on the decimal, and my table isn't numeric data. I'm trying to make the Cayley table for the quaternions, and a minimal working example for what my table looks like is:

\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath,amsthm,amsfonts,amssymb,amscd}

\begin{document}

\begin{table}[h]
    \begin{center}
        \begin{tabular}{l|llllllll}
            $*$&$1$&$-1$&$i$&$-i$&$j$&$-j$&$k$&$-k$\\\hline
            $1$&$1$&$-1$&$i$&$-i$&$j$&$-j$&$k$&$-k$\\
            $-1$&$-1$&$1$&$-i$&$i$&$-j$&$j$&$-k$&$k$\\
            $i$&$i$&$-i$&$-1$&$1$&$k$&$-k$&$-j$&$j$\\
            $-i$&$-i$&$i$&$1$&$-1$&$-k$&$k$&$j$&$-j$\\
            $j$&$j$&$-j$&$-k$&$k$&$-1$&$1$&$i$&$-i$\\
            $-j$&$-j$&$j$&$k$&$-k$&$1$&$-1$&$-i$&$i$\\
            $k$&$k$&$-k$&$-j$&$j$&$-i$&$i$&$1$&$-1$\\
            $-k$&$-k$&$k$&$j$&$-j$&$i$&$-i$&$-1$&$1$\\
        \end{tabular}
    \end{center}
\end{table}

\end{document}

which produces the following output.

bad Cayley table

What I'd like for the table to look like is this:

good Cayley table

I'm sure that there's already a similar question on this site, but I couldn't find anything that applied to non-numeric cell values.

  • Possibly related: https://tex.stackexchange.com/questions/378290/align-elements-in-tabular-like-align-environment – steve Apr 24 '20 at 00:56

1 Answers1

1

I'm almost 100% sure there's better solutions, but you could use \phantom{-}:

\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath,amsthm,amsfonts,amssymb,amscd}

\begin{document}

\begin{table}[h]
    \begin{center}
        \begin{tabular}{l|llllllll}
            $*$ & $\phantom{-}1$ & $-1$ & $\phantom{-}i$ & $-i$ & $\phantom{-}j$ & $-j$ & $\phantom{-}k$ & $-k$ \\\hline
            $\phantom{-}1$ & $\phantom{-}1$ & $-1$ & $\phantom{-}i$ & $-i$ & $\phantom{-}j$ & $-j$ & $\phantom{-}k$ & $-k$ \\
            $-1$ & $-1$ & $\phantom{-}1$ & $-i$ & $\phantom{-}i$ & $-j$ & $\phantom{-}j$ & $-k$ & $\phantom{-}k$ \\
            $\phantom{-}i$ & $\phantom{-}i$ & $-i$ & $-1$ & $\phantom{-}1$ & $\phantom{-}k$ & $-k$ & $-j$ & $\phantom{-}j$ \\
            $-i$ & $-i$ & $\phantom{-}i$ & $\phantom{-}1$ & $-1$ & $-k$ & $\phantom{-}k$ & $\phantom{-}j$ & $-j$ \\
            $\phantom{-}j$ & $\phantom{-}j$ & $-j$ & $-k$ & $\phantom{-}k$ & $-1$ & $\phantom{-}1$ & $\phantom{-}i$ & $-i$ \\
            $-j$ & $-j$ & $\phantom{-}j$ & $\phantom{-}k$ & $-k$ & $\phantom{-}1$ & $-1$ & $-i$ & $\phantom{-}i$ \\
            $\phantom{-}k$ & $\phantom{-}k$ & $-k$ & $-j$ & $\phantom{-}j$ & $-i$ & $\phantom{-}i$ & $\phantom{-}1$ & $-1$ \\
            $-k$ & $-k$ & $\phantom{-}k$ & $\phantom{-}j$ & $-j$ & $\phantom{-}i$ & $-i$ & $-1$ & $\phantom{-}1$ \\
        \end{tabular}
    \end{center}
\end{table}

\end{document}

screenshot

Or, in this case, even just aligning the columns to the right might do (it does not produce the exact same output, though):

\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath,amsthm,amsfonts,amssymb,amscd}

\begin{document}

\begin{table}[h]
    \begin{center}
        \begin{tabular}{r|rrrrrrrr}
            $*$  & $1$  & $-1$ & $i$  & $-i$ & $j$  & $-j$ & $k$  & $-k$ \\\hline
            $1$  & $1$  & $-1$ & $i$  & $-i$ & $j$  & $-j$ & $k$  & $-k$ \\
            $-1$ & $-1$ & $1$  & $-i$ & $i$  & $-j$ & $j$  & $-k$ & $k$ \\
            $i$  & $i$  & $-i$ & $-1$ & $1$  & $k$  & $-k$ & $-j$ & $j$ \\
            $-i$ & $-i$ & $i$  & $1$  & $-1$ & $-k$ & $k$  & $j$  & $-j$ \\
            $j$  & $j$  & $-j$ & $-k$ & $k$  & $-1$ & $1$  & $i$  & $-i$ \\
            $-j$ & $-j$ & $j$  & $k$  & $-k$ & $1$  & $-1$ & $-i$ & $i$ \\
            $k$  & $k$  & $-k$ & $-j$ & $j$  & $-i$ & $i$  & $1$  & $-1$ \\
            $-k$ & $-k$ & $k$  & $j$  & $-j$ & $i$  & $-i$ & $-1$ & $1$ \\
        \end{tabular}
    \end{center}
\end{table}

\end{document}

Also, Math mode in tabular without having to use $...$ everywhere might be of interest. All the best!

steve
  • 2,154
  • Oh yeah, right aligning the columns looks perfect! – Calvin Godfrey Apr 24 '20 at 00:51
  • 2
    Right-aligning the columns would probably look pretty good until you get a $-m$. But here's a suggestion for a "shorthand'': \newcommand{\0}{\phantom{-}. Then a positive cell might contain \01. The digits are left for use in temporary definitions, and since such a command can be only the one digit long, no space is wanted between it and what follows. – barbara beeton Apr 24 '20 at 01:43
  • Oops! The \newcommand{\0){...} that I suggested in an earlier comment is missing a final }. (Sorry.) – barbara beeton Apr 24 '20 at 14:49