# Latex arrows around corner tikz

I'm experiencing some troubles with Latex and the arrows in the tilkzpicture-environment. Can someone help me how to close the arrow on the bottom? I tried different things such as --|, |-, –| but it won't work

\documentclass[a4paper,12pt, right=10mm]{article}
\usepackage    [english]{babel}
\usepackage{marvosym}
\usepackage{eurosym}
\usepackage[labelfont=bf,textfont=it, font=small]{caption}
\usepackage{amsthm}
\usepackage[pdftex]{graphicx}
\usepackage{fancyhdr}
\setlength{\parindent}{0mm}
\usepackage{setspace}
\usepackage{flowchart} \usetikzlibrary{arrows, positioning,fit,calc}
\usepackage{amsmath, multicol, color, longtable}
\usepackage{array}
\usepackage{mathtools}

\begin{document}

\begin{figure}
\begin{tikzpicture}[>=stealth, node distance=7mm, %thick,
typetag/.style={rectangle, draw=black!50, font=\scriptsize, anchor=west}
]
\center{

\node (A2) at (0,0) [title]{\underline{Training set}};

\node (A3) [below= of A2.west, typetag] {$\mathbf{x}_{2N/10 \, +1}$, ... $\mathbf{x}_{{3N}{/10}}$};
\node (A7) [below=of A3.west, typetag] {...};
\node (A6) [below=of A7.west, typetag] {$\mathbf{x}_{9N/10 \, +1}$, ... $\mathbf{x}_{N}$};

\node (A1) [draw=double, double distance 1pt, thick, fit={(A2) (A3) (A7) (A6)} ] {};

\node (B1) [right= of A3.west, xshift=4cm ]{\underline{Coefficients:}};
\node (B2) [below= of B1.center, typetag, xshift=-0.5cm ] {$\hat{\beta}^{test}$};

\node (B3) [draw, thick, fit={(B1) (B2)} ] {};

\node (C1) [right= of B1.right, xshift=1cm ] [title]{\underline{Test set}};
\node (C2) [below= of C1.west, typetag] {$\mathbf{x}_{1}$, ... $\mathbf{x}_{{N}{/10}}$};

\node (C3) [draw=double, double distance 1pt, thick, fit={(C1) (C2)} ] {};

\node (D1) [right= of C1.right, xshift=1cm ] [title]{\underline{Mean-squared Error}};
\node (D2) [below= of D1.west, typetag] {$\mathbf{\hat{y}}_{1}-\mathbf{y}_1$, ... , $\mathbf{\hat{y}}_{{N}{/10}}-\mathbf{y}_{N/10}$};
\node (D3) [draw=double, double distance 1pt, thick, fit={(D1) (D2)} ] {};

\node (z) [below= of D3.center, yshift=-1.5 cm]{};
\node (z1) [below = of C3.center, yshift=-2cm]{};
\node (z2) [below= of A1.south, yshift=-0.5cm]{};

\draw[->] (A1) -- node[above] {lasso} (B3)(B3) ;
\draw[->] (C3) -- (z1) -- node[above] {permute test and training sets} (z2) -| (A1);
\draw[->] (B3) -- (C3);
\draw[->] (C3) -- (D3);
\node (X) [draw=double, double distance 1pt, thick, fit={(A1) (B3) (C3) (D3) (z1) (z2)} ] {};

}
\end{tikzpicture}
\caption{The diagramme shows the function of the crossvalidation}
\end{figure}
\end{document}

• What packages do you use? – Harald Lichtenstein Nov 7 '17 at 12:16
• Sorry, I had some troubles with adjusting the code, I'm actually very familiar with Latex, but the Tikz-environment is making me crazy. – Simona Nov 7 '17 at 12:22
• I wouldn't use nodes as coordinates in your case. It is sufficient to draw like this: \draw[->] (C3) -- +(0,-2) node[above, xshift=-3cm] {permute test and training sets} -| (A1); – Harald Lichtenstein Nov 7 '17 at 12:50

The reason you have gaps in that line, is that you're drawing lines between nodes. When drawing a line to a node, the line stops at the edge of a node. Try for example

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (a) {};
\node (b) at (1,0) {};
\node (c) at (2,0) {};
\draw (a) -- (b) -- (c);
\end{tikzpicture}
\end{document}


You get a gap in the line because it stops at the left edge of b, and starts again on the right edge of b. To fix that, use the center anchor:

\draw (a) -- (b.center) -- (c);


Or define them as \coordinates instead of \nodes, i.e. \coordinate (b) at (1,0);. In other words this is a working definition:

\draw[->] (C3) -- (z1.center) -| (A1);


You do not need z2 as well. z1 is directly below C3, so draw a straight line there, and then use -| to draw first horizontally to directly below A1, and then straight up.

