2

I have a Table in Tikz where the first column is a consecutive enumeration i.e.

\documentclass[12pt,twoside]{report}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,shadings,arrows,decorations.markings,decorations.pathmorphing}
\tikzset{
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            rectangle,
            draw=black,
            align=center,
                        text width=10em,
                        fill=gray!10,
                   },
                        %baseline={([yshift=-0.5ex]current bounding box.center)},
        %minimum height=1.0em,
        %text depth=0.2em,
        %text height=0.8em,
                %text centered,
        nodes in empty cells,
                row 1/.style={nodes={fill=black, text=white, font=\bfseries}},
                row 2/.style={nodes={text height=3.3ex}},
            }
        }


\begin{document}

Hello World...
\begin{table}
\centering
\begin{tikzpicture}
\matrix[table, ampersand replacement=\&] (TabA1)
{
1 \& A2 \& A3 \\
2 \& $10^{2^{3^{4^{5}}}}$ \& B3 \\
3 \& A2 \& A3 \\
4 \& A2 \& A3 \\
5 \& A2 \& A3 \\
6 \& A2 \& A3 \\
7 \& A2 \& A3 \\
8 \& A2 \& A3 \\
};
\end{tikzpicture}
\caption{Glossary}
\label{tabA1}
\end{table}


\end{document}

How can this be done automatically? I guess using a counter or is there a very simple solution in tikz?

1
  • Done Big Brother ;)
    – Diger
    Jul 3, 2018 at 13:33

1 Answer 1

5

You can add a style for the first column:

column 1/.style={nodes={node contents={\the\pgfmatrixcurrentrow}}}

Note that the nodes in the first column do not accept additional contents anymore.

Full MWE:

\documentclass[12pt,twoside]{report}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix,shadings,arrows,decorations.markings,decorations.pathmorphing}
\tikzset{
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            rectangle,
            draw=black,
            align=center,
                        text width=10em,
                        fill=gray!10,
                   },
                        %baseline={([yshift=-0.5ex]current bounding box.center)},
        %minimum height=1.0em,
        %text depth=0.2em,
        %text height=0.8em,
                %text centered,
        nodes in empty cells,
                row 1/.style={nodes={fill=black, text=white, font=\bfseries}},
                row 2/.style={nodes={text height=3.3ex}},
                column 1/.style={nodes={node contents={\the\pgfmatrixcurrentrow}}},
            }
        }


\begin{document}

Hello World...
\begin{table}
\centering
\begin{tikzpicture}
\matrix[table, ampersand replacement=\&] (TabA1)
{
 \& A2 \& A3 \\
 \& $10^{2^{3^{4^{5}}}}$ \& B3 \\
 \& A2 \& A3 \\
 \& A2 \& A3 \\
 \& A2 \& A3 \\
 \& A2 \& A3 \\
 \& A2 \& A3 \\
 \& A2 \& A3 \\
};
\end{tikzpicture}
\caption{Glossary}
\label{tabA1}
\end{table}


\end{document}
7
  • Do I need to specify this globally or can I also just add it to this one tikzpicture? So basically [table, ampersand replacement=\&, column 1/.style...]
    – Diger
    Jul 3, 2018 at 12:32
  • @Diger You can add it to a specific matrix, as you suggest. Jul 3, 2018 at 12:44
  • The \numexpr isn't necessary by the way. Jul 3, 2018 at 12:44
  • What is it for?
    – Diger
    Jul 3, 2018 at 12:49
  • @TorbjørnT. Thanks for the heads-up, I will remove it.
    – Max
    Jul 3, 2018 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .