不能在方括號中使用宏

不能在方括號中使用宏

我在投影機簡報中製作了一個矩陣,並嘗試設定節點的樣式。

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

在三行註解掉的行中,第一行有效,另外兩行失敗。對於 \foo 巨集我得到:

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.

但如果我將巨體貼在我嘗試使用它的地方,那就沒問題了。顯然我錯過了一些關於巨集的基本知識,但是什麼呢?

在 tikzset 中呼叫 \foo 會得到相同的結果。

foreach 給了我一些完全神秘的東西:

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

非常感謝任何幫助。

答案1

我不認為你可以在這個意義上使用宏,但你可以定義一個新的樣式,即

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

使用該定義,新增將樣式foo=1新增inuse至第 1 行、foo=2第 2 行等。

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

在此輸入影像描述

相關內容