3

I need to create a row of circles (colored markers) followed by some text. See the image below.

enter image description here

I achieve this using the following code.

\begin{tikzpicture}[scale=2]
\matrix[nodes={minimum width=0.05cm, minimum height=0.05cm},
 row sep=0.001cm, column sep=0.1cm] {
 \node[circle, fill=red ] {}; & 
 \node[circle, fill=blue ] {}; & 
 \node[circle, fill=green ] {}; & 
 \node[circle, fill=orange ] {}; & 
 \node[circle, fill=pink ] {}; & 
 \node[draw=none, fill=none, text width=4cm]{Some Text};\\}; 
\end{tikzpicture}

Since I need to produce this output repetitively, I thought of defining a new command with the code above. Here's the command I defined.

\newcommand{\MarkersAndText}{6}{
\begin{tikzpicture}[scale=2]
    \matrix[nodes={minimum width=0.05cm, minimum height=0.05cm},
        row sep=0.001cm, column sep=0.1cm] {
    \node[circle, fill=#1] {}; &  
    \node[circle, fill=#2] {}; &  
    \node[circle, fill=#3] {}; & 
    \node[circle, fill=#4] {}; & 
    \node[circle, fill=#5] {}; & 
    \node[draw=none, fill=none, text width=4cm]{#6};\\}; 
\end{tikzpicture}

However, I get the following error,

Illegal parameter number in definition of \tikz@temp. <to be read again> 1 l.19 \node[circle, fill=#1] {}; & 

Can anyone help me to understand how I can resolve this problem?

Edit : David Carlisle's answer solves the error above. The compilation produces another error when I use the command \MarkersAndText. The new error is,

Package pgf Error: Single ampersand used with wrong catcode

The error is fixed by changing the code block as,

\matrix[nodes={minimum width=0.05cm, minimum height=0.05cm}, row sep=0.001cm, column sep=0.1cm, ampersand replacement=\&] { \node[circle, fill=#1] {}; \&

I am curious as to why the code without ampersand replacement works when outside \newcommand.

rs_
  • 175
  • I hope this question helps your curiosity: http://tex.stackexchange.com/questions/1111/problem-with-defining-shortcuts-for-tikz-matrices/1114#1114 – Ignasi Sep 03 '15 at 13:42
  • Thank you for the link. I have a follow up question with the above setup but for a slightly different problem should I edit the question here? or ask a new question? – rs_ Sep 03 '15 at 17:56
  • I think it is better to make a new question with a link to this one. If you edit this one, David's answer won't fit it. – Ignasi Sep 04 '15 at 10:31
  • Igansi's comment above is on point. I would like to know how one can pass a color as an argument to tkzstyle. I don't understand the solution posted here. – Cyriac Antony Feb 17 '22 at 08:00

1 Answers1

7
\newcommand{\MarkersAndText}{6}

should be

\newcommand{\MarkersAndText}[6]

As it was you were defining \MarkersAndText to be 6

David Carlisle
  • 757,742