22

I have the following TikZ grid:

Current Output

But I'd like to draw the grid without edges, or with half of the edges cells missing, so that it appears to be a small section of a larger, infinite grid. What's the best way to achieve this?

\documentclass[border=0.25cm]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \draw[step=0.5cm,color=gray] (0,0) grid (3.5,3.5);
  \end{tikzpicture}
\end{document}
Richard
  • 5,723

5 Answers5

30

If you make the coordinates not an exact multiple of the step size you can get this effect:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
   \draw[step=0.5cm,color=gray] (.75,.75) grid (3.75,3.75);
\end{tikzpicture}
\end{document}

output of code

Alan Munn
  • 218,180
15

If you care for something a bit fancier, you could also try that one:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
   \draw[step=0.5cm,color=gray,path fading=south] (.99,.75) grid (3.5,1);
   \draw[step=0.5cm,color=gray,path fading=north] (.99,3.5) grid (3.5,3.75);
   \draw[step=0.5cm,color=gray,path fading=west] (.75,.99) grid (1,3.5);
   \draw[step=0.5cm,color=gray,path fading=east] (3.5,.99) grid (3.75,3.5);
   \draw[step=0.5cm,color=gray] (1,1) grid (3.5,3.5);
\end{tikzpicture}
\end{document}

output of code

Alan Munn
  • 218,180
  • Possible : \begin{tikzpicture}[styling/.style={step=0.5cm,color=gray,path fading=#1}] then styling=south etc. and styling=none for the last path. – Alain Matthes May 25 '12 at 11:18
10

I needed the same thing, but had things in the cells of the grid, which I also wanted to fade out. This solution is perhaps less elegant than others, but permits this sort of fading.

It just puts fading rectangles over the edges so they fade out to white.

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

\begin{document}
\begin{tikzpicture}
    \draw[step=0.5cm] (0.1,0.1) grid (3.9,3.9);
    % Some letters to demonstrate fading
    \node at (0.25,0.25) {F};
    \node at (0.75,0.75) {A};
    \node at (1.25,0.25) {D};
    \node at (1.75,0.75) {E};

    \begin{scope}[transparency group]
        % Left edge
        \fill[path fading=east, color=white] (0,0) rectangle (1,4);
        % Bottom edge
        \fill[path fading=north, color=white] (0,0) rectangle (4,1);
        % Right edge
        \fill[path fading=west, color=white] (3,0) rectangle (4,4);
        % Top edge
        \fill[path fading=south, color=white] (0,3) rectangle (4,4);
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
markgw
  • 101
6

You could draw some additional lines:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (0,0) grid (3.5,3.5);
\foreach \i in {0,0.5,...,3.5}
  {
  \draw[gray] (3.5,\i) -- (3.8,\i);
  \draw[gray] (0,\i) -- (-0.3,\i);
  \draw[gray] (\i,3.5) -- (\i,3.8);
  \draw[gray] (\i,0) -- (\i,-0.3);
  }
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
4

With PSTricks,

\documentclass{article}
\usepackage{pstricks,multido}

\begin{document}
\begin{pspicture}(-4.5,-4.5)(4.5,4.5)
\multido{\ix=-4+1}{9}
{
    \psline(\ix,4.5)(\ix,-4.5)
    \psline(4.5,\ix)(-4.5,\ix)
}
\end{pspicture}
\end{document}

enter image description here