Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % https://tex.stackexchange.com/questions/39553/
- % usage:
- % \begin{tikzpicture}
- % \draw (-2,-2) to[grid with coordinates] (7,4);
- % \end{tikzpicture}
- \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,font=\scriptsize] at (\x,\grd@ya) {$\pgfmathprintnumber{\x}$};
- \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
- \node[anchor=east,font=\scriptsize] at (\grd@xa,\y) {$\pgfmathprintnumber{\y}$};
- }
- }
- },
- minor help lines/.style={
- help lines,
- step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step},
- color=gray!30,
- },
- major help lines/.style={
- help lines,
- line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
- step=\pgfkeysvalueof{/tikz/grid with coordinates/major step},
- color=gray!30,
- },
- grid with coordinates/.cd,
- minor step/.initial=.2,
- major step/.initial=1,
- major line width/.initial=0.5pt,
- }
- \makeatother
Advertisement
Add Comment
Please, Sign In to add comment