Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to TeX.SE. –  Peter Grill Apr 16 '13 at 22:26
    
Something like \draw (0,0) to[grid with coordinates,/tikz/grid with coordinates/major step x=.5] (5,1); perhaps ... –  Alain Matthes Apr 16 '13 at 22:28
    
Hello to you too! –  percusse Apr 16 '13 at 23:03
    
Thanks for welcome! I hope I will not have to visit TeX.SE often ;] –  Mateusz Dziedzic Apr 16 '13 at 23:38
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
    
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? –  Mateusz Dziedzic Apr 16 '13 at 23:42
    
@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. –  Mateusz Dziedzic Apr 17 '13 at 18:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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