# Arrow parallel to a line with TikZ

I want to add an arrow at $f_2$ pointing towards $Q$ in the following figure.

It should be parallel to the line, I hope this minimal example is enough.

  \documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,3) -- node[anchor=south west] {$f_2$} ($cm,0)-(5pt,5pt)$);
% Circle and the Q
\fill[black] ($(4cm,0) - (5pt,5pt)$) circle (5pt)
node (Q) [anchor=south,above=1pt] {$Q$};
\end{tikzpicture}
\end{document}


I searched for a solution but only found how to make many arrow with markings.

-
Could you add to your MWE how you drew the other parts of the picture (the circles, the Q node)? Should the arrow go all the way from f_2 to Q or stop after a fixed length? If the nodes f_2 and Q has similar properties regarding their size, the parallelism would be naturally given. –  Qrrbrbirlbel Mar 1 '13 at 14:55
the full code is here latex.informatik.uni-halle.de/latex-online/temp/… i have changed the MWE –  Dominic Michaelis Mar 1 '13 at 15:05

Depending on what you want (an arrow connecting $f_2$ and Q, but that it is parallel to another given line? Or simply an arrow parallel to a given line, and at a given distance?) the solution will be different.

My approach is for the second case. You have an arbitrary line (A)--(B) and you want to put above (or below) that line a segment parallel to the given line.

In orde to do so, I defined a style called parallel sement to be used as part of a to command, which makes use of three keys:

• segment distance The distance to the line (A) -- (B) (if it is negative, the segment will be below that line). By default it is 1mm.
• segment length The length (in any units) of the segment. If you don't give units, it will be a fraction of the half of the line (A)--(B) (so, if you set segment length=2 you would get a line of equal length than (A)--(B), but if you set segment length=2mm your segment will be exactly 2mm in length. By default it is 5ex.
• segment pos is the position of the center of the segment in relation with line (A)--(B). It is a fraction of the total length, so if you give segment pos=0.5 the segment will be centered in (A)--(B), while segment pos=1 will center it on (B). By default it is 0.5.

The following code shows the definition of this style and an example which uses it three times, with different values of the above keys. After the result I'll write a little explanation about how it works.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\usetikzlibrary{calc}
\tikzset{
parallel segment/.style={
segment distance/.store in=\segDistance,
segment pos/.store in=\segPos,
segment length/.store in=\segLength,
to path={
($(\tikztostart)!\segPos!(\tikztotarget)!\segLength/2!(\tikztostart)!\segDistance!90:(\tikztotarget)$) --
($(\tikztostart)!\segPos!(\tikztotarget)!\segLength/2!(\tikztotarget)!\segDistance!-90:(\tikztostart)$)  \tikztonodes
},
% Default values
segment pos=.5,
segment length=3ex,
segment distance=1mm,
},
}

\begin{tikzpicture}
\coordinate (A) at (0,3);
\coordinate (B) at (4,0);
\draw (A) -- node[anchor=south west] {$f_2$} (B);
\draw[->]      (A) to[parallel segment]      (B);
\draw[->,blue] (A) to[parallel segment,
segment pos=.2]      (B);
\draw[->, red] (A) to[parallel segment,
segment length=5ex,
segment distance=2mm,
segment pos=.8]         (B);
\end{tikzpicture}
\end{document}


Result:

# How it works

Let's dissect the style definition:

