2

This is a request for an enhancement to this fairly amazing answer by Loop Space: How can I show coordinates by grid in TikZ automatically?.

This answer gives a recipe for placing a grid on a page with coordinates on the left and the bottom. I want an enhanced recipe, which has coordinates on the right and the top, as well. I'd prefer an answer that builds directly on Loop Space's answer, but I suppose other choices are ok too.

My reason for asking this question is that I want to use a coordinate grid for annotations. And having the coordinates on both sides would make annotations easier, because the coordinates would be more easily visible. See, for example, my answer to "Text annotations and image additions to PDF file using free software".

3

Yes, with minimal surgery.

\documentclass[tikz,border=3.14mm]{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,
    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 (-2,-2) to[grid with coordinates] (7,4);
\end{tikzpicture}
\end{document}

enter image description here

You could also add is if keys to make things a bit more easy to customize. This yields the same output as above:

\documentclass[tikz,border=3.14mm]{standalone}
\makeatletter
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\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={
    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}
        {\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}
      }
    }
  },
  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 (-2,-2) to[grid with coordinates,ticks on top=true,ticks right=true] (7,4);
\end{tikzpicture}
\end{document}

but if you do, say

\begin{tikzpicture}
\draw (-2,-2) to[grid with coordinates,ticks on top=true,ticks right=true,
ticks at bottom=false] (7,4);
\end{tikzpicture}

enter image description here

Initially, the ticks left and at the bottom are true while the others are false such that you reproduce the output of the original answer if you do not add any of these keys.

ADDENDUM: A version without \pgfextra. The \GridCore code is entirely from this cool answer.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\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 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] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}
2

A PSTricks solution just for fun purposes. Someone stole the 0 labels on the horizontal axes.

\documentclass[pstricks,border=1cm,12pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\pspicture[xticksize=-5 5,yticksize=-5 5,subticks=5,subticksize=1,axesstyle=none](-5,-5)(5,5)
    \psaxes(0,0)(-5,-5)(5,5)
    \psaxes[xlabelPos=top,ylabelPos=right](0,0)(-5,-5)(5,5)
\endpspicture
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.