8

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

enter image description here

\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.

enter image description here

How to create integer labels on TikZ grid?

I might need something like floor or ceiling functions here.

2

3 Answers 3

7

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:

enter image description here

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}
6
  • Please don't use hard coded literals {-5,...,5}. :D Mar 8, 2012 at 16:51
  • @DamienWalters: Good point. Have updated solution Mar 8, 2012 at 17:11
  • What are \x/\NumberOfXGrid*\wd\TempBox and \y/\NumberOfYGrid*\ht\TempBox for? :-) Why don't simply use \x and \y instead? Mar 8, 2012 at 21:25
  • @DamienWalters: Yep, no need... Updated MWE to reflect this. Thanks for catching it. Mar 9, 2012 at 0:07
  • 3
    @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. Mar 10, 2012 at 1:23
8

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.

example

5
  • 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. Nov 21, 2013 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, 2013 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. Nov 21, 2013 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. Nov 22, 2013 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, 2013 at 15:32
6

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}

enter image description here

3
  • What is 28.4? Mar 8, 2012 at 21:09
  • @DamienWalters Just a conversion from ptto cm.
    – percusse
    Mar 8, 2012 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}. Oct 15, 2014 at 18:04

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.