parallel segment/.style={
segment distance/.store in=\segDistance,
segment pos/.store in=\segPos,
segment length/.store in=\segLength,


This part simply extract the value of the already explained keys and store these values into macros, which will be used to build the segment.

   to path={
($(\tikztostart)!\segPos!(\tikztotarget)!\segLength/2!(\tikztostart)!\segDistance!90:(\tikztotarget)$) --
($(\tikztostart)!\segPos!(\tikztotarget)!\segLength/2!(\tikztotarget)!\segDistance!-90:(\tikztostart)$)  \tikztonodes
},


This part defines the shape of the path that will be drawn when the user writes (A) to[parallel segment] (B). Tikz has stored for us the coordinates of the starting point (A in the example) and ending point (B in the example) in the macros called \tikztostart and \tikztoend, but in order to make easier to type and read the explanation, I'll refer to those points as A and B.

So, how is the path built? As you can see it is of the form:

(some complex expression)  -- (some other complex expression) \tikztonodes


Leaving apart the last \tikztonodes which is required for reasons beyond my understanding, what remains is simply a straight line between two points, and the "complex expression" only computes those two points, which are then the starting and ending point of the segment to be drawn.

So let's examine the first "complex expression" (translating \tikztostart and \tikztotarget for A and B which are shorter and more understandable):

($(A)!\segPos!(B)!\segLength/2!(A)!\segDistance!90:(B)$)


This expression uses the interpolation operator !x! several times. Each time it applies to the resulting coordinate of the expression at its left, so let's read this from left to right:

• ($(A)!\segPos!(B) is a point in the line A--B, at a position given by \segPos. By default it is 0.5 so this represent the middle point of A--B. • !\segLength/2!(A) means: from the computed point (the middle of A--B) move the length \segLength/2 towards A. So, using the default values, this will leave us in a point which is at 2.5ex from the center of A--B in direction towards A. • !\segDistance!90:(B) means: and from that point, move the amount \segDistance in the direction perpendicular to thispoint--B (90 is the angle, hence pependicular). So, using the default values this will move us 1mm perpendicular to A--B. After all those interpolations, the point we get is the starting point of the desired segment. You can try to decipher and understand the syntax of the other end of the segment. Finally, some default values are assigned to the keys:  segment pos=.5, segment length=3ex, segment distance=1mm,  I hope this explanation helped. - @JlDiaz It is working, but I don't know what it is doing exactly, could you be so kind to explain what the to path makes? – Dominic Michaelis Mar 1 '13 at 15:45 @DominicMichaelis I did a complete rewrite of my answer. Explanations added. – JLDiaz Mar 1 '13 at 16:38 I don't know how you have created the node Q. Hence this is one blind shot:  \documentclass{scrartcl} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw (0,3) -- node[anchor=south west] (f) {$f_2$} (4,0) node[anchor=south west] (q) {$Q$}; \draw[-latex] (f) -- (q); \end{tikzpicture} \end{document}  You can bring the arrow close to the letters by reducing the inner sep such as \draw (0,3) -- node[anchor=south west,inner sep=1pt] (f) {$f_2$} (4,0) node[anchor=south west,inner sep=1pt] (q) {$Q$};  # Edit: Playing little with the dimensions of above and inner sep for the nodes f2 and Q and labelling them, you can draw the required line: \documentclass{scrartcl} %borceux, handbook of categorical algebra \usepackage{tikz} %mac lane, categories for the working mathematician \usetikzlibrary{calc,patterns} \begin{document} \begin{tikzpicture} \tikzstyle{ground}=[fill,pattern=north east lines,draw=none,pattern color=gray] \coordinate (A) at (0cm,0cm); \coordinate (B) at (0cm,3cm); \coordinate (C) at (4cm,0cm); \coordinate (B1) at ($ (B)+(5pt,0pt)$); % x koordinate des oberen lagers \coordinate (B2) at ($ (B)-(0pt,5pt)$); %y koordinate des oberen lagers \coordinate (C1) at ($ (C)+(-5pt,0pt)$); % x koordinate des unteren lagers \coordinate (C2) at ($ (C)+(0,5pt)$); %y koordinate des unteren Lagers \draw [|<->|, very thin] ($  (A)-(12pt,0) $)--node[fill=white]{3}($ (B)-(12pt,0pt)$); \draw {} (A)--node[ground,anchor=east,minimum height=3.25cm,yshift=-0.125cm]{} (B); \draw [very thick] ($(B1)+(B2)-(B)$) --node[anchor=south west,above=4pt,inner sep=1pt](f){$f_2$}($(C1)+(C2)-(C)$); \draw [|<->|,very thin] ($ (A)-(0cm,12pt)$)-- node[fill=white]{4}($(C)-(0,12pt)$); \draw (A) -- node[ground,anchor=north,minimum width=4cm]{}(C); \draw (C) -- node[ground,anchor=west,minimum height=3.25cm,yshift=-0.125cm]{} +(0,3cm); %Pfeile \draw [<-,shorten <=1mm, very thick] (B1)-- +(0,0.75cm)node[anchor=south]{$f$}; \draw [<-,shorten <=1mm, very thick] (B2) -- +(-0.75cm,0)node[anchor=east]{$f_1$}; \draw [<-,shorten <=1mm, very thick] (C2) -- +(0.75cm,0)node[anchor=west]{$f_3$}; \draw [<-,shorten <=1mm, very thick] (C1) -- +(0,-0.75cm)node[anchor=north]{$f_4$}; %oben links wÃ¤nde fÃ¼r Lager \draw [very thick] (B) -- + (10pt,0); \draw [very thick] (B) -- +(0,-10pt); \fill [black,opacity=0.5] ($ (B1)+(B2)-(B) $) circle (5pt) node[anchor=south west] {\textcolor{black}{$P$}}; \fill [black,opacity=0.5] ($(C1)+(C2)-(C)$) circle (5pt) node[anchor=south, above=4pt,inner sep=1pt](q){\textcolor{black}{$Q\$}};
\draw[-latex] (f) -- (q);
\end{tikzpicture}
\end{document}


-