For whatever reason, the \draw
command doesn't like the listofitem
calculations inside of its arguments. The alternative here is to save the desired tokens in a token list and then out put the token list clean, after all the calculations are done.
I introduce two macros, \addpathtoks{}
to add literal tokens to the path, and \xaddpathtoks
, to expand (once) the first token in argument and add the result to the path. The expand part is needed to turn the \x
into an actual number.
UPDATED ANSWER to updated question
Note the black triangle is done via \band
macro, the dashed yellow (overlaying) triangle is done manually.
\documentclass[]{scrartcl}
\usepackage{tikz}
\usepackage{listofitems}
\newtoks\pathtoks
\newcommand\addpathtoks[1]{%
\pathtoks\expandafter{\the\pathtoks#1}}
\newcommand\xaddpathtoks[1]{%
\expandafter\addpathtoks\expandafter{#1}}
\newcommand{\band}[3]{
\readlist*\mylist{#3}
\pathtoks{}
\foreachitem\x\in\mylist[]{%
\ifnum\xcnt=1\relax \addpathtoks{\draw#1}\else
\addpathtoks{--}
\fi
\addpathtoks{(360/}\addpathtoks{#2*}%
\xaddpathtoks{\x:}\xaddpathtoks{\x)}
\ifnum\xcnt=\listlen\mylist[]\relax\addpathtoks{--cycle;}\fi
}
\the\pathtoks
}
\begin{document}
\begin{tikzpicture}
\band{[black,thick]}{3}{1,2,3}
\draw[yellow,thin,dashed] (360/3:1) -- (360/3*2:2) -- (360/3*3:3) --cycle;
\end{tikzpicture}
\end{document}
See SUPPLEMENT for possible improvement.
ORIGINAL ANSWER to original question
\documentclass[]{scrartcl}
\usepackage{tikz}
\usepackage{listofitems}
\newtoks\pathtoks
\newcommand\addpathtoks[1]{%
\pathtoks\expandafter{\the\pathtoks#1}}
\newcommand\xaddpathtoks[1]{%
\expandafter\addpathtoks\expandafter{#1}}
\begin{document}
\readlist*\mylist{-10,10}
\begin{tikzpicture}
\draw[black,thick] (0,0)--(0,-10)--(0,10)--cycle;
\pathtoks{}
\foreachitem\x\in\mylist[]{%
\ifnum\xcnt=1\relax \addpathtoks{\draw[black,thick] (1,0)}\fi
\addpathtoks{--(1,}\xaddpathtoks{\x)}%
\ifnum\xcnt=\listlen\mylist[]\relax\addpathtoks{--cycle;}\fi
}
\the\pathtoks
\end{tikzpicture}
\end{document}
SUPPLEMENT
If I understand what is actually being done here, one may be able to get away with two, rather than three, arguments to \band
, if it is always the case that the original #2
will always be the list length of #3
.
\documentclass[]{scrartcl}
\usepackage{tikz}
\usepackage{listofitems}
\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*\mylist{#2}
\pathtoks{}
\foreachitem\x\in\mylist[]{%
\ifnum\xcnt=1\relax \addpathtoks{\draw#1}\else
\addpathtoks{--}
\fi
\addpathtoks{(360/}\xxaddpathtoks{\listlen\mylist[]*}%
\xaddpathtoks{\x:}\xaddpathtoks{\x)}
\ifnum\xcnt=\listlen\mylist[]\relax\addpathtoks{--cycle;}\fi
}
\the\pathtoks
}
\begin{document}
\begin{tikzpicture}
\band{[black,thick]}{1,2,3}
\draw[yellow,thin,dashed] (360/3:1) -- (360/3*2:2) -- (360/3*3:3) --cycle;
\end{tikzpicture}
\end{document}
foreach
here? (This is not a criticism, really just a question. ;-)\draw[black,thick] (0,0) foreach \X in {-10,10} { -- (0,\X)}--cycle;
or\draw[black,thick] plot[samples at={0,-10,10}] (0,\x) --cycle;
do what I think you want to do.listofitems
package.foreach
and was able to get it working based on your suggestion. It's even cleaner now than usinglistofitems
so that's great!