5

Code:

     \begin{tikzpicture}
      \matrix[matrix of math nodes, anchor=south west,
      nodes={
      draw=red,
      align=center, 
      inner sep=0pt,
      text width=1cm - \pgflinewidth,
      minimum size = 1cm - \pgflinewidth
      }
      ]{
        A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
        0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
      };
     \foreach \x in {0,...,16}
     \node[align=center] at (\x+1,0) {\x};
     \end{tikzpicture}

Output:

enter image description here

How can I program LaTeX in order to have consecutive numbers out and below of the matrix corresponding the columns of the matrix? Like this:

enter image description here

Ignasi
  • 136,588
Andrea Leo
  • 1,011
  • Some examples: https://tex.stackexchange.com/a/386860/1952, https://tex.stackexchange.com/questions/279252/automated-matrix-padding-in-tikz/279545#279545 – Ignasi Dec 15 '17 at 14:26

5 Answers5

7

Another solution which automatically writes the column labels below the last row elements.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix[matrix of math nodes, anchor=south west,
    column sep=-\pgflinewidth,
    row sep=-\pgflinewidth,
    nodes={
        draw=red,
      align=center, 
      outer sep=0pt,
      inner sep=0pt,
      text width=1cm,
      minimum size = 1cm
    },
    row 2/.style={execute at begin cell={%
                |[label={[minimum size=6mm]below:\the\numexpr\pgfmatrixcurrentcolumn-1}]|},}
    ]{
        A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
      0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
    };
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
4

A very simple one

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}

     \begin{tikzpicture}
      \matrix[matrix of math nodes, anchor=south west,
      nodes={
      draw=red,
      align=center, 
      inner sep=0pt,
      text width=1cm - \pgflinewidth,
      minimum size = 1cm - \pgflinewidth
      }
      ]{
        A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
        0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
      };
     \foreach \x in {0,...,16}
      \node[%
       gray,
       anchor=north east,
       align=center,
       text width=\pgflinewidth,
       minimum size = 1cm - \pgflinewidth
       ]%
      at (\x+1,0) {\x};

      \end{tikzpicture}
\end{document}
Cragfelt
  • 4,005
3
 \documentclass{standalone}

 \usepackage{tikz}
 \usetikzlibrary{matrix}

 \begin{document}

 \begin{tikzpicture}
       \matrix[matrix of math nodes, anchor=south west,
       nodes={
       draw=red,
       align=center, 
       inner sep=0pt,
       text width=1cm - \pgflinewidth,
       minimum size = 1cm - \pgflinewidth
       }
       ]{
         A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
         0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
       };
      \foreach \x in {0,...,16}
      \node[align=center] at (\x+0.62,-0.04) {\x};
 \end{tikzpicture}

 \end{document}

enter image description here

Nakrule
  • 741
  • 2
    You can also use \foreach \x in {1,...,17}\node[below= of n-1-\x] {\number\numexpr\x-1}; if you call the matrix node (n) and use positioning library. – percusse Dec 15 '17 at 13:30
  • @percusse Thank you! one line to solve the problem! Can you explain the command \number\numexpr please? – Andrea Leo Dec 15 '17 at 15:59
  • @AndreaLeo They are eTeX primitives to do simple math on-the-fly which TeX doesn't do. https://tex.stackexchange.com/questions/88344/references-for-dimexpr-numexpr . For \number just use \the\numexpr as you would do with counters. It just fixes a technicality. – percusse Dec 15 '17 at 16:33
3

The idea of this solution is to put the numbers into the third row of the matrix. But since I don't want to drop the loop, the solution becomes more complicated then expected.

First, I make sure that nodes are drawn with borders only for the first two rows, hence the [cell/.style] option and [row 1/.style]-[row 3/.style].

Second, following an answer in Tikz foreach inside matrix I collect the numbers from 0 to 16 into a macro \mymatrixcontents and use it as the third matrix row. The \catcode command is necessary because without it PGF issues an error (alternatively, you might replace & by, say, \& and use option ampersand replacement=\&).

The code:

\documentclass{article}
\usepackage[landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}[cell/.style={align=center,inner sep=0pt,text width=1cm,minimum size=1cm}]
  \let\mymatrixcontent\empty
  \catcode`\&=\active
  \foreach \x in {0,...,16} {%
    \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\x &}%
  }
  \gappto\mymatrixcontent{\\}
  \tikzstyle{row 3}=[gray,draw=white]
  \matrix[matrix of math nodes, anchor=south west,
          row 1/.style={nodes={cell,draw=red}},
          row 2/.style={nodes={cell,draw=red}},
          row 3/.style={nodes={cell,gray}}
          ]{
      A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
      0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
      \mymatrixcontent
    };
\end{tikzpicture}
\end{document}

The output:

enter image description here

3

without matrix, shorter, simpler ?

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 0pt,
  start chain = going right,
   box/.style = {draw=red, minimum size=10mm, outer sep=0pt}
                        ]
\foreach \i/\j [count =\k from 0] in {
        A/0, B/1, D/2, B/1, A/0, C/1, E/2, G/3,
        E/2, H/3, E/2, C/1, F/2, I/3, F/2, C/1,
        A/0}
{
\node (n\k) [box,on chain] {\i};
\node [box, below=of n\k, label={[text=gray]below:\k}] {\j};
}
    \end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517