# draw line between two halves of a tikz matrix easily

\begin{center}\begin{tikzpicture}
\matrix (m)
[
matrix of math nodes,
row sep    = 3em,
column sep = 4em
]
{
1 & 3 \\
2 & 4\\
};
\end{tikzpicture}\end{center}


I'd like to draw a vertical line to separate the two sides of the matrix. I realize one can extend the matrix and such but I'd rather have something that is a bit more general and doesn't explicitly depend on a bunch of stuff(such as specifying nodes, extending the matrix size, computing mid points using some package, etc). (this way it works for more complex examples).

• If the number of columns is even, you can simply do \draw (m.north) -- (m.south);. If not, have a look at my fixed matrix in this answer. – Max Sep 10 '18 at 12:49
• @Max If the number is even and the nodes have all the same widths. – marmot Sep 10 '18 at 12:50
• Here you can find macros that do that. It is the same trick as in Zarko's answer, but done some months ago... – marmot Sep 10 '18 at 12:54
• @marmot Oops, forgot that for a second. – Max Sep 10 '18 at 12:54

try

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc,
matrix
}

\begin{document}
\begin{tikzpicture}
\matrix (m)
[
matrix of math nodes,
row sep    = 3em,
column sep = 4em
]
{
X & A \\
Y & B\\
};
\draw ($(m-1-1.north east)!0.5!(m-1-2.north west)$) -- ($(m-2-1.south east)!0.5!(m-2-2.south west)$);
\end{tikzpicture}
\end{document}


• This trick has already been used here and perhaps before. When I wrote that answer, I couldn't find an earlier post, but that does not mean much. – marmot Sep 10 '18 at 12:56
• @marmot, i know that i didn't discover a wheel, it exist very long time :-). i accidentally had a little free time, so I wrote this simple answer. – Zarko Sep 10 '18 at 13:06