# TikZ: How to draw a flow chart using library chains and matrix of nodes?

In this MWE of a flowchart, it doesn't compile when \chainin command is used and I need to understand the following:

1. align=center doesn't center the content of the node, for example, (m-2-1).
2. How can I break the line inside any of the matrix nodes since using \\ makes the compilation run for a long time?
3. How can I join between (m-3-1) and (m-1-2) in such a way that the join goes to the right until the midway, then vertically to the left of (m-1-2) then right?
4. When using \chainin command, I got an error: Undefined control sequence. \chainin despite loading chains library.

I already drew this flowchart without chains, but I need to know if it is possible with it or not.

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,chains, positioning,matrix,shapes}

\begin{document}

\begin{tikzpicture}[
every node/.style={draw,rectangle,align=center},
every join/.style=latex,
join
]
\matrix (m) [matrix of nodes,column sep=5mm,row sep=5mm]{
{1\\new line}   & 4\\
2               & 5\\
3               & 6\\
};

{ [start chain]
\chainin (m-1-1);
\chainin (m-2-1);
\chainin (m-3-1);
\chainin (m-1-2);
\chainin (m-2-2);
\chainin (m-3-2);
}

\end{tikzpicture}

\end{document}

• Your code doesn't work at all here, don't you get any errors when you try? Concerning 2, seems you also need to specify text width for matrix nodes, see tex.stackexchange.com/a/112726/586 – Torbjørn T. May 6 '17 at 17:39
• @TorbjørnT. My code doesn't work when using \chainin command as I said in my question body, and I need to understand why it fails. – Diaa May 6 '17 at 17:41
• Right, sorry, but you first get an error because of the line break in the first node of the matrix, which is fixed with text width, and then the text is centered in the nodes. – Torbjørn T. May 6 '17 at 17:42
• @TorbjørnT. You are right, specifying text width fixed my first two issues. – Diaa May 6 '17 at 17:44
• And the second I suppose? (I mean, \\  didn't work at all without text width.) Anyways, I'm no chains expert, but I think perhaps there has to be something on the chain already for \chainin to work. Someone else will probably be able to answer though. – Torbjørn T. May 6 '17 at 17:48

The problem with the Undefined control sequence is that you're missing one library:

\usetikzlibrary{scopes}


As far as I'm aware of, It's not mentioned in the manual but I've seen this issue before.

As for your issue (3), I think it's best that you draw that line separately and break the chain into two. At least because I don't know how to modify the \chainin path operator...

## MWE:

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,chains,scopes,positioning,matrix,shapes,calc}

\begin{document}
\begin{tikzpicture}[
every node/.style={draw,rectangle,align=center}, >=latex
]

\matrix (m) [matrix of nodes,column sep=5mm,row sep=5mm]
{
\node(m-1-1){1\\new line};  & 4\\
2               & 5\\
3               & 6\\
};

%\matrix[below=5cm] (m) [matrix of nodes,column sep=5mm,row sep=5mm]{
%   |[text width=1.5cm]| {1\\new line}  & 4\\
%   2               & 5\\
%   3               & 6\\
%};

{ [start chain,every on chain/.style={join=by ->}]
\chainin (m-1-1);
\chainin (m-2-1);
\chainin (m-3-1);
\chainin (m-1-2);
\chainin (m-2-2);
\chainin (m-3-2);
}

\draw[red,->] (m-3-1) -| ($(m-3-1)!.7!(m-1-2)$) |-(m-1-2);
\end{tikzpicture}
\end{document}


I also have in the MWE used a different method for making the line break inside the matrix environment (issue 2). As said so in the manual one can use nested nodes, so since every matrix cell is a node we can use a \node inside that node, and since we already have the style every node set to align=center, that node is already center aligned and therefore there will be no need for specifying a text width. Though I do not know why the key align=center does not work inside \matrix, perhaps a bug...

• Thanks for your answer. So, if I want to remove the black join between (m-3-1) and (m-1-2) in your answer, I have to make two chains (from 1 to 3, then from 4 to 6) instead of one single chain, right? – Diaa May 6 '17 at 19:14
• Yes, precisely! – Guilherme Zanotelli May 7 '17 at 14:59