3

This is a request for a slightly enhanced version of an answer to the question: "Grid with coordinates on all sides?", which is itself an enhancement of a previous answer by @Loopspace.

I'd like a colored grid version of the first version of the code, at the beginning of the question. The linked answer included three versions for good measure. I expect this is easy for someone who is familiar with the relevant parts of PGF/TikZ. For concreteness, let's say light/leaf green. And I'd like the entire grid in green, please, except the numbers.

Motivation: I use this grid code for annotating PDF forms. I'd like to try the grid in a color different from black, and green in particular, because I think green will contrast with the actual form, which is usually black and white

Thanks.

Faheem Mitha
  • 7,778
  • Add color=green, (or any other colo of your choice) to minor help lines/.style= and major help lines/.style= to color all lines of the grid. – leandriis Aug 31 '19 at 20:44
  • @leandriis Ok. I didn't realise it was so easy. Do you think I should delete the question? Alan Munn said the same thing in chat. Or would you like to write an answer? – Faheem Mitha Aug 31 '19 at 20:49

3 Answers3

6

You can add colour to the style directly in its definition, or append to the style when you produce the grid. In this code I show the latter solution. I've removed the tikz option as a global option, so that I can load xcolor with the dvipsnames option for those named colours (load before tikz).

\documentclass[border=3.14mm]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter% code from https://tex.stackexchange.com/a/469213/194703
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\def\GridCore{\edef\grd@@target{(\tikzinputsegmentlast)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikzinputsegmentfirst)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \draw[major help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \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}
        {\ifticksB
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \fi
        \ifticksT
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        \fi
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\ifticksL
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \fi
        \ifticksR
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};
        \fi}
}  
\newif\ifticksL  
\newif\ifticksR  
\newif\ifticksT  
\newif\ifticksB  
\tikzset{ticks left/.is if=ticksL,
ticks right/.is if=ticksR,
ticks on top/.is if=ticksT,
ticks at bottom/.is if=ticksB,
ticks left=true,
  ticks at bottom=true,
  ticks right=false,
  ticks on top=false,
  grid with coordinates/.style={
  decorate,decoration={show path construction,
  lineto code={\GridCore
    }}
  },
  minor help lines/.style={
    help lines,
    step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
  },
  major help lines/.style={
    help lines, line cap=rect,
    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[grid with coordinates,ticks on top=true,ticks right=true,major help lines/.append style=LimeGreen,minor help lines/.append style=LimeGreen ] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}

enter image description here

Alan Munn
  • 218,180
  • Can I suggest you to add line cap=rect in the major help lines style, so the corners at coordinates (-2,-2), (7,-2), (-2,4) and (7,4) are better (magnify your picture at these 4 corners to see the flaw)? – quark67 Aug 31 '19 at 22:15
  • @quark67 Thanks! Done. – Alan Munn Sep 01 '19 at 00:30
6

This is an alternative that is based on the version without \pgfextra, the rationale being that the pgfmanual v.3.1.4 says about \pgfextra on p. 167

Note that this operation should only be used by real experts and should only be used deep inside clever macros, not on normal paths.

It is also an attempt to "clean up" the keys in such a way that everything is under the /tikz/grid with coordinates/ directory. It has now the key all help lines which appends some style to both the minor and major help lines. Also the syntax has been attempted to clean up, inspired by this discussion: now you only need to say ticks/.list={top,left} and no longer say true several times.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter% https://tex.stackexchange.com/a/469213/194703
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\def\GridCore{\edef\grd@@target{(\tikzinputsegmentlast)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikzinputsegmentfirst)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[grid with coordinates/minor help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
        \draw[grid with coordinates/major help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast)
        (\tikzinputsegmentfirst) rectangle (\tikzinputsegmentlast);
        \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}
        {\ifticksB
        \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
        \fi
        \ifticksT
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        \fi
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\ifticksL
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \fi
        \ifticksR
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};
        \fi}
}  
\newif\ifticksL  
\newif\ifticksR  
\newif\ifticksT  
\newif\ifticksB  
\tikzset{grid with coordinates/.style={
  /utils/exec=\tikzset{grid with coordinates/.cd,ticks=none,#1},
  decorate,decoration={show path construction,
  lineto code={\GridCore
    }}
  },
  grid with coordinates/.cd,
  ticks/.is choice,
  ticks/left/.code=\ticksLtrue,
  ticks/right/.code=\ticksRtrue,
  ticks/top/.code=\ticksTtrue,
  ticks/bottom/.code=\ticksBtrue,
  ticks/none/.code=\ticksLfalse\ticksRfalse\ticksTfalse\ticksBfalse,
  ticks/all/.code=\ticksLtrue\ticksRtrue\ticksTtrue\ticksBtrue,
  minor step/.initial=.2,
  major step/.initial=1,
  major line width/.initial=2pt,
  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}
  },
 all help lines/.code={\tikzset{grid with coordinates/.cd,
    major help lines/.append style={#1},
    minor help lines/.append style={#1}
    }} 
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw[grid with coordinates={ticks/.list={top,left},
 all help lines={draw=green!60!black},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}

\begin{tikzpicture}
\draw[grid with coordinates={ticks=all,
 all help lines={draw=blue},minor help lines/.append style={dashed},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}

\begin{tikzpicture}
\draw[grid with coordinates={ticks=none,
 all help lines={draw=red},minor help lines/.append style={densely dotted},
 }] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks. I almost feel some version of this should go into some utility package. – Faheem Mitha Sep 01 '19 at 05:34
  • I used the code without \pgfextra as the base for my answer too. – Alan Munn Sep 01 '19 at 13:51
  • @AlanMunn I didn't say you didn't. ;-) –  Sep 01 '19 at 13:52
  • @Schrödinger'scat What is the problem with GitHub? It seems straightforward enough. I'm not really a Git fan, but one can do simple things with it. No worse then SE, really. – Faheem Mitha Sep 01 '19 at 18:12
  • @Schrödinger'scat Not being a Git user, I haven't done much of anything with GitHub. But I don't see serious difficulties in doing so. – Faheem Mitha Sep 01 '19 at 18:58
  • @FaheemMita Yes, it is certainly my bad. Nonetheless, I cannot perform even the simplest operations. (I successfully worked with cvs and plain git, but in the context of GitHub even the simplest commands from the tutorials lead to cryptic error messages and nothing of the sort I was used to.) –  Sep 01 '19 at 19:32
5

The color of the grid lines can be adjusted by adding color=green, to minor help lines/.style= and major help lines/.style=.

Full example:

\documentclass[tikz,border=3.14mm,svgnames]{standalone}


\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}};
        \node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
        }
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        {\node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
        \node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};}
      }
    }
  },
  minor help lines/.style={
    help lines,
    color=green,
    step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
  },
  major help lines/.style={
    help lines,
    color=green,
    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 (-2,-2) to[grid with coordinates] (7,4);
\end{tikzpicture}
\end{document}

enter image description here

leandriis
  • 62,593
  • Can I suggest you to add line cap=rect in the major help lines style, so the corners at coordinates (-2,-2), (7,-2), (-2,4) and (7,4) are better (magnify your picture at these 4 corners to see the flaw)? – quark67 Aug 31 '19 at 22:17