Alternatively you could avoid defining the z-nodes, by using

 \draw[->] (C3) -- ++(0,-3cm) -| (A1);


There are several other problems with your code though, surely you must get a lot of errors from it:

• Have you defined the title style somewhere else? I added just an empty definition here.
• draw=double,double distance 1pt should be draw,double,double distance=1pt I'd think.
• B1.right should be B1.east. There is no anchor called right.
• Place all the options inside the same brackets, e.g. [right=of A,title], instead of [right=of A][title].
• \center{ ... } doesn't make sense inside a tikzpicture, it causes an error, and I think it should be used as \begin{center} .. \end{center}.
• Don't specify the driver ([pdftex]) for graphicx. The package figures out on it's own, so use just

\usepackage{graphicx}


(Or remove it altogether, as it's also loaded by TikZ.)

I also set the superscript test as \mathrm, and used e.g. $x_1, \dots, x_N$ instead of $x_1$, ..., $x_N$.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[
>=stealth,
node distance=7mm,
title/.style={},
thick, typetag/.style={rectangle, draw=black!50, font=\scriptsize, anchor=west}
]

\node (A2) at (0,0) {\underline{Training set}};
\node (A3) [below=of A2.west, typetag] {$\mathbf{x}_{2N/10 , +1}, \dots,\mathbf{x}_{{3N}{/10}}$}; \node (A7) [below=of A3.west, typetag] {\dots};
\node (A6) [below=of A7.west, typetag] {$\mathbf{x}_{9N/10 , +1}, \dots, \mathbf{x}_{N}$};
\node (A1) [draw,double, double distance=1pt, thick, fit={(A2) (A3) (A7) (A6)} ] {};

\node (B1) [right=4.7cm of A3.west]{\underline{Coefficients:}};
\node (B2) [below= of B1.center, typetag, xshift=-0.5cm ] {$\hat{\beta}^{\mathrm{test}}$};
\node (B3) [draw, thick, fit={(B1) (B2)} ] {};

\node (C1) [right=1.7cm of B1.east, title]{\underline{Test set}};
\node (C2) [below=of C1.west, typetag] {$\mathbf{x}_{1},\dots, \mathbf{x}_{{N}{/10}}$};
\node (C3) [draw,double, double distance=1pt, thick, fit={(C1) (C2)} ] {};

\node (D1) [right=1.7cm of C1.east,title]{\underline{Mean-squared Error}};
\node (D2) [below=of D1.west, typetag] {$\mathbf{\hat{y}}_{1}-\mathbf{y}_1,\dots, \mathbf{\hat{y}}_{{N}{/10}}-\mathbf{y}_{N/10}$};
\node (D3) [draw,double, double distance=1pt, thick, fit={(D1) (D2)} ] {};

\node (z) [below=-2.2cm of D3.center]{};
\node (z1) [below=2.7cm of C3.center]{};
\node (z2) [below=1.2 of A1.south]{};

\draw[->] (A1) -- node[above] {lasso} (B3.west |- A1.east);
%\draw[->] (C3) -- (z1.center) -| (A1);
\draw[->] (C3) -- ++(0,-3cm) -| (A1);
\draw[->] (B3) -- (C3);
\draw[->] (C3) -- (D3);

\node (X) [double, double distance=1pt, thick, fit={(A1) (B3) (C3) (D3) (z1) (z2)} ] {};

\end{tikzpicture}
\end{document}

• Dear Tjorbjørn, thank you for the help. I see the problem with using nodes instead of coordinates. I thought it was the same. And I didn't know that I had to use the math environment, since it was working either way! ANYWAY; THANK YOU A LOT!!!! – Simona Nov 7 '17 at 12:30
• @Simona I added a couple of extra points to my answer, and edited the code a bit more. Well, the $x_1$, ... $x_N$ part is working, but I think $x_1,\dots,x_N$ is more semantically correct. – Torbjørn T. Nov 7 '17 at 12:38
• That's true, anyway, it's fine, I had to add "draw=" into all the boxes where I wanted to have a double frame, like in your picture, otherwise there was no frame at all. But this must be something to do with the other packages I used. – Simona Nov 7 '17 at 13:51
• @Simona I don't follow, what must be to do with other packages? – Torbjørn T. Nov 7 '17 at 14:00
• When I copied your code and pasted it into my longer document, after compiling I had exactly the same image as above except for the frames around 'Test set', ‘Training Set‘ and ‘Mean Squared error‘. So I added node (C3) [draw=double, double distance=1pt, thick, fit={(C1) (C2)} ] {}; But I guess, you've already changed that! - sorry for causing confusions – Simona Nov 7 '17 at 14:30