Verwenden von \foreach innerhalb eines Zeichenbefehls mit TIKZ

Verwenden von \foreach innerhalb eines Zeichenbefehls mit TIKZ

Ich bin ganz neu in der Verwendung, \foreachaber nicht im Codieren im Allgemeinen. Folgen Sie dem Code in Antwort Nummer 40 dieses Beispiels:So zeichnen Sie in TikZ eine Tangente eines beliebigen Punkts auf einem Pfad

tangent=0.xWie kann ich die Liste in der Draw-Anweisung richtig komprimieren ?

Ich habe ein paar Ideen ohne Erfolg ausprobiert:

\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}

um ein Ergebnis wie dieses zu erhalten:

resultierende Zahl

Wie kann ich die Liste „tangent=0.x“ in der Draw-Anweisung richtig verdichten?

Antwort1

Wenn Sie dieselbe Taste wiederholt aufrufen müssen, können Sie verwenden <key>/.list={<foreach expression>}, wobei <foreach expression>es sich um eine einfache Liste oder eine Liste mit Auslassungspunkten handeln kann. In diesem Fall können Sie also verwenden\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}

Antwort2

Dies ist ein etwas anderer Ansatz (ich gebe zu, dass es sich um eine Problemumgehung handelt), er funktioniert in diesem Szenario jedoch einwandfrei.

\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);

Ersetzen Sie einfach den tikzpictureTextkörper durch diesen Code.


Endgültiger 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}

Antwort3

Dies kann einfach mit durchgeführt werden foreach. Ich verwende einen einfacheren Ansatz als Ihren. Das Ergebnis ist

Bildbeschreibung hier eingeben

\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}

verwandte Informationen