TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
You mean like this? – Alenanno Feb 19 at 19:37
    
Thanks everyone for your input. Interesting alternate approaches. – user3780514 Feb 20 at 15:13
up vote 5 down vote accepted

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}
share|improve this answer

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}
share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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