# Can't use macro in square brackets

I've made a matrix in a beamer presentation and I'm trying to style the nodes.

\newcommand\foo{row 1/.style={inuse}}
\begin{frame}{Chunks}
\tikzset{
inuse/.style={text=red},
free/.style={text=green},
addr/.style={minimum width=3em},
data/.style={nodes=draw,minimum width=6em},
}
\scalebox{0.6}{
\begin{tikzpicture}[node distance=2mm]
\matrix(magic) [matrix of nodes,ampersand replacement=\&,row sep=-\pgflinewidth,
column 1/.style={addr},
column 2/.style={data, column sep=1em},
%row 1 column 2/.style={inuse},
%\foo ,
%\foreach \r in { 1,2,3 } {row \r column 2/.style={inuse}, }
column 3/.style={addr},
column 4/.style={free, data, column sep=1em},
]
{
100: \& 00000010 \& 110: \& 12345678 \& 120: \& 12345678 \& 130: \& 12345678    \\
101: \& 00000001 \& 111: \& 12345678 \& 121: \& 12345678 \& 131: \& 12345678    \\
102: \& 00000002 \& 112: \& 12345678 \& 122: \& 12345678 \& 132: \& 12345678    \\
103: \& 00000003 \& 113: \& 12345678 \& 123: \& 12345678 \& 133: \& 12345678    \\
};
\end{tikzpicture}
}
\end{frame}


Of the three commented out lines, the first works and the other two fail. For the \foo macro I get:

Package pgfkeys Error: I do not know the key '/tikz/row 1/.style={inuse}' and
I am going to ignore it. Perhaps you misspelled it.


But if I stick the macro body where I tried to use it, no problem. Clearly I'm missing something fundamental about macros, but what?

Invoking \foo in the tikzset gave the same result.

The foreach gives me something totally cryptic:

! Undefined control sequence.
\foreach ...reach \let \pgffor@assign@before@code
=\pgfutil@empty \let \pgff...


Any help much appreciated.

• Could you please provide a working MWE, including the preamble you're using? – JMP Mar 22 '16 at 19:10
• Putting braces around the macro usually works: {\foo} – John Kormylo Mar 22 '16 at 19:43

## 1 Answer

I don't think you can use macros in that sense, but you can just define a new style instead, i.e.

\tikzset{foo/.style={row #1/.style={inuse}}}


With that definition, add foo=1 to add the inuse style to row 1, foo=2 for row 2, etc.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{foo/.style={row #1/.style={inuse}}}
\begin{document}
\begin{frame}{Chunks}
\tikzset{
inuse/.style={text=red},
free/.style={text=green},
addr/.style={minimum width=3em},
data/.style={nodes=draw,minimum width=6em},
}
\scalebox{0.6}{
\begin{tikzpicture}[node distance=2mm]
\matrix(magic) [matrix of nodes,ampersand replacement=\&,row sep=-\pgflinewidth,
column 1/.style={addr},
column 2/.style={data, column sep=1em},
foo=1,
column 3/.style={addr},
column 4/.style={free, data, column sep=1em},
]
{
100: \& 00000010 \& 110: \& 12345678 \& 120: \& 12345678 \& 130: \& 12345678    \\
101: \& 00000001 \& 111: \& 12345678 \& 121: \& 12345678 \& 131: \& 12345678    \\
102: \& 00000002 \& 112: \& 12345678 \& 122: \& 12345678 \& 132: \& 12345678    \\
103: \& 00000003 \& 113: \& 12345678 \& 123: \& 12345678 \& 133: \& 12345678    \\
};
\end{tikzpicture}
}
\end{frame}
\end{document}


• Thanks. I'll try that tomorrow. But I'm more worried about my overly simplistic understanding of macros than about getting this matrix to work. Its not just in square brackets - it failed in the tikzset too, and the foreach behaved equally mysteriously. What is a macro if not just a text substitution, and why did it fail? – Adrian May Mar 22 '16 at 22:29
• @AdrianMay A macro has to be expanded to its definition, and I guess the expansion of \foo isn't done (properly) in this context. But I'm afraid I've never delved into the depths of either expansion and argument parsing, so I cannot really give an answer. There are however several people on the site who could I think, so you may well get a better answer later. – Torbjørn T. Mar 22 '16 at 23:32