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.



        \draw[red] (-1,0.7) -- (5,-2.3);



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

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

% \usetikzlibrary{showgrid}

    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},
    keep bb/.code={\useasboundingbox (current bounding box.south west) rectangle (current bounding box.north west);},
    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,

    \begin{scope}[every show grid,show grid/.cd,#1]
    \draw [help lines]
        (current bounding box.south west) grid
        (current bounding box.north east);
    \pgfpointanchor{current bounding box}{south west}
    \pgfpointanchor{current bounding box}{north east}
    \foreach \X in {\xbeg,...,\xend} {
        \node [below,show grid/labels,show grid/xlabels] at (\X,\ya) {\X};
    \foreach \X in {\xbeg,...,\xend} {
        \node [above,show grid/labels,show grid/xlabels] at (\X,\yb) {\X};
    \foreach \Y in {\ybeg,...,\yend} {
        \node [left,show grid/labels,show grid/ylabels] at (\xa,\Y) {\Y};
    \foreach \Y in {\ybeg,...,\yend} {
        \node [right,show grid/labels,show grid/ylabels] at (\xb,\Y) {\Y};
%\tikzset{showgrid} % would enable it globally
\tikzset{every show grid/.style={show grid/keep bb}}%  Keep the original bounding box!


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


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.