4

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

2
 \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

  • 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. tex.stackexchange.com/questions/88344/… . For \number just use \the\numexpr as you would do with counters. It just fixes a technicality. – percusse Dec 15 '17 at 16:33
4

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

4

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

  • Great idea! Very useful. – CarLaTeX Dec 18 '17 at 8:59
  • @CarLaTeX. Thank you, but I had some external help ;-) – Ignasi Dec 18 '17 at 9:41
  • (+1) This one must be the accepted one! – Cragfelt Dec 18 '17 at 15:56
3

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}
  • Thank for your time! PERCUSSE solve the problem in one line! – Andrea Leo Dec 15 '17 at 16:08
2

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

  • TThank for your time! PERCUSSE solve the problem in one line! – Andrea Leo Dec 15 '17 at 16:08
  • You should add row sep=-\pgflinewidth, column sep=-\pgflinewidth to avoid thick lines between the cells. – CarLaTeX Dec 15 '17 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.