2

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

enter image description here

The lines still disappear.

enter image description here

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.

  1. 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.
  2. 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".
  3. 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.
  4. 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. enter image description here enter image description here

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.)

  • the code here isn't really comparable, you are placing a single coloured panel behind the entire row. If you colour different cells with differently backgrounds then you would see similar effects in tikz. the panels are not bigger than the cells using colortbl but they may be rendered over-large due to pixel boundaries. – David Carlisle Feb 19 '17 at 14:37
  • the hhline question that you link to does not indicate that there are problems with that package, it was simply user-error in the input as explained in the answer there. – David Carlisle Feb 19 '17 at 14:38
  • 1
    I normally use a combination of tikz (to redraw the rules and sometimes also for the background color) and standard tabulars. E.g. like here tex.stackexchange.com/a/294799/2388. But is imho not simpler. A very good colored table is like a good graphic and takes some time. – Ulrike Fischer Feb 19 '17 at 14:58
  • @DavidCarlisle sorry i was lazy to make a new document to show that the hhline doesn't help, i just google something about hhline. (lol) the post is edited. – fairytale Feb 19 '17 at 15:20
  • thanks for fixing the hhline reference. your second paragraph is still wrong of course, whether or not you agree, the panels do not overlap the lines as specified. Not that that helps you as a user, they still vanish in the pdf reader at some resolutions. – David Carlisle Feb 19 '17 at 15:30
1

Does this count as perfect table?

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,backgrounds,fit}

\begin{document}

\tikzset{
    a/.style={
        draw,fill=white
    },
    b/.style={
        draw,fill=gray!50
    },
    c/.style={
        draw,fill=green,inner ysep=0,inner xsep=-.5\pgflinewidth
    }
}

\begin{tikzpicture}
  \matrix (m) [
  matrix of nodes,
  nodes in empty cells,
  row sep=-\pgflinewidth,
  column sep=-2\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex},
  column 1/.style = {nodes={a, minimum width=1cm}},
  column 2/.style = {nodes={a, minimum width=2cm}},
  column 3/.style = {nodes={a, minimum width=2cm}},
  row 1/.style={nodes={b}},
  ] 
  {   & 2 & 3 \\
      &   &   \\
    2 & c & d \\
    3 & & e   \\
  };

\node[c,fit=(m-2-1)(m-2-3)]{\parbox[c][2.5em][b]{5cm}{\centering green cell long}};

\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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