我是新手,\foreach
但对编程不是很熟悉。按照此示例答案 40 中的代码操作:如何在 TikZ 中绘制路径上任意点的切线
我怎样才能正确地压缩tangent=0.x
draw 语句中的 , 列表?
我尝试过一些想法,但没有成功:
\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=0.1,
tangent=0.2,
tangent=0.3,
tangent=0.4,
tangent=0.5,
tangent=0.6,
tangent=0.7,
tangent=0.8,
tangent=0.9,
%%%% attempt 1
%tangent={0.05,0.01,...0.95}
%%%%% attempt 2
%\foreach \y in {0.1,0.2,...,0.9}
%{
%tangent=\y,
%}
] (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}
得到这样的结果:
我怎样才能正确地压缩绘制语句中的切线=0.x列表?
答案1
当您需要重复调用同一个键时,可以使用<key>/.list={<foreach expression>}
,其中<foreach expression>
可以是简单列表,也可以是带省略号的列表。因此,在这种情况下,您可以使用\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}
答案2
这是一种略有不同的方法(我承认这是一种变通方法),但它非常适合这种情况。
\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);
tikzpicture
只需用此代码替换主体即可。
最终代码:
\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}
答案3
这可以通过 轻松完成foreach
。我使用的方法比你的更简单。结果是
\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}