8

As it's my first question, I want to say hello first!

I am using [grid with coordinates] code from Andrew Stacey answer (link) for How can I show coordinates by grid in TikZ automatically?.

I modified it a bit and tried to acquire different major/minor lines xstep and ystep, I ended with:

\documentclass{article}

%\usepackage{pgfplots}
\usepackage{tikz,pgfplots}

\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 x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \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,
    gray,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1,
  major step y/.initial=1,
  major line width/.initial=1pt,
}
\makeatother

What I need now is to specify xstep and ystep in actual tikzpicture, e.g. in code below I want to pass xstep and ystep (for minor and major lines separately) so that Fig. B gets major lines xstep =0.2.

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=1,yscale=2]
    \draw (0,0) to[grid with coordinates] (5,1);    
    \end{tikzpicture}
    \caption{A}\label{fig:sphere}
\end{figure}    

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=5,yscale=2]
    \draw (0,0) to[grid with coordinates] (1,1); %major step x =0.2
    \end{tikzpicture}
    \caption{B}\label{fig:sphere}
\end{figure}

\end{document}

I can't guess how to pass the parameters.

m-dz
  • 297

1 Answers1

6

You can use the option

grid with coordinates/major step x=<value>

A complete example:

\documentclass{article}

%\usepackage{pgfplots}
\usepackage{tikz,pgfplots}

\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 x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \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,
    gray,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1,
  major step y/.initial=1,
  major line width/.initial=1pt,
}
\makeatother

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=1,yscale=2]
    \draw (0,0) to[grid with coordinates] (5,1);    
    \end{tikzpicture}
    \caption{A}\label{fig:sphere}
\end{figure}    

\begin{figure}
    \centering
    \begin{tikzpicture}[xscale=5,yscale=2,grid with coordinates/major step x=0.2]
    \draw (0,0) to[grid with coordinates] (1,1); %major step x =0.2
    \end{tikzpicture}
    \caption{B}\label{fig:sphere}
\end{figure}

\end{document}

enter image description here

Analogously, you can change major step y, minor step x, minor step y, and major line width.

Gonzalo Medina
  • 505,128
  • I knew it's something simple, thank a lot! Is there some place (e.g. printed book, PDF, something...) you recommend to start creating/editing such code not with trial and error method? – m-dz Apr 16 '13 at 23:42
  • 1
    @MateuszDziedzic You're welcome. I guess you already know the pgfmanual for the basics? Other sources might be the sources of PGF/TikZ itself. – Gonzalo Medina Apr 16 '13 at 23:46
  • I'll look closer to pgfmanual, I somehow missed it. – m-dz Apr 17 '13 at 18:08