# Drawing lines using \foreach

I'd like to get a picture similar to that uploaded below. Basically, I'm trying to add perpendicular lines to every ray on the positive orthant. This is my attempt, could you tell me what is wrong in the code? The problem is, obviously, with the perpendicular lines, where I get the following error: !Package pgf error: No shape named 1 is known. Thank you in advance, because you are really helping me a lot.

    \documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]

\draw[thick,->] (0,0) -- (4.5,0) node[anchor=north west] {\small Security 1};
\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east] {\small Security 2};

\coordinate (A) at (0,0);
\coordinate (B) at (2.5,3);

\foreach \x [count=\xi] in {15,45,75} {
\draw[->](0,0)--(\x:3.5);
\node at (\x:3.9){$x_{.\xi}$};
\draw ($(\xi)!3cm!270:(A)$) -- ($(\xi)!4cm!90:(A)$);
}

\end{tikzpicture}
\end{document}


Also, how can I tell the code to hide the line perpendicular to the second ray?

• To reference a node, you need to give it a name: \node (\xi) at..... (Do not include thank you in your post - it is always implied - not necessary) – hpekristiansen May 3 at 17:27

## 1 Answer

Thanks to Marmot, he accomplishes a number of improvements, leaving the syntax closer to what the OP had proposed, and achieving conditionals by way of sign, etc.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]

\draw[thick,->] (0,0) -- (4.5,0) node[anchor=north west] {\small Security 1};
\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east] {\small Security 2};

\coordinate (A) at (0,0);
\coordinate (B) at (2.5,3);

\foreach \x [count=\xi] in {15,45,75} {
\draw[->](0,0)--(\x:3.5) coordinate (\xi);
\node at (\x:3.9){$x_{.\xi}$};
\draw (A) -- ($(A)!{sign(45-\x)*3.5cm}!90:(\xi)$);
}
\end{tikzpicture}

\end{document}


ORIGINAL ANSWER:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]

\draw[thick,->] (0,0) -- (4.5,0) node[anchor=north west] {\small Security 1};
\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east] {\small Security 2};

\coordinate (A) at (0,0);
\coordinate (B) at (2.5,3);

\foreach \x [count=\xi] in {15,45,75} {
\draw[->](0,0)--(\x:3.5);
\node at (\x:3.9){$x_{.\xi}$};
\ifnum\x<45\relax\draw[-](0,0)--(\the\numexpr\x+90\relax:3.5);\fi
\ifnum\x>45\relax\draw[-](0,0)--(\the\numexpr\x-90\relax:3.5);\fi
}
\end{tikzpicture}

\end{document}

• Thank you, @Steven! What was wrong in my code? – Nenne May 3 at 17:25
• @Nenne I am not a tikz expert, and did not recognize the syntax \draw ($(\xi)!3cm!270:(A)$) -- ($(\xi)!4cm!90:(A)$); But I also knew you would require conditionals in order to draw the perpendicular rightward, leftward, or not at all. – Steven B. Segletes May 3 at 17:27
• @marmot I thank you for helping me, though I don't think that is the result the OP wants, since it: 1) draws perpendiculars to the 45deg line, and 2) extends the perpendicular both rightward and leftward from the origin. – Steven B. Segletes May 3 at 18:19
• OK, next step: use \foreach \x [count=\xi] in {15,45,75} { \draw[<-](\x:3.5) coordinate[label=\x:$x_{.\xi}$] (\xi) -- (A) -- ($(A)!{sign(45-\x)*3.5cm}!90:(\xi)$); }, which is shorter ;-) – marmot May 3 at 18:53
• @Nenne \draw (-15:1) arc (-15:105:1); but it doesn't hurt to ask new questions. – marmot May 4 at 15:44