Guest User

tikz grid!

a guest
Jun 25th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. % https://tex.stackexchange.com/questions/39553/
  2. % usage:
  3. % \begin{tikzpicture}
  4. % \draw (-2,-2) to[grid with coordinates] (7,4);
  5. % \end{tikzpicture}
  6.  
  7. \makeatletter
  8. \def\grd@save@target#1{%
  9. \def\grd@target{#1}}
  10. \def\grd@save@start#1{%
  11. \def\grd@start{#1}}
  12. \tikzset{
  13. grid with coordinates/.style={
  14. to path={%
  15. \pgfextra{%
  16. \edef\grd@@target{(\tikztotarget)}%
  17. \tikz@scan@one@point\grd@save@target\grd@@target\relax
  18. \edef\grd@@start{(\tikztostart)}%
  19. \tikz@scan@one@point\grd@save@start\grd@@start\relax
  20. \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
  21. \draw[major help lines] (\tikztostart) grid (\tikztotarget);
  22. \grd@start
  23. \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
  24. \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
  25. \grd@target
  26. \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
  27. \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
  28. \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
  29. \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
  30. \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
  31. \node[anchor=north,font=\scriptsize] at (\x,\grd@ya) {$\pgfmathprintnumber{\x}$};
  32. \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
  33. \node[anchor=east,font=\scriptsize] at (\grd@xa,\y) {$\pgfmathprintnumber{\y}$};
  34. }
  35. }
  36. },
  37. minor help lines/.style={
  38. help lines,
  39. step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step},
  40. color=gray!30,
  41. },
  42. major help lines/.style={
  43. help lines,
  44. line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
  45. step=\pgfkeysvalueof{/tikz/grid with coordinates/major step},
  46. color=gray!30,
  47. },
  48. grid with coordinates/.cd,
  49. minor step/.initial=.2,
  50. major step/.initial=1,
  51. major line width/.initial=0.5pt,
  52. }
  53. \makeatother
Advertisement
Add Comment
Please, Sign In to add comment