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 use TikZ matrices to for some drawings. I am currently using nodes and positioning them manually to simulate a tabular. That helps in positioning arrows and shading.

TikZ matrix

However, if I use something like the following code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix [matrix of nodes,row sep=0,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0   & 6 \\
1   & 3 \\
};
\end{tikzpicture}
\end{document}

The nodes' lower and upper borders do not overlap, resulting in a double border between nodes.

Double border

edit: Jake suggested a very nice solution, using negative row separation row sep. That work very well for one column tables. But the columns are misaligned when there are more columns.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black},
space/.style={minimum height=1.5em,matrix of nodes,row sep=-\pgflinewidth,column sep=-\pgflinewidth,column 1/.style={font=\ttfamily}}]

\matrix (first) [space, column 1/.style={font=\ttfamily},column 2/.style={nodes={cell,minimum width=2em}}]
{
0   & 6 \\
1   & 3 \\
2   & 9 \\
};

\matrix (second) [right=of first, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0   &a  & 6 \\
1   &   & 3 \\
2   &c  & 9 \\
};

\matrix [right=of second, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0   &a  & 6 \\
1   &b  & 3 \\
2   &c  & 9 \\
};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
    
It's a good idea to make your examples full compilable documents, starting from \documentclass. Otherwise everyone who tries to compile the example first has to figure out what packages are needed (it's not immediately obvious that \usetikzlibrary{positioning} is required here, for example), and then write an adequate .tex file. –  Jake May 19 '11 at 9:27
    
Sorry, you are absolutely right. Matrix is also lacking. I will edit my question. –  ipavlic May 19 '11 at 9:28
add comment

1 Answer

up vote 32 down vote accepted

If you set row sep to -\pgflinewidth, the lines will overlap exactly:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0   & 6 \\
1   & 3 \\
};
\end{tikzpicture}
\end{document}

matrix of nodes

In order to make sure that the cells align correctly horizontally, you need to make the text height and text depth the same across the entire row. By setting nodes in empty cells, all cells will be drawn, even if no content is provided. Alternatively, you can enter {} into empty cells to achieve the same effect.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black},
space/.style={minimum height=1.5em,matrix of nodes,row sep=-\pgflinewidth,column sep=-\pgflinewidth,column 1/.style={font=\ttfamily}},text depth=0.5ex,text height=2ex,nodes in empty cells]

\matrix (first) [space, column 1/.style={font=\ttfamily},column 2/.style={nodes={cell,minimum width=2em}}]
{
0   & 6 \\
1   & 3 \\
2   & 9 \\
};

\matrix (second) [right=of first, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0   &a  & 6 \\
1   &   & 3 \\
2   &c  & 9 \\
};

\matrix [right=of second, space, column 2/.style={minimum width=3em,nodes={cell,minimum width=3.5em}},column 3/.style={nodes={cell,minimum width=2em}}]
{
0   &a  & 6 \\
1   &b  & 3 \\
2   &c  & 9 \\
};
\end{tikzpicture}
\end{document}

matrix tables with fixed text depth and text height

share|improve this answer
    
That's great! Thank you. –  ipavlic May 18 '11 at 14:03
    
@Jake: A slight problem. What if some nodes are empty in a matrix of nodes. They then do not take up space, and various rows get misaligned. –  ipavlic May 18 '11 at 19:46
    
@ipavlic: I'm not sure I understand what you mean. Could you edit your question to include an example? –  Jake May 18 '11 at 19:51
    
@Jake: I added the example. –  ipavlic May 19 '11 at 9:15
1  
@Jake: If I could upvote more than once, I would :). Thanks again! –  ipavlic May 19 '11 at 12:55
show 4 more comments

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.