3

In this code from TikZ (finite) grid with character in each cell

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

\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\};
\end{tikzpicture}
\end{document}

I would like to delete the top and left edges of the grid.

I have found solutions using nodes but much prefer this one with a matrix.

Ethan Bolker
  • 9,333
  • 3
  • 42
  • 69

2 Answers2

3

We can use \foreach:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
%\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\foreach \i in {-0.5,0,0.5,1} {
    \draw[gray] (-1,-\i)--(1,-\i);
    \draw[gray] (\i,-1)--(\i,1);
}
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\};
\end{tikzpicture}
\end{document}

enter image description here

Honestly I don't think nodes or matrices have any thing to do here.


This approach is based on marmot's creative way, which is nicer and more tricky:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] ({-1cm+0.2pt},-1) grid (1cm,{1cm-0.2pt});
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\};
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thanks. There is a "matrix" in the code. Other answers to my original question used nodes. – Ethan Bolker Mar 25 '19 at 13:46
  • @EthanBolker No problem. You are welcome ;) –  Mar 25 '19 at 13:47
  • Maybe mention also \draw[step=0.5cm,color=gray] (-0.99,-1) grid (1,0.99); which is shorter and gives (for all practical purposes) the same output. –  Mar 25 '19 at 15:15
  • @marmot Ohh your way is wonderfully creative :)) I did not even think of that. However they are not mathematically correct. –  Mar 25 '19 at 15:17
  • @JouleV What do you mean by "not mathematically correct"? It is as correct as a truncated grid can be. –  Mar 25 '19 at 15:18
  • @marmot As I know the default value of line width is 0.4pt, so to make it mathematically correct I think we should use \draw[step=0.5cm,color=gray] ({-1cm+0.2pt},-1) grid (1cm,{1cm-0.2pt}); :) –  Mar 25 '19 at 15:21
  • @JouleV And what about the line cap? BTW, this is why I wrote "or all practical purposes". –  Mar 25 '19 at 15:28
  • @marmot That's why I wrote "mathematically" :) –  Mar 25 '19 at 15:32
  • I see. "mathematically" = "doesn't matter" ;-) –  Mar 25 '19 at 15:32
2

Another solution. It draws the grid with a matrix of drawn nodes. After that, left and top border are deleted with a white supperposed line.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={draw=gray, anchor=center, minimum size=.6cm}, column sep=-\pgflinewidth, row sep=-\pgflinewidth] (A) {
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\};
\draw[white] ([xshift=.5\pgflinewidth]A-4-1.south west)|-([yshift=-.5\pgflinewidth]A-1-4.north east);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588