Here's a solution that uses to path
s to execute arbitrary code in the guise of a simple command. If the preamble were hidden away in a package, the invocation would just be:
\begin{tikzpicture}
\draw (-2,-2) to[grid with coordinates] (7,4);
\end{tikzpicture}
The sneaky trick is to use the fact that the to path
declaration can contain arbitrary code via a \pgfextra
command. With that, it's just a matter of grabbing the starting and ending coordinates (which are encoded as \tikztostart
and \tikztotarget
) and then using your original code (which I assume you were reasonably happy with) as the code to be executed.
With the preamble, the code is:
\documentclass{article}
%\url{}
\usepackage{tikz}
\makeatletter
\def\grd@save@target#1{%
\def\grd@target{#1}}
\def\grd@save@start#1{%
\def\grd@start{#1}}
\tikzset{
grid with coordinates/.style={
to path={%
\pgfextra{%
\edef\grd@@target{(\tikztotarget)}%
\tikz@scan@one@point\grd@save@target\grd@@target\relax
\edef\grd@@start{(\tikztostart)}%
\tikz@scan@one@point\grd@save@start\grd@@start\relax
\draw[minor help lines] (\tikztostart) grid (\tikztotarget);
\draw[major help lines] (\tikztostart) grid (\tikztotarget);
\grd@start
\pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
\pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
\grd@target
\pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
\pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
\pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
\pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
\foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
\node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
\foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
\node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
}
}
},
minor help lines/.style={
help lines,
step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
},
major help lines/.style={
help lines,
line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
step=\pgfkeysvalueof{/tikz/grid with coordinates/major step}
},
grid with coordinates/.cd,
minor step/.initial=.2,
major step/.initial=1,
major line width/.initial=2pt,
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw[help lines,step=.2] (-2,-2) grid (7,4);
\draw[help lines,line width=.6pt,step=1] (-2,-2) grid (7,4);
\foreach \x in {-2,-1,0,1,2,3,4,5,6,7}
\node[anchor=north] at (\x,-2) {\x};
\foreach \y in {-2,-1,0,1,2,3,4}
\node[anchor=east] at (-2,\y) {\y};
\end{tikzpicture}
\begin{tikzpicture}
\draw (-2,-2) to[grid with coordinates] (7,4);
\end{tikzpicture}
\end{document}
(Your original grid is included first for comparison. I thickened the major lines on my version, by the way.)
The extra bits, and the stuff with the @
s, is for processing the coordinates to figure out where to start and end the labelling. The assumption is that we start at the first coordinate and work our way up according to the major step separation to the second coordinate. So we need to process these coordinates to extract that information. This means that the coordinates to the command can be any TikZ coordinate: we use \tikz@scan@one@point
which is how TikZ itself parses coordinates.
One thing to note is the use of \pgfmathprintnumber
to ensure that the labels are printed nicely.
The result is as follows:

tikz-grid
. – Marco Daniel Dec 29 '11 at 11:02best-practice
isn't the correct tag? – Marco Daniel Dec 29 '11 at 12:56