# TikZ: vertical rule between columns with differently sized cells

I would like to draw a rule separating two columns. Unfortunately, it won't work properly if the cells have different widths. Are there any known workarounds?

My code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
a & b & c \\
d & long row & e \\
f & g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}


Result (notice the rule intersecting the long cell):

-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
\node(a){a}; & \node(b){b}; & \node(s1){}; & \node(c){c}; \\
\node(d){d}; & \node(long){long row}; & \node(s2){}; & \node(e){also long}; \\
\node(f){f}; & \node(g){g}; & \node(s3){}; & \node(h){h}; \\
};
\draw[red] (s1.north) -- (s3.south);
\end{tikzpicture}
\end{document}


-
The problem is that this won't work if the cell containing e is longer than the cells c and h. Add some more characters to cell e and you'll see what I mean. –  watain Apr 25 '14 at 21:11
Also, the rule won't be straight anymore if cell c is wider than cell h, or vice versa. –  watain Apr 25 '14 at 21:14
I edited my answer to include a blank spacer column, through which you could draw the line. Not the most elegant solution, but it may work. –  erik Apr 25 '14 at 21:25
That isn't too elegant, that's true - but it works. Thanks a lot! I wonder whether there's a way to force all cells in a column to be of equal width, as it would make things much easier. Unfortunately, I haven't found a way to do that yet. –  watain Apr 25 '14 at 21:38
You could use row sep= and col sep=, but then the spacer cell approach wouldn't work. Can you use one of the tabular environments instead? That might be better than the TikZ matrix for what you're trying to achieve. –  erik Apr 25 '14 at 21:44

You can specify the text width for those nodes to be the width of the long row and align=center.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
a & |[text width=\widthof{long row},align=center]|b & c \\
d & |[align=center]|long row & e \\
f & |[text width=\widthof{long row},align=center]|g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}


# Update:

You can fix the text width for the entire second column by adding

column 2/.style={nodes={text width=\widthof{long row},align=center}}


to the matrix options.

Code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
column 2/.style={nodes={text width=\widthof{long row},align=center}}]{
a & b & c \\
d & long row & e \\
f & g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}

-
That's interesting. However, it's pretty inconvenient having to specify long row for each cell. Thank you anyway! –  watain Apr 26 '14 at 9:24
@watain Please see the update. –  Harish Kumar Apr 26 '14 at 23:11

Add empty cells with optinal text height to adjust placement of rule.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells]{
a & b &|[text height=1em]| & c \\
d & long row && e \\
f & g &|[text height=1em]|& h \\
};
\draw[red] (m-1-2.north east -| m-3-3.south) -| (m-3-3.south);
\end{tikzpicture}
\end{document}


-