
我對使用是全新的,\foreach
但一般來說不是編碼。遵循本範例的答案 40 中的程式碼:如何在TikZ中繪製路徑上任意點的切線
如何正確壓縮tangent=0.x
繪製語句中的 , 列表?
我嘗試了一些想法但沒有成功:
\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}
得到這樣的結果:
如何正確壓縮繪圖語句中的 tangent=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}