Selectively "erase" lines from TikZ matrix
You can avoid the unwanted cell borders by just not drawing them. To this end, this answer defines a style pborder
, which accepts any combination of the letters t
, b
, l
and r
, representing top
, bottom
, left
and right
respectively. So
\path (0,4) node[pborder=tbl,font=\sffamily] (tbl) {tbl}
node[right=of tbl,pborder=br,font=\sffamily] (br) {br};
will produce
and
\matrix(lol)[matrix of nodes, nodes in empty cells,draw,
inner sep=-\pgflinewidth/2,outer sep=0pt,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={draw,outer sep=0pt},text width=2em, text height=2em]{
& & & & \\
|[pborder=tbl]| & |[pborder=tbr]| & & & |[pborder=ltr]| \\
& |[pborder=ltr]| & & & |[pborder=lbr]| \\
& |[pborder=lbr]| & & & \\
};
So there is absolutely no overpainting whatsoever.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\usepgfmodule{parser}
\pgfparserdef{matrixborder}{initial}{the letter l}%
{\draw (path picture bounding box.north west)
-- (path picture bounding box.south west);}%
\pgfparserdef{matrixborder}{initial}{the letter r}%
{\draw (path picture bounding box.north east)
-- (path picture bounding box.south east);}%
\pgfparserdef{matrixborder}{initial}{the letter t}%
{\draw (path picture bounding box.north west)
-- (path picture bounding box.north east);}%
\pgfparserdef{matrixborder}{initial}{the letter b}%
{\draw (path picture bounding box.south west)
-- (path picture bounding box.south east);}%
\pgfparserdef{matrixborder}{initial}{the character ;}%
{\pgfparserswitch{final}}
\tikzset{pborder/.style={draw=none,path picture={%
\pgfparserparse{matrixborder}#1;%
}}}
\begin{document}
\begin{tikzpicture}
\path (0,4) node[pborder=tbl,font=\sffamily] (tbl) {tbl}
node[right=of tbl,pborder=br,font=\sffamily] (br) {br};
\matrix(lol)[matrix of nodes, nodes in empty cells,draw,
inner sep=-\pgflinewidth/2,outer sep=0pt,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={draw,outer sep=0pt},text width=2em, text height=2em]{
& & & & \\
|[pborder=tbl]| & |[pborder=tbr]| & & & |[pborder=ltr]| \\
& |[pborder=ltr]| & & & |[pborder=lbr]| \\
& |[pborder=lbr]| & & & \\
};
\end{tikzpicture}
\end{document}
You could say that if you want to drop one border there should be a simpler way than adding all the borders that are not to be dropped. This slight extension does this. Now e.g. |[pborder=-r]|
drops the border on the right.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\usepgfmodule{parser}
\newif\ifnegateborder
\negateborderfalse
\pgfparserdef{matrixborder}{initial}{the letter l}%
{\ifnegateborder
\draw (path picture bounding box.north west)
-- (path picture bounding box.north east)
|- (path picture bounding box.south west);
\else
\draw (path picture bounding box.north west)
-- (path picture bounding box.south west);
\fi}%
\pgfparserdef{matrixborder}{initial}{the letter r}%
{\ifnegateborder
\draw (path picture bounding box.north east)
-- (path picture bounding box.north west)
|- (path picture bounding box.south east);
\else
\draw (path picture bounding box.north east)
-- (path picture bounding box.south east);
\fi}%
\pgfparserdef{matrixborder}{initial}{the letter t}%
{\ifnegateborder
\draw (path picture bounding box.north west)
-- (path picture bounding box.south west)
-| (path picture bounding box.north east);
\else
\draw (path picture bounding box.north west)
-- (path picture bounding box.north east);
\fi}%
\pgfparserdef{matrixborder}{initial}{the letter b}%
{\ifnegateborder
\draw (path picture bounding box.south west)
-- (path picture bounding box.north west)
-| (path picture bounding box.south east);
\else
\draw (path picture bounding box.south west)
-- (path picture bounding box.south east);
\fi}%
\pgfparserdef{matrixborder}{initial}{the character -}%
{\negatebordertrue}%
\pgfparserdef{matrixborder}{initial}{the character ;}%
{\pgfparserswitch{final}}%
\tikzset{pborder/.style={draw=none,path picture={%
\negateborderfalse
\pgfparserparse{matrixborder}#1;%
}}}
\begin{document}
\begin{tikzpicture}
\matrix(lol)[matrix of nodes, nodes in empty cells,draw,
inner sep=-\pgflinewidth/2,outer sep=0pt,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={draw,outer sep=0pt},text width=2em, text height=2em]{
& & & & \\
|[pborder=-r]| & |[pborder=-l]| & & & |[pborder=-b]| \\
& |[pborder=-b]| & & & |[pborder=-t]| \\
& |[pborder=-t]| & & & \\
};
\end{tikzpicture}
\end{document}
Not sure exactly why you want to draw over the lines later, but one way you can work around the issue of the edges is to use \shorten
to make the line slightly shorter
shorten <= 0.5\pgflinewidth, shorten >= 0.5\pgflinewidth
Code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\tikzset{Blank Out Line/.style={line width = 1pt, draw=white, shorten <= 0.5\pgflinewidth, shorten >= 0.5\pgflinewidth}}
\begin{document}
\begin{tikzpicture}
\matrix(lol)[matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth,column sep=-\pgflinewidth,nodes={draw},text width=2em, text height=2em]{
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\path[Blank Out Line] (lol-2-2.north west) -- (lol-2-2.south west);
\path[Blank Out Line] (lol-3-5.north east) -- (lol-3-5.north west);
\path[Blank Out Line] (lol-3-2.south east) -- (lol-3-2.south west);
\end{tikzpicture}
\end{document}