3

When draw a table with matrix, the line sounds like not perfect aligned.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit,backgrounds}
\begin{document}
\tiny\begin{tikzpicture}[>=latex]
\matrix (m1) [matrix of nodes,anchor=center,align=left,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={draw,align=left},
    column 1/.style={nodes={minimum width=3em,minimum height=1.5em}},
    column 2/.style={nodes={minimum width=8em,minimum height=1.5em}},
    ]
{
\_id & \_data \\   
32 & Keypress.ogg \\   
57   & Sparse.ogg \\ 
\vdots & \vdots \\   
};
\node[above=0 of m1]  (N1) {example};
\begin{pgfonlayer}{background}
\node[fit=(m1)(N1), fill=blue!10,dashed] {};
\end{pgfonlayer}


\end{tikzpicture}
\end{document}

The lines between cells not align perfectly (maybe a little bit thick and shifted). and the text not align to left in the cells.

enter image description here

lucky1928
  • 4,151

2 Answers2

5

This is because of the different depths of the texts, and you can cure this by setting universal text depths and text heights.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit,backgrounds}
\begin{document}
\tiny\begin{tikzpicture}[>=latex]
\matrix (m1) [matrix of nodes,anchor=center,align=left,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={draw,align=left,text depth=0.25ex,text height=1.5em-0.25ex},
    column 1/.style={nodes={minimum width=3em,minimum height=1.5em}},
    column 2/.style={nodes={minimum width=8em,minimum height=1.5em}},
    ]
{
\_id & \_data \\   
32 & Keypress.ogg \\   
57   & Sparse.ogg \\ 
\vdots & \vdots \\   
};
\node[above=0 of m1]  (N1) {example};
\begin{pgfonlayer}{background}
\node[fit=(m1)(N1), fill=blue!10,dashed] {};
\end{pgfonlayer}


\end{tikzpicture}
\end{document}

enter image description here

minimum height=1.5em is redundant but I kept it.

To align the text on the left, a quick solution is to set the text width appropriately. (There is a better solution which I will add once I am on my other laptop...)

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit,backgrounds}
\begin{document}
\tiny\begin{tikzpicture}[>=latex]
\matrix (m1) [matrix of nodes,anchor=center,align=left,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={draw,align=left,text depth=0.25ex,text height=1.5em-0.25ex},
    column 1/.style={nodes={text width=width("\_id"),minimum height=1.5em}},
    column 2/.style={nodes={text width=width("Keypress.ogg"),minimum height=1.5em}},
    ]
{
\_id & \_data \\   
32 & Keypress.ogg \\   
57   & Sparse.ogg \\ 
\vdots & \vdots \\   
};
\node[above=0 of m1]  (N1) {example};
\begin{pgfonlayer}{background}
\node[fit=(m1)(N1), fill=blue!10,dashed] {};
\end{pgfonlayer}


\end{tikzpicture}
\end{document}

And this is the more automatic solution. It allows you to specify the alignment of every column, the allowed values are l, c and r.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit,backgrounds}
\usepackage{eqparbox}
\newbox\matrixcellbox
\tikzset{column align/.style 2 args={column #1/.style={nodes={execute at begin
node={\setbox\matrixcellbox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
\begin{document}
\tiny\begin{tikzpicture}[>=latex]
\matrix (m1) [matrix of nodes,anchor=center,align=left,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={draw,align=left,text depth=0.25ex,text height=1.5em-0.25ex},
    column 1/.style={nodes={minimum width=3em,minimum height=1.5em}},
    column 2/.style={nodes={minimum width=8em,minimum height=1.5em}},
    column align={1}{l},column align={2}{l},
    ]
{
\_id & \_data \\   
32 & Keypress.ogg \\   
57   & Sparse.ogg \\ 
\vdots & \vdots \\   
};
\node[above=0 of m1]  (N1) {example};
\begin{pgfonlayer}{background}
\node[fit=(m1)(N1), fill=blue!10,dashed] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, how to align text to left of cell! – lucky1928 Oct 23 '19 at 00:39
  • @lucky1928 I added a first solution using text width. There is a better solution available but I do not have it on my laptop I am at now. –  Oct 23 '19 at 00:45
2

I am thinking about just draw it!

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit,backgrounds,calc}
%https://tex.stackexchange.com/questions/225604/determine-number-of-columns-in-tikz-matrix/493744
\makeatletter
\tikzset{savecols/.style={execute at end matrix={
    \xdef#1{\the\pgf@matrix@numberofcolumns}}},
saverows/.style={execute at end matrix={
    \xdef#1{\the\pgfmatrixcurrentrow}}}}
\makeatother
\begin{document}
\tiny\begin{tikzpicture}[>=latex]
\matrix (m) [matrix of nodes,anchor=center,align=left,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    column 1/.style={nodes={minimum width=3em,minimum height=1.5em}},
    column 2/.style={nodes={minimum width=8em,minimum height=1.5em}},
    saverows=\rows,savecols=\cols,
    ]
{
\_id & \_data \\   
32 & Keypress.ogg \\   
57   & Sparse.ogg \\ 
\vdots & \vdots \\   
};
\foreach \col in {1,...,\cols} {
    \draw (m-1-\col.north west) -- (m-\rows-\col.south west);
    \ifnum\col=\cols
        \draw (m-1-\col.north east) -- (m-\rows-\col.south east);
    \fi
}
\foreach \row in {1,...,\rows} {
    \draw (m-\row-1.north west) -- (m-\row-\cols.north east);
    \ifnum\row=\rows
        \draw (m-\row-1.south west) -- (m-\row-\cols.south east);
    \fi
}
\end{tikzpicture}
\end{document}

enter image description here

beetlej
  • 947
  • 5
  • 15