# How to create integer labels on TikZ grid when we don't know the grid size at compile time?

I want to put integer labels on the grid. The grid size depends on the image loaded.

The real scenario is to vectorize a JPG image but I need a grid label for navigation as follows

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}

\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}

\begin{document}
\begin{tikzpicture}
\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);

% I want to put integer labels for navigation purpose. The following code cannot be compiled!
%\foreach \x in {-0.5\wd\TempBox,1,...,0.5\wd\TempBox} { \node [anchor=north] at (\x,0) {\x}; }
%\foreach \y in {-0.5\ht\TempBox,1,...,0.5\ht\TempBox} { \node [anchor=east] at (0,\y) {\y}; }

\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}


The following figure is the original image to complete the MWE.

How to create integer labels on TikZ grid?

I might need something like floor or ceiling functions here.

-
Do you see tex.stackexchange.com/questions/9559/… ? –  Alain Matthes Mar 8 '12 at 16:46
@Altermundus: I have seen it before but which one can be applied here? –  kiss my armpit Mar 8 '12 at 16:49

You need to do the loop with integer values, and compute the positions based on these integer values. Below I have used \pgfmathsetmacro to compute the step size in each direction, and \pgfmathtruncatemacro to compute the integer start and end points for the loops:

## Code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}

\begin{document}
\begin{tikzpicture}
% Compute step size and number of grid lines in each direction:
\pgfmathsetmacro{\NumberOfXGrid}{\wd\TempBox/1.0cm}%
\pgfmathtruncatemacro{\XGridStart}{-0.5*\NumberOfXGrid}%
\pgfmathtruncatemacro{\XGridEnd}{0.5*\NumberOfXGrid}%

\pgfmathsetmacro{\NumberOfYGrid}{\ht\TempBox/1.0cm}%
\pgfmathtruncatemacro{\YGridStart}{-0.5*\NumberOfYGrid}%
\pgfmathtruncatemacro{\YGridEnd}{0.5*\NumberOfYGrid}%

\foreach \x in {\XGridStart,...,\XGridEnd} {
\node [anchor=north] at (\x,-0.5\ht\TempBox-1ex) {$\x$};
}
\foreach \y in {\YGridStart,...,\YGridEnd} {
\node [anchor=east] at (-0.5\wd\TempBox-0.5em,\y) {$\y$};
}

\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);

\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}

-
Please don't use hard coded literals {-5,...,5}. :D –  kiss my armpit Mar 8 '12 at 16:51
@DamienWalters: Good point. Have updated solution –  Peter Grill Mar 8 '12 at 17:11
What are \x/\NumberOfXGrid*\wd\TempBox and \y/\NumberOfYGrid*\ht\TempBox for? :-) Why don't simply use \x and \y instead? –  kiss my armpit Mar 8 '12 at 21:25
@DamienWalters: Yep, no need... Updated MWE to reflect this. Thanks for catching it. –  Peter Grill Mar 9 '12 at 0:07
@DamienWalters: The \$ you are referring to is not the usual math mode indicator, but instead part of the \usetikzlibrary{calc} used for coordinate calculations. However, this was only necessary in earlier versions of this code and is not needed now, so have removed it. Thanks for looking thru the code in such detail. –  Peter Grill Mar 10 '12 at 1:23

I updated the onimage package form the "From Answers to Packages" project to support absolutely spaced help lines. You can download the .dtx from Lauchpad. Then run (pdf)latex on it to obtain the package (.sty) file and documentation.

To get grid lines spaced 1cm apart, you can use it as follows:

\documentclass{article}
\usepackage{onimage}

\begin{document}

\begin{tikzonimage}[width=0.8\textwidth]{some_image.jpg}
[tsx/show help lines=1cm,tsx/scale cs=none]
% Some optional TikZ drawing commands here.
\end{tikzonimage}

\end{document}


The first optional argument of tikzonimage is passed to \includegraphics, then comes the file name and then TikZ options.

-
I have a feature request for onimage: Could you add a coordinate system which reflects the size (in cm) of the original, unscaled image? This would allow people to get coordinates with "lineal und bleistift" from a printout. –  Ulrike Fischer Nov 21 '13 at 18:40
@UlrikeFischer: I don't quite understand what you mean by "size of the original, unscaled image"? Is that size in pixels/ppi of ?? –  Caramdir Nov 21 '13 at 21:31
I mean the values you would get with scale=none and an unscaled image. In your example above the values change if one change the width. It would also enough if one could set the maximum values for scale=both to something different to 1. –  Ulrike Fischer Nov 21 '13 at 21:42
My aim is a coordinate system which reflects the "natural" size of the graphic and is not (if possible) affected by graphics manipulation. In the mean time I realized that this mean that I will have to intercept keys like trim and angle so I will have to write my own environment based on tikzonimage. –  Ulrike Fischer Nov 22 '13 at 8:10
@UlrikeFischer: I'll have a look at it (when I have some time -- I haven't touched or used tikzonimage for a while). –  Caramdir Nov 22 '13 at 15:32

Here is another version with numbers are on the picture rather than axes with slightly different code than Peter's.

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\newsavebox\TempBox
\savebox\TempBox{\includegraphics{batman.jpg}}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0] at (0,0) {\usebox\TempBox};
\draw[white](-0.5\wd\TempBox,-0.5\ht\TempBox) grid (0.5\wd\TempBox,0.5\ht\TempBox);
\pgfmathparse{int(-0.5\wd\TempBox/28.4)}%
\edef\ww{\pgfmathresult}
\pgfmathparse{int(-0.5\ht\TempBox/28.4)}%
\edef\hh{\pgfmathresult}
% I want to put integer labels for navigation purpose. The following code cannot be compiled!
\foreach \x in {\ww,...,-\ww} { \node [white,anchor=north] at (\x,0) {\x}; }
\foreach \y in {\hh,...,-\hh} { \node [white,anchor=east] at (0,\y) {\y}; }

\draw[line width=2pt,blue] (0,0) ellipse (6.3 and 3.4);
\draw[line width=2pt,blue] (0,0) ellipse (6.9 and 4.0);
\end{tikzpicture}
\end{document}


-
What is 28.4? –  kiss my armpit Mar 8 '12 at 21:09
@DamienWalters Just a conversion from ptto cm. –  percusse Mar 8 '12 at 22:39
More accurate ratio of cm to pt : \usepackage[nomessages]{fp}\edef\PT{\number\dimexpr 1pt}\edef\CM{\number\dimexpr 1cm}\FPeval\CMtoPT{\CM/\PT}. –  kiss my armpit Oct 15 '14 at 18:04