10

I'm brand new to using \foreach but not coding in general. Following the code in answer number 40 of this example: How to draw tangent line of an arbitrary point on a path in TikZ

How can I properly condense the tangent=0.x, list in the draw statement?

I've tried a few ideas with no success:

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

to get a result like this:

resulting figure

How can I properly condense the tangent=0.x, list in the draw statement?

  • You mean like this? – Alenanno Feb 19 '16 at 19:37
  • Thanks everyone for your input. Interesting alternate approaches. – user3780514 Feb 20 '16 at 15:13
5

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}
  • What about if I have two arguments in my style and I want both to run through a list? Specifically, my style reads loop/.style={decoration={markings,mark=at position {#1} with {\arrow{>},\node[anchor=\pgfdecoratedangle-90,rotate=-\pgfdecoratedangle] {#2};}},postaction={decorate}} and I'd like to give pairwise values to #1 and #2. – Casimir Sep 27 '17 at 10:54
6

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

This can be done easily with foreach. I use simpler approach than yours. The result is

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.