4

I'm creating a step-wise schematic of a process of re-linking a doubly linked lists. My intuition is to use split rectangles for the nodes (representing pointers and value, respectively), and then a matrix to arrange these rectangles.

Since the figure in the end will have many repeated arrows (and some that differ between rows, but that's a later problem), I'd like to use a matrix of nodes and the M-i-j-syntax. But, with this I cannot use the nodes=somestyle option of the matrix, at least not if I want different text in the split rectangles, as discussed here. It seems as if I'm deemed to use the syntax |[stylename]| before each node, but I get a rather odd behavior from the following MWE: the rectangle won't appear, instead the style-specifier (or what to call it) shows! Annoyingly, it only happens in the first column on row 2 and 3, but nowhere else. See included picture.

One simple solution is to just add an empty node in the beginning of each row (e.g. by adding {} & before A1, A2 and A3), but this seemsa bit stupid, and I'd like to understand what is going on instead.

What goes wrong here? Is there a better approach?

enter image description here

\documentclass[crop,tikz]{standalone}
\usetikzlibrary{shapes.multipart, matrix}

\begin{document}

\begin{tikzpicture}[triple/.style={rectangle split, rectangle split parts=3,
    draw, rectangle split horizontal, minimum size=5mm}]

\matrix (M) [matrix of nodes, row sep=0.5cm, column sep=0.5cm] {    
  |[triple]| \nodepart{two} A1 &
  |[triple]| \nodepart{two} B1 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C1 &
  |[triple]| \nodepart{two} D1 &
  |[triple]| \nodepart{two} E1 \\
  
  |[triple]| \nodepart{two} A2 &
  |[triple]| \nodepart{two} B2 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C2 &
  |[triple]| \nodepart{two} D2 &
  |[triple]| \nodepart{two} E2 \\
  
  |[triple]| \nodepart{two} A3 &
  |[triple]| \nodepart{two} B3 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C3 &
  |[triple]| \nodepart{two} D3 &
  |[triple]| \nodepart{two} E3 \\
  };

  
\end{tikzpicture}
\end{document}
1
  • 2
    Welcome to tex.sx. Commented Jun 7, 2022 at 15:40

2 Answers 2

7

Just remove (or comment out) the blank lines in the \matrix :-)

Guessing what happens: the TikZ matrix code scanning for the special |...| syntax at the start of an entry sees \par (generated from the blank line) and stops here, concluding there is no |...| in the cell. Then | gets typeset as cell contents and becomes “—” in the output because your document uses OT1 font encoding (it is the default), and that's how it is in OT1 (slot "7C is for the em dash, cf. texdoc encguide for encguide.pdf, p. 19). With T1 encoding, you would get a “|” in the output instead (texdoc encguide p. 22).

BTW, why char "7C from the current font? Because | has catcode 12 (this is standard), and its TeX-internal code (~ ASCII) is "7C.

\documentclass[crop,tikz]{standalone}
\usetikzlibrary{shapes.multipart, matrix}

\begin{document}
\begin{tikzpicture}[triple/.style={rectangle split, rectangle split parts=3,
    draw, rectangle split horizontal, minimum size=5mm}]

\matrix (M) [matrix of nodes, row sep=0.5cm, column sep=0.5cm] {
  |[triple]| \nodepart{two} A1 &
  |[triple]| \nodepart{two} B1 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C1 &
  |[triple]| \nodepart{two} D1 &
  |[triple]| \nodepart{two} E1 \\
  % <----------------- Here
  |[triple]| \nodepart{two} A2 &
  |[triple]| \nodepart{two} B2 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C2 &
  |[triple]| \nodepart{two} D2 &
  |[triple]| \nodepart{two} E2 \\
  % <----------------- Here
  |[triple]| \nodepart{two} A3 &
  |[triple]| \nodepart{two} B3 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C3 &
  |[triple]| \nodepart{two} D3 &
  |[triple]| \nodepart{two} E3 \\
  };
\end{tikzpicture}
\end{document}

enter image description here

4

What @frougon guesses is right: when matrix of nodes is used, the \par token is not ignored and stops the detection for |.

In example below, \tikz@lib@matrix@start@cell is patched so when a \par token is encountered, it's skipped (but not ignored) and the detection for | continues:

\documentclass[crop,tikz]{standalone}
\usetikzlibrary{shapes.multipart, matrix}

\makeatletter
% originally defined in tikzlibrarymatrix.code.tex
\def\tikz@lib@matrix@start@cell{%
  % skip possible \par
  \pgfutil@ifnextchar\par
    {\par\expandafter\tikz@lib@matrix@start@@cell\pgfutil@gobble}
    {\tikz@lib@matrix@start@@cell}%
}
\def\tikz@lib@matrix@start@@cell{%
  \pgfutil@ifnextchar|%
    {\tikz@lib@matrix@with@options}{\tikz@lib@matrix@normal@start@cell}%
}
\makeatother

\begin{document}

\begin{tikzpicture}[every label/.style={align=center, font=\scriptsize\ttfamily}]
  \matrix[label={normal\\ no blank line}] {
    \node{a}; & \node{b}; \\
    \node{c}; & \node{d}; \\
  };

  \matrix[label={normal\\ blank line}] at (2,0) {
    \node{a}; & \node{b}; \\

    \node{c}; & \node{d}; \\
  };

  \matrix[matrix of nodes, label={matrix of nodes\\ no blank line}] at (4,0) {
   |[red]| a & b \\
   |[red]| c & d \\
  };

  \matrix[matrix of nodes, label={matrix of nodes\\ blank line}] at (6,0) {
   |[red]| a & b \\
   
   |[red]| c & d \\
  };
\end{tikzpicture}

\begin{tikzpicture}[triple/.style={rectangle split, rectangle split parts=3,
    draw, rectangle split horizontal, minimum size=5mm}]

\matrix (M) [matrix of nodes, row sep=0.5cm, column sep=0.5cm] {    
  |[triple]| \nodepart{two} A1 &
  |[triple]| \nodepart{two} B1 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C1 &
  |[triple]| \nodepart{two} D1 &
  |[triple]| \nodepart{two} E1 \\
  
  |[triple]| \nodepart{two} A2 &
  |[triple]| \nodepart{two} B2 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C2 &
  |[triple]| \nodepart{two} D2 &
  |[triple]| \nodepart{two} E2 \\
  
  |[triple]| \nodepart{two} A3 &
  |[triple]| \nodepart{two} B3 &
  {$\ldots$} &
  |[triple]| \nodepart{two} C3 &
  |[triple]| \nodepart{two} D3 &
  |[triple]| \nodepart{two} E3 \\
  };
  
\end{tikzpicture}
\end{document}

enter image description here enter image description here

You must log in to answer this question.

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