2

From the very first example of a matrix presented in the "Matrix Library" section 57.1 of the TikZ & PGF manual, I expected the following to result in an arrow connecting the "Signatures" cell to the "Transition" cell in the picture.

    \documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,positioning,arrows}
\begin{document}
\begin{tikzpicture}[every node/.style={draw, anchor=west}] % added anchor=west
\matrix [matrix of nodes,draw=red,column sep=1cm,name=m]
  {
   \node [xshift=1mm] {\textbf {Transition Record}};\\ % added xshift=1mm
   \node [align=left]
      {\textbf{Signatures} \\
       signed by Ted};\\
   \node [align=left] {\textbf{Transition} \\ \(\tau\)};\\
   \node [align=left]
      {\textbf{End State} \\
       \(\mathtt{Alice2}\mid\mathtt{Carol2}\mid\mathtt{Ted}\)};\\
};
\draw[-latex,bend right] (m-2-1.north west) to (m-4-1.south west);
\end{tikzpicture}
\end{document}

This results in the errors:

  • Package pgf Error: No shape named m-4-1 is known. ...] (m-2-1.north west) to (m-4-1.south west)
  • Package pgf Error: No shape named m-2-1 is known. ...draw[-latex,bend right] (m-2-1.north west)

On the other hand, if I replace the \draw command with

\draw[-latex,bend right] (m.north west) to (m.south west);

I get exactly the arrow I would expect.

Given that the name "m" is known, it is not clear to me why derived names, "m-1-1", "m-2-1", etc. would not be. This is especially the case given the above cited example from the manual. Apart from getting some insight as to why I am unable to reference the "unknown" shape names, I would like to understand the rules regarding the introduction of names and the scopes within which they are available to reference.

Thank you in advance.

  • This is because you are mixing matrix of nodes with matrix. In your case, a plain matrix would be more appropriate, or to keep matrix of nodes but then remove the \node commands from the matrix (and deal with the line breaks and styles in a different way). – user121799 Aug 21 '18 at 18:29
  • It appears that when you use an explicit \node command, the derived names aren't generated from that point onward. (Seems like a bug to me.) You can, however, name the node in the matrix yourself. – Derek Aug 21 '18 at 18:40
  • Another comment: I just realized that you got your code from Torbjørn's nice answer. It is always better to say where the code is coming from. In most cases this will lead to a better answer. And I think you should accept Torbjørn's nice answer. You can accept an answer by clicking on the checkmark left of it. – user121799 Aug 21 '18 at 18:41
  • @Derek I would not call this a bug. A node can only have one name. If you call \node somewhere, you expect to be able to give it a name. Of course, it would be nice if these nodes had automatically an alias of the type you imply, but even that is not really doable because you could put two nodes into that cell. – user121799 Aug 21 '18 at 18:52
  • 1
    @marmot I agree, it's not a bug. The automatic naming is superseded by the explicit \node command. – Derek Aug 21 '18 at 19:05
3

There are two types of matrices floating around in TikZ: matrix of nodes and matrix. You are mixing those. If you use matrix of nodes, you do not have to put the \node commands, and the nodes get automatically labeled. If you do a plain matrix, you can keep your node commands but you need to name the nodes yourself. I use both types of matrices in the following code.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,positioning,arrows}
\begin{document}
\begin{tikzpicture}[every node/.style={draw, anchor=west}] % added anchor=west
\matrix [matrix,draw=red,column sep=1cm,name=m]
  {
   \node [xshift=1mm] {\textbf {Transition Record}};\\ % added xshift=1mm
   \node [align=left] (m-2-1)
      {\textbf{Signatures} \\
       signed by Ted};\\
   \node [align=left] {\textbf{Transition} \\ \(\tau\)};\\
   \node [align=left] (m-4-1)
      {\textbf{End State} \\
       \(\mathtt{Alice2}\mid\mathtt{Carol2}\mid\mathtt{Ted}\)};\\
};
\draw[-latex,bend right] (m-2-1.north west) to (m-4-1.south west);
\end{tikzpicture}

\begin{tikzpicture}[every node/.style={draw, anchor=west}] % added anchor=west
\matrix [matrix of nodes,draw=red,column sep=1cm,name=m,nodes={align=left,text
width=4cm}]
  {
   |[xshift=1mm]| \textbf {Transition Record}\\ % added 
   {\textbf{Signatures} \\
       signed by Ted}\\
   {\textbf{Transition} \\ \(\tau\)}\\
   {\textbf{End State} \\
       \(\mathtt{Alice2}\mid\mathtt{Carol2}\mid\mathtt{Ted}\)}\\
};
\draw[-latex,bend right] (m-2-1.north west) to (m-4-1.south west);
\end{tikzpicture}
\end{document}

enter image description here

  • There is nothing wrong with using \node in matrix of nodes — see section 57.1.3 in the manual. – Derek Aug 21 '18 at 18:42
  • @Derek Did you read "Conceptually, this key adds \node{ at the beginning and }; at the end of each cell and sets the anchor of the node to base." ? I agree that "there is nothing wrong", though. But I was not claiming it is wrong, just saying that " If you use matrix of nodes, you do not have to put the \node commands,", which is different from "... must not put ...", but I agree " ... do not need to ... " would have been clearer. – user121799 Aug 21 '18 at 18:49
0

edit: sorry i had some problems with uploading correct version of code ... now should be ok

a slightly modified second example in the nice marmot answer. by use of the stackengine package it gives the similar result as his first example:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, matrix}
\usepackage{stackengine}

\begin{document}
    \begin{tikzpicture}                         % <---
\matrix (m) [matrix of nodes, draw=red,
         nodes={draw, anchor=west},             % <---
         row sep=-\pgflinewidth]                % <---
  {
   |[xshift=1mm]| \textbf {Transition Record}\\ 
   \shortstack[l]{\textbf{Signatures} \\          % <---
       signed by Ted}\\
   \shortstack[l]{\textbf{Transition} \\          % <---
       \(\tau\)}\\
   \shortstack[l]{\textbf{End State} \\           % <---
       \(\mathtt{Alice2}\mid\mathtt{Carol2}\mid\mathtt{Ted}\)}\\
};
\draw[-latex] (m-2-1.north west) to [bend right] (m-4-1.south west);
    \end{tikzpicture}
\end{document}

enter image description here

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.