11

How do I fill a grid in TikZ? I want to do something like:

\draw[draw,fill=red!20] (0,0) grid (2,2);

instead of manually drawing 4 rectangles.

Ben Ruijl
  • 838

3 Answers3

17

Unfortunately not a direct solution, but you can just append a rectangle operation going back to the origin of the grid:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [ultra thick, draw=black, fill=black!20!white] (0,0) grid  (2,2) rectangle (0,0);
\end{tikzpicture}
\end{document}

filled grid tikz

Jake
  • 232,450
4

I think no but perhaps I am wrong. One rectangle is enough:

  \begin{tikzpicture}
  \fill[red!20](0,0) rectangle (2,2); 
  \draw (0,0) grid (2,2);
  \end{tikzpicture} 
pluton
  • 16,421
Alain Matthes
  • 95,075
0

With PSTricks. Unfortunately PSTricks' grid has no options for changing

  • the fill color
  • the line style

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\addtopsstyle{gridstyle}{griddots=0}
\begin{document}
\begin{pspicture}[showgrid=top](4,4)
    \psframe*[linecolor=gray](4,4)
\end{pspicture}
\end{document}