Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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): enter image description here

share|improve this question

3 Answers 3

up vote 6 down vote accepted

How about this?

\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}

enter image description here

share|improve this answer
    
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 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 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 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 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 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}

enter image description here

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}
share|improve this answer
    
That's interesting. However, it's pretty inconvenient having to specify long row for each cell. Thank you anyway! –  watain Apr 26 at 9:24
1  
@watain Please see the update. –  Harish Kumar Apr 26 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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.