I have seen many discussions about "Table cell color overlaps cell border". I want to share my try on this problem. Also, I am a beginner in latex, my code may not be the best, I would like to see if there is any better solution.
Some said that it is the problem of pdf viewer. I don't agree, because the cellcolor is really "bigger" than the cell.
Some said that we can make the border thicker, but I think it make my tables look strange, so I don't want to use it. On the other hand, the \hhline solution cannot help much.
Colored tables and cline/hhline
The lines still disappear.
I started to use tikz matrix to draw tables.
The cellcolor is perfect and will not overlap any cell border. However, there are some disadvantages.
- The tikz picture is in a box. (i'm not very sure) The box caused indentations and overfull hbox. I need to use trimbox to correct the position of the table.
- Every cell has different size according to the contents inside them, you need to set a minimum height/width to make your table display properly. You need to find the best value by "trial and error".
- Making multicolumn/multirow is difficult. You need to use a parbox to insert the content into the cell, also, you need to find the best position of parbox by "trial and error" too.
- It is difficult to make line break too. You need to set the
textwidth
of the cell to make linebreak.
Here is the MWE.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,backgrounds,fit}
\tikzset{no right/.style={
draw=none,
append after command={
[shorten <= -0.5\pgflinewidth]
(\tikzlastnode.north east)
edge(\tikzlastnode.north west)
([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west)
edge([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
}
}
}
\tikzset{no left/.style={
draw=none,
append after command={
[shorten <= -0.5\pgflinewidth]
(\tikzlastnode.north east)
edge(\tikzlastnode.north west)
([shift={( -0.5\pgflinewidth,0.5\pgflinewidth)}]\tikzlastnode.north east)
edge([shift={( -0.5\pgflinewidth,0.5\pgflinewidth)}]\tikzlastnode.south east)
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
}
}
}
\tikzset{no left right/.style={
draw=none,
append after command={
[shorten <= -0.5\pgflinewidth]
(\tikzlastnode.north east)
edge(\tikzlastnode.north west)
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
}
}
}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black},nodes in empty cells]
\matrix (m) [
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-2\pgflinewidth,
nodes={anchor=center,text height=2ex,text depth=0.25ex},
column 1/.style = {nodes={cell, minimum width=1cm}},
column 2/.style = {nodes={cell, minimum width=2cm}},
column 3/.style = {nodes={cell, minimum width=2cm}},
row 1/.style={nodes={cell,fill=black!25!white}},
row 2/.style={nodes={cell}},
row 3/.style={nodes={cell}},
row 4/.style={nodes={cell}},
]
{ & 2 & 3 \\
|[no right,fill=black!25!white]| & |[no left right,draw=none,fill=black!25!white]| & |[no left,fill=black!25!white]| \\
2 & c & d \\
3 & & e \\
};
\node[fit=(m-2-1)(m-2-3)]{\parbox[c][2.5em][b]{5cm}{\centering green cell long}};
\end{tikzpicture}
\end{document}
All borders can be seen at any zoom level.
The above code is based on: Cut one side of a rectangle node in TikZ
Borders of a matrix are not well placed
My question is : Is there any simpler way to draw "perfect table"? (i.e, Table cell color will not overlap cell border.)
Best Answer
Does this count as perfect table?