How to draw a Cayley table

Your input is almost good, but you can do better.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\begin{document}

This is a Cayley table
\[
    \begin{array}{l|*{5}{l}}
        & 1   & a   & a^2 & a^3  & a^4 \\
    \hline
    1   & 1   & a   & a^2 & a^3  & a^4 \\
    a   & a   & a^2 & a^3 & a^4  & a^5 \\
    a^2 & a^2 & a^3 & a^4 & a^5  & a^6 \\
    a^3 & a^3 & a^4 & a^5 & a^6  & a^7 \\
    a^4 & a^4 & a^5 & a^6 & a^7  & a^8 \\
    \end{array} 
\]

This is the same, but with some more spacing,
\[
    \setlength{\extrarowheight}{3pt}% local setting
    \begin{array}{l|*{5}{l}}
        & 1   & a   & a^2 & a^3  & a^4 \\
    \hline
    1   & 1   & a   & a^2 & a^3  & a^4 \\
    a   & a   & a^2 & a^3 & a^4  & a^5 \\
    a^2 & a^2 & a^3 & a^4 & a^5  & a^6 \\
    a^3 & a^3 & a^4 & a^5 & a^6  & a^7 \\
    a^4 & a^4 & a^5 & a^6 & a^7  & a^8 \\
    \end{array} 
\]

\end{document}

With array, each cell is assumed to be in math mode. The setting of \extrarowheight will not propagate to other instances of array, because \[...\] forms a group.

enter image description here


Basically I've just taken the answer to the question you linked and changed few lines to adapt to your needs.

Here's the code, followed by the output you will get and a brief explanation of what's going on.

The code below produces 2 tables, in one there's written ANYTHING in the top left corner, in the second one there's a white space (just don't put anything in the column "argument".

\documentclass[10pt]{article}
\usepackage{array}

\begin{document}

    \setlength\extrarowheight{3pt}
\noindent\begin{tabular}{c | c c c c c}
    ANYTHING & 0 & 1 & 2 & 3 & 4  \\
    \cline{1-6}
    0 & 0 & 0 & 0 & 0 & 0 \\
    1 & 0 & 0 & $a^2$ & 25 & 25 \\
    2 & $a^2$ & 0 & 20 & 25 & 25 \\
    2 & 0 & $a^2$ & 20 & 25 & 25 \\
    2 & 0 & 0 & 20 & 25 & 25 \\
\end{tabular}


\noindent\begin{tabular}{c | c c c c c}
     & 0 & 1 & 2 & 3 & 4  \\
    \cline{1-6}
    0 & 0 & 0 & 0 & 0 & 0 \\
    1 & 0 & 0 & $a^2$ & 25 & 25 \\
    2 & $a^2$ & 0 & 20 & 25 & 25 \\
    2 & 0 & $a^2$ & 20 & 25 & 25 \\
    2 & 0 & 0 & 20 & 25 & 25 \\
\end{tabular}

\end{document}

enter image description here enter image description here

How does it work? A table is done with the environment {tabular}, which requires as argument the numer of columns (or rows... just read the documentation to know more) which is specified by the argument {c | c c c c c}: 6 columns divided by a line between the first and the second col. The 'c' stands for centered meaning that the column will be aligned to the center, if you want you can change it to l (left) or r (right).

Then in the tables all the entries are done by spacing them with &, and the \cline takes as argument the number of columns that you want it to span: from column 1 to column 6.

The command \setlength\extrarowheight{3pt} adds some extra row height (how surprising!).

Package {array} is needed for the length \extrarowheight.


Some automatizing is in order here

\documentclass{article}
\usepackage{amsmath, amssymb}
\usepackage{array}

\usepackage{xinttools}
\usepackage{bnumexpr}
\begin{document}

This is a Cayley table
\[
    \begin{array}{l|*{5}{l}}
        & 1   & a   & a^2 & a^3  & a^4 \\
    \hline
    1   & 1   & a   & a^2 & a^3  & a^4 \\
    a   & a   & a^2 & a^3 & a^4  & a^5 \\
    a^2 & a^2 & a^3 & a^4 & a^5  & a^6 \\
    a^3 & a^3 & a^4 & a^5 & a^6  & a^7 \\
    a^4 & a^4 & a^5 & a^6 & a^7  & a^8 \\
    \end{array} 
\]

This is the same, but with some more spacing,
\[
    \setlength{\extrarowheight}{3pt}% local setting
    \begin{array}{l|*{5}{l}}
        & 1   & a   & a^2 & a^3  & a^4 \\
    \hline
    1   & 1   & a   & a^2 & a^3  & a^4 \\
    a   & a   & a^2 & a^3 & a^4  & a^5 \\
    a^2 & a^2 & a^3 & a^4 & a^5  & a^6 \\
    a^3 & a^3 & a^4 & a^5 & a^6  & a^7 \\
    a^4 & a^4 & a^5 & a^6 & a^7  & a^8 \\
    \end{array} 
\]

Again, but illustrating that $a=4$ generates a cyclic sub-group of order
$5$ of $(\mathbb{Z}/11\mathbb{Z})^*$:
\[
    \setlength{\extrarowheight}{3pt}% local setting
    \begin{array}{c|*{5}{c}}
        & 1   & a   & a^2 & a^3  & a^4 \\
    \hline
    \xintFor #1 in {0, 1, 2, 3, 4}:
    {\ifnum #1=0 1\else a^{#1}\fi 
       \xintFor #2 in {0, 1, 2, 3, 4}:
      {%
       &\thebnumexpr 4^(#1+#2) /: 11\relax
      }%
    \\
    }%
    \end{array} 
\]
and $a=2$ generates the full group:
\[
    \setlength{\extrarowheight}{3pt}% local setting
    \begin{array}{c|*{10}{c}}
        & 1 & a  \xintFor*#1 in {23456789}:{&a^{#1}}\\
    \hline
    \xintFor* #1 in {0123456789}:
    {\ifnum #1=0 1\else a^{#1}\fi 
       \xintFor* #2 in {0123456789}:
      {%
       &\thebnumexpr 2^(#1+#2) /: 11\relax
      }%
    \\
    }%
    \end{array} 
\]
\end{document}

enter image description here

Tags:

Tables