3

This question already has an answer here:

When creating tikz pictures I quite often think that it would be quite useful to display a coordinate grid like this Grid in the background of my picture in order to align certain points, lines, etc.

I found this question in which plenty of ways were described for drawing a grid but each of those need an explicit specification on how big the grid has to be.
However I want the grid to automatically span the whole image background without having to adjust its size whenever I add another point to the image.

After digging through the manual I found the backgrounds library which has an option (gridded) that almost fits my needs. The only thing missing are the coordinate labels.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}

    \begin{tikzpicture}[gridded]
        \draw[red] (-1,0.7) -- (5,-2.3);
    \end{tikzpicture}

\end{document}

produces

Compiled output of the MWE

My question is: How can I add the coordinate labels to this grid?
If someone can tell me how to get the image boundaries - that is the rectangle containing the current image - it should be rather trivial to add the coordinate labels...

marked as duplicate by Raven, Community Dec 30 '18 at 14:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • A similar question here might be useful. I tried it out-- it's working, but the download link is not available. So have to copy-paste the preamble to your tex (or a separate) file (and include it). – Partha D. Dec 30 '18 at 13:49
  • @ParthaD.Maybe I am missing something but all the answers require a defined width for the grid when creating it. This question is specifically about not needing/knowing that... – Raven Dec 30 '18 at 13:52
  • 1
    Sorry, I provided the wrong link previously. Here's the correct one. – Partha D. Dec 30 '18 at 14:09
3

Just pasting @Martin Scharrer's preamble to your example and aligning the [gridded] option to [show grid = true]

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
% \usetikzlibrary{showgrid}

\makeatletter
\newif\if@showgrid@grid
\newif\if@showgrid@left
\newif\if@showgrid@right
\newif\if@showgrid@below
\newif\if@showgrid@above
\tikzset{%
    every show grid/.style={},
    show grid/.style={execute at end picture={\@showgrid{grid=true,#1}}},%
    show grid/.default={true},
    show grid/.cd,
    labels/.style={font={\sffamily\small},help lines},
    xlabels/.style={},
    ylabels/.style={},
    keep bb/.code={\useasboundingbox (current bounding box.south west) rectangle (current bounding box.north west);},
    true/.style={left,below},
    false/.style={left=false,right=false,above=false,below=false,grid=false},
    none/.style={left=false,right=false,above=false,below=false},
    all/.style={left=true,right=true,above=true,below=true},
    grid/.is if=@showgrid@grid,
    left/.is if=@showgrid@left,
    right/.is if=@showgrid@right,
    below/.is if=@showgrid@below,
    above/.is if=@showgrid@above,
    false,
}

\def\@showgrid#1{%
    \begin{scope}[every show grid,show grid/.cd,#1]
    \if@showgrid@grid
    \begin{pgfonlayer}{background}
    \draw [help lines]
        (current bounding box.south west) grid
        (current bounding box.north east);
%
    \pgfpointxy{1}{1}%
    \edef\xs{\the\pgf@x}%
    \edef\ys{\the\pgf@y}%
    \pgfpointanchor{current bounding box}{south west}
    \edef\xa{\the\pgf@x}%
    \edef\ya{\the\pgf@y}%
    \pgfpointanchor{current bounding box}{north east}
    \edef\xb{\the\pgf@x}%
    \edef\yb{\the\pgf@y}%
    \pgfmathtruncatemacro\xbeg{ceil(\xa/\xs)}
    \pgfmathtruncatemacro\xend{floor(\xb/\xs)}
    \if@showgrid@below
    \foreach \X in {\xbeg,...,\xend} {
        \node [below,show grid/labels,show grid/xlabels] at (\X,\ya) {\X};
    }
    \fi
    \if@showgrid@above
    \foreach \X in {\xbeg,...,\xend} {
        \node [above,show grid/labels,show grid/xlabels] at (\X,\yb) {\X};
    }
    \fi
    \pgfmathtruncatemacro\ybeg{ceil(\ya/\ys)}
    \pgfmathtruncatemacro\yend{floor(\yb/\ys)}
    \if@showgrid@left
    \foreach \Y in {\ybeg,...,\yend} {
        \node [left,show grid/labels,show grid/ylabels] at (\xa,\Y) {\Y};
    }
    \fi
    \if@showgrid@right
    \foreach \Y in {\ybeg,...,\yend} {
        \node [right,show grid/labels,show grid/ylabels] at (\xb,\Y) {\Y};
    }
    \fi
    \end{pgfonlayer}
    \fi
    \end{scope}
}
\makeatother
%\tikzset{showgrid} % would enable it globally
\tikzset{every show grid/.style={show grid/keep bb}}%  Keep the original bounding box!


\begin{document}

    \begin{tikzpicture}[show grid = true]
        \draw[red] (-1,0.7) -- (5,-2.3);
    \end{tikzpicture}

\end{document}

which produces--

labelled grids

  • Yes that is exactly what I was looking for. Thank you! – Raven Dec 30 '18 at 14:33

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