Graphics, Figures & TablesUsing matrix of nodes with Tikz

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Basso
Posts: 3
Joined: Thu Jul 20, 2023 6:05 pm

Using matrix of nodes with Tikz

Post by Basso »

I need to use the matrix command to create tikz diagrams like

Code: Select all

\begin{tikzpicture}[-stealth',looseness=.5,auto]
	\matrix [matrix of math nodes, column sep={1.5cm,between origins}, row sep={1.5cm,between origins}]
	{|(a)| A& |(b)| B& |(c)| C \\[12pt]
		&|(x)| \alpha & \\
	};
\end{tikzpicture}
But I'd like to be able to create matrix arguments in a macro, for example:

Code: Select all

\def\contenumatrice{|(a)| A& |(b)| B& |(c)| C \\[12pt] &|(x)| \alpha& \\}
then giving the contents of this macro as an argument :

Code: Select all

\begin{tikzpicture}[-stealth',looseness=.5,auto]
	\matrix[matrix of math nodes, column sep={1.5cm,between origins}, row sep={1.5cm,between origins}]
	{\contenumatrice};
\end{tikzpicture}
The compilation produces the error "Undefined control sequence \contenumatrice". This seems to mean that tikz takes \contenumatrice as a matrix argument without searching or developing it.
I then tried to force the development of \contenumatrice with \expandafter:

Code: Select all

\begin{tikzpicture}[-stealth',looseness=.5,auto]\expandafter\matrix\expandafter[matrix of math nodes, column sep={1.5cm,between origins}, row sep={1.5cm,between origins}]
	\expandafter{\contenumatrice};
\end{tikzpicture}
The compilation produces another error: "A node must have a (possibly empty) label text. \expandafter" which I haven't found explained in the pgftikz manual.
Clearly, tikz does not accept the parameterization of a matrix command using a macro. Is it possible to force tikz to use a macro's development of a macro as an argument to the matrix?
Last edited by Stefan Kottwitz on Sun Jul 23, 2023 7:06 pm, edited 1 time in total.
Reason: code marked

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Using matrix of nodes with Tikz

Post by kaiserkarl13 »

Using "&" inside a command causes problems in this case. I think you're also going to have trouble with "\\" inside a \def: you cannot have paragraphs or newlines inside \def unless it's preceded by \long (or even better, use \newcommand).
Basso
Posts: 3
Joined: Thu Jul 20, 2023 6:05 pm

Using matrix of nodes with Tikz

Post by Basso »

Thanks kaiserkarl. But unfortunately this does not change the outcome. I get always the same error messages
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Using matrix of nodes with Tikz

Post by rais »

Basso wrote: The compilation produces the error "Undefined control sequence \contenumatrice".
Assuming you have not misspelled it in either definition or usage, you may have used the definition within a TeX group, after which the definition is void...
The real message should have been

Code: Select all

! Package pgf Error: Single ampersand used with wrong catcode.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.16 {\contenumatrice
                     };
because the ampersand (&) is made an active character---and using & within the macro's definition prevents it from becoming active.
This should work:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, arrows}
\def\contenumatrice{|(a)| A\pgfmatrixnextcell |(b)| B\pgfmatrixnextcell |(c)| C \\[12pt] \pgfmatrixnextcell |(x)| \alpha\pgfmatrixnextcell \\}

\begin{document}
\begin{tikzpicture}[-stealth',looseness=.5,auto]
\matrix[matrix of math nodes, column sep={1.5cm,between origins}, row sep={1.5cm,between origins}]
{\contenumatrice};
\end{tikzpicture}
\end{document}
or try your hand at the key `/tikz/ampersand replacement' if you don't want to use \pgfmatrixnextcell for every `&' in your macro.

@kaiserkarl13: if I understood this right, a \long would be required, if the argument of the defined macro should be able to deal with \par or the like.
Say, after \def\foo{a\par b}, \foo contains a valid definition, but after \def\foo#1{#1}, a call like \foo{a\par b} should fail with

Code: Select all

Runaway argument?
{a
! Paragraph ended before \foo was complete.
KR
Rainer
Basso
Posts: 3
Joined: Thu Jul 20, 2023 6:05 pm

Using matrix of nodes with Tikz

Post by Basso »

Thanks! It works with \pgfmatrixnextcell
Post Reply