Command not producing expected output

by me--   Last Updated June 13, 2019 04:23 AM

This is a follow-on question from here. Things are almost working but something strange is going on with my \band command. It renders the first band correctly, but then the second band causes things to go haywire.

\documentclass[]{scrartcl}

\usepackage{tikz}
\usepackage{listofitems}

\begin{document}

\newtoks\pathtoks
\newcommand\addpathtoks[1]{%
    \pathtoks\expandafter{\the\pathtoks#1}}
\newcommand\xaddpathtoks[1]{%
    \expandafter\addpathtoks\expandafter{#1}}
\newcommand\xxaddpathtoks[1]{%
    \expandafter\xaddpathtoks\expandafter{#1}}

\newcommand{\band}[2]{
    \readlist*\valuelist{#2}
    \pathtoks{}
    \foreachitem\x\in\valuelist[]{%
        \ifnum\xcnt=1\relax \addpathtoks{\draw[#1]}\else
            \addpathtoks{--}
        \fi
        \addpathtoks{(360/}\xxaddpathtoks{\listlen\valuelist[]*}%
        \xaddpathtoks{\x:}\xaddpathtoks{\x)}
        \ifnum\xcnt=\listlen\valuelist[]\relax\addpathtoks{--cycle;}\fi
    }

    \the\pathtoks
}

\begin{tikzpicture}
    % This is what I expect the below \band invocations to produce, and this renders correctly.
    \draw[red] (360/3:1) -- (360/3*2:2) -- (360/3*3:3) --cycle;
    \draw[blue] (360/3:3) -- (360/3*2:2) -- (360/3*3:1) --cycle;
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}
    % These bands, which should be equivalent to the above are not rendering correctly.
    \band{red}{1,2,3};
    \band{blue}{3,2,1};
\end{tikzpicture}

\end{document}

This renders like this:

enter image description here

The top picture is what I'd expect, but the bottom seems to re-render the same shape over the top of the existing one.

Weirder still, if two values are the same:

\band{red}{2,3,3};

It renders a straight line:

enter image description here

And if all three values are the same, it renders nothing at all! It's like it's de-duplicating the list...?

So my questions are:

  1. Why does my repro above render the same triangle twice instead of the expected triangles?
  2. Why do duplicate values cause weird things to happen?
Tags : tikz-pgf


Answers 1


TikZ has all the means built in to simplify this quite a bit. All these macros can be absorbed in a simple style

band/.style={insert path={plot[samples at={0,1,2}]
    ({360/3*(\x+1)}:{#1}[\x]) -- cycle}}

with which you only need to say

\draw[red,band={1,2,3}];

You could make this a macro, but this would make it less powerful. MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[band/.style={insert path={plot[samples at={0,1,2}]
    ({360/3*(\x+1)}:{#1}[\x]) -- cycle}},font=\sffamily]

  \begin{scope}[xshift=-5cm,local bounding box=by hand]
    \draw[red] (360/3:1) -- (360/3*2:2) -- (360/3*3:3) --cycle;
    \draw[blue] (360/3:3) -- (360/3*2:2) -- (360/3*3:1) --cycle;
  \end{scope}   
  \node[above=1ex] at (by hand.north){by hand};
  %
  \begin{scope}[local bounding box=automatic]
    \draw[red,band={1,2,3}];
    \draw[blue,band={3,2,1}];
  \end{scope}   
  \node[above=1ex] at (automatic.north){automatic};
\end{tikzpicture}
\end{document}

enter image description here

marmot
marmot
June 13, 2019 04:02 AM

Related Questions


Updated April 27, 2019 15:23 PM

Updated December 20, 2018 03:23 AM

Updated November 20, 2018 12:23 PM

Updated December 17, 2018 22:23 PM

Updated December 18, 2018 02:23 AM