Using \foreach inside a draw command using TIKZ
This is a slightly different approach (I admit this is a workaround), however, it works for this scenario just fine.
\foreach [count=\y] \x in {1,1,2,1,2,1,4,3,1.5}
{
\path [tangent=\y/10] (0,0) arc (180:0:10);
\draw [red, thick, ->, use tangent=1] (0,0) -- (0,-1*\x);
}
\draw (0,0) arc (180:0:10);
Just replace the tikzpicture
body by this code.
Final code:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
tangent/.style={
decoration={
markings,% switch on markings
mark=
at position #1
with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
]
\foreach [count=\y] \x in {1,1,2,1,2,1,4,3,1.5}
{
\path [tangent=\y/10] (0,0) arc (180:0:10);
\draw [red, thick,->, use tangent=1] (0,0) -- (0,-1*\x);
}
\draw (0,0) arc (180:0:10);
\end{tikzpicture}
\end{document}
When you need to call the same key repeatedly, you can use <key>/.list={<foreach expression>}
, where <foreach expression>
can be a simple list, or a list with ellipses. So in this case, you can use \draw [tangent/.list={0.1,0.2,...,0.9}](0,0) arc (180:0:10);
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
tangent/.style={
decoration={
markings,% switch on markings
mark=
at position #1
with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
]
\draw [tangent/.list={0.1,0.2,...,0.9}](0,0) arc (180:0:10);
\foreach \x [count=\xi] in {1,1,2,1,2,1,4,3,1.5}
{
\draw [red, thick,->, use tangent=\xi] (0,0) -- (0,-1*\x);
}
\end{tikzpicture}
\end{document}
This can be done easily with foreach
. I use simpler approach than yours. The result is
\documentclass[border={10}]{standalone}
\usepackage{tikz}
\begin{document}
\def \r {8}
\begin{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\draw[very thick] (\r,0) arc (0:180:\r);
\foreach \angle / \y in {18/6, 36/7, 54/6, 72/7,90/6, 108/7,126/6,144/7,162/6}
\draw [<-, very thick,red]
( { (\y)*cos(\angle)}, { (\y)*sin(\angle)} ) --
( { (\r)*cos(\angle)}, { (\r)*sin(\angle)} );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{tikzpicture}
\end{document}