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.

I found that the step of the grid is not relative to where the grid is called, but relative to the origin. Therefore in a figure like this one the grid gets shifted with respect the multiple axis. Is there any trick to make it relative to the place it has been called or it is needed to calculate the shift \pgfmathsetmacro{\myshiftX}{calculations} and supply it as a parameter \draw[step=0.25,xshift=\myshiftX]

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[node distance=3mm, scale=1.2]
\def\lengthAxis{1.1}
\def\AxisSpace{2.4}

\foreach \elem [count=\i] in {1,2,3,4,5,6,7,8}{
\pgfmathsetmacro{\x}{\AxisSpace*mod(\i-1,4)}
\pgfmathsetmacro{\y}{-1*\AxisSpace*int((\i-1)/4)}

\draw[step=0.25,gray,very thin] (\x,\y) +(-1.05,-1.05) grid +(1.05,1.05);

\path (\x,\y)   + (          \lengthAxis,                         0)    coordinate (meanI)
                        + (  -1*\lengthAxis,                          0)    coordinate (medI) 
                        + (                       0,        \lengthAxis )   coordinate (minI) 
                        + (                           0,  -1*\lengthAxis)   coordinate (maxI);

\draw[thin] (meanI) -- (medI) (minI) -- (maxI);

\draw[thin] (\x,\y) + (    1,      0)   circle [radius=1pt] node[below] {1}
                                + (   -1,      0)   circle [radius=1pt] node[above] {1}
                                + (      0,     1)  circle [radius=1pt] node[right]         {1}
                                + (        0,     -1)   circle [radius=1pt] node[left]      {1}
                                + ( 0.5,       0)   circle [radius=1pt] node[below] {0.5}
                                + ( -0.5,      0)   circle [radius=1pt] node[above] {0.5}
                                + (    0,   0.5)    circle [radius=1pt] node[right]         {0.5}
                                + (    0, -0.5) circle [radius=1pt] node[left]      {0.5};

}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
3  
The easiest might be to simply use \draw[step=0.25,gray,very thin, shift={(\x,\y)}] (-1.05,-1.05) grid (1.05,1.05); shifting the origin to (\x,\y). (By the way, there is a spurious {\scalefont{0.45} in your MWE which I have removed in my edit along with the pgfplots tag.) –  Qrrbrbirlbel Aug 28 '13 at 0:27
add comment

1 Answer

up vote 4 down vote accepted

As said in a comment, the easiest in your case might be simply to use

\draw[step=0.25,gray,very thin, shift={(\x,\y)}] (-1.05,-1.05) grid (1.05,1.05);

which shifts the whole picture about the vector (\x, \y) and simply draws a grid from (-1.05,-1.05) to (1.05,1.05).


You might also be inclined to use a \matrix here.

Code

\documentclass[tikz]{standalone}
\tikzset{
  every myGrid picture/.style={scale=1.2},
  every myGrid grid/.style={step=0.25, help lines, draw},
  every myGrid ticks/.style={radius=+1pt, draw, nodes={font=\tiny}},
  every myGrid axes/.style=draw,
  myGrid grid/.initial=1.05,
  myGrid axis/.initial=1.1,
}
\newcommand*\myGrid[1][]{%
\scope[every myGrid picture/.try, #1]
  \path[every myGrid grid/.try]
    (-\pgfkeysvalueof{/tikz/myGrid grid}, -\pgfkeysvalueof{/tikz/myGrid grid}) grid
    ( \pgfkeysvalueof{/tikz/myGrid grid},  \pgfkeysvalueof{/tikz/myGrid grid});
  \path[every myGrid ticks/.try]
    \foreach \sign/\val in {-/1, -/0.5, /0.5, /1} {
     (right:\sign\val) circle[]
       node[style/.expanded={\if\sign-above\else below\fi}] {\val}
     (up:   \sign\val) circle[]
       node[style/.expanded={\if\sign-left\else right\fi}]  {\val}
   };
  \path[every myGrid axes/.try]
   (down:\pgfkeysvalueof{/tikz/myGrid axis})--(   up:\pgfkeysvalueof{/tikz/myGrid axis})
   (left:\pgfkeysvalueof{/tikz/myGrid axis})--(right:\pgfkeysvalueof{/tikz/myGrid axis});
\endscope
}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\matrix [
  column sep={1.2*2.4cm,between origins},
  row sep={1.2*2.4cm,between origins}
]{
 \myGrid & \myGrid & \myGrid & \myGrid \\
 \myGrid & \myGrid & \myGrid & \myGrid[every myGrid grid/.append style={step=.1}] \\
};
\end{tikzpicture}

\begin{tikzpicture}\def\AxisSpace{2.4}
\foreach \elem [evaluate={\xVal=\AxisSpace*mod(\elem,4)},
                evaluate={\yVal=-\AxisSpace*int(\elem/4)}] in {0,...,7}{
  \myGrid[shift={(\xVal, \yVal)}]
}
\end{tikzpicture}
\end{document}

Output

enter image description here

enter image description here

share|improve this answer
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.