5

I have the following code

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}       
\pgfplotsset{compat=1.10,   
             ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds, 
                calc,
                patterns}

\begin{document}

\begin{tikzpicture}[scale=4, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic[fill=green] {myc} (1,3) pic[fill=blue] {myc} (2,6) pic[fill=blue] {myc};
\end{tikzpicture}

\end{document}

The code defines the outline of a tile in the line starting with tikzset{

I want to put this tile at various places and I want to use different colors to fill the tile. The above code is one of the attempts to get this done, which however does not work.

The output I get is the following:

enter image description here

I very recently learnt about the tikzset command (thanks to TeXstackexchange) and have no understanding of it. Can somebody please help me here and also, if possible, suggest a reference.

5

Just use pic actions.

\documentclass[tikz,border=3.14mm]{standalone}

\begin{document}

\begin{tikzpicture}[scale=4, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm,pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic[fill=green] {myc} (1,3) pic[fill=blue] {myc} (2,6) pic[fill=blue] {myc};
\end{tikzpicture}

\end{document}

enter image description here

  • 2
    @caffeinemachine I personally prefer pic actions over an argument in the pic in this case because you can then add dashed and/or draw=red and so on which you cannot do in the same simple way if you use arguments of the pics. – marmot May 20 at 12:54
4

Your pic can have arguments.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}       
\pgfplotsset{compat=1.10,   
             ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds, 
                calc,
                patterns}

\begin{document}

\begin{tikzpicture}[scale=4, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm,fill=#1] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic {myc=red} (1,3) pic {myc=green} (2,6) pic {myc=blue};
\end{tikzpicture}

\end{document}

enter image description here

  • Yeah I looked at the manual. But it's big. Can you tell me under what heading should I look? Thanks. – caffeinemachine May 20 at 12:51
  • 2
    @caffeinemachine Alright, it is in section 18, especially 18.3. It tells you how to define new pics. Reading about the key handler and the example on page 264, you will know that pic is, literally, a kind of style. So you read about .style key handler in section 87.4.4, page 966, and boom, you know how to add one, or two, or even nine arguments to a pic. – user156344 May 20 at 12:54

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.