[Tex/LaTex] TikZ: How to draw a flow chart using library chains and matrix of nodes

tikz-chainstikz-matrixtikz-pgf

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}

Best Answer

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...

enter image description here

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...