12

I'm trying to place a grid over an image to help me place other object where I want on the image. But, I'm having difficulty getting tikz to show coordinates as I wish.

Here's my MWE:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}

\noindent 
\begin{tikzpicture}
  \node[anchor=south west,inner sep=0pt] (image) at (0,0) {\includegraphics[width=4.5in]{example-image-a}};
  \begin{scope}[x={(image.center |- {(0,0)})},y={(image.center -| {(0,0)})}]
      \draw[help lines,xstep=0.1,ystep=0.1,yellow!20] (0,0) grid (2,2);
      \pgfkeys{/pgf/number format/precision=1}
      \foreach \x in {0,1,...,20} 
        { 
          \pgfmathparse{\x/10};
          \edef\mylabel{\pgfmathresult}
          \node [anchor=north] at (\x/10,0) {\rotatebox{-90}{\mylabel}}; 
        }
  \end{scope}
  \draw[line width=4pt,red] (image.south west) rectangle (image.north east);
\end{tikzpicture}

\end{document}

enter image description here

What I would like is to have the x-values of 0.0, 0.1, 0.2, 0.3,... along the bottom border of the image.

I have tried the following too:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}

\noindent 
\begin{tikzpicture}
  \node[anchor=south west,inner sep=0pt] (image) at (0,0) {\includegraphics[width=4.5in]{example-image-a}};
  \begin{scope}[x={(image.center |- {(0,0)})},y={(image.center -| {(0,0)})}]
      \draw[help lines,xstep=0.1,ystep=0.1,yellow!20] (0,0) grid (2,2);
      \pgfkeys{/pgf/number format/precision=1}
      \foreach \x in {0,0.1,...,2} 
        { 
          \node [anchor=north] at (\x,0) {\rotatebox{-90}{\x}}; 
        }
  \end{scope}
  \draw[line width=4pt,red] (image.south west) rectangle (image.north east);
\end{tikzpicture}

\end{document}

which results in

enter image description here

This time the x-values are not only off but also they're not counting through and showing 2.0.

Additionally I've tried

\node [anchor=north] at (\x,0) {\rotatebox{-90}{\pgfmathprintnumberto[precision=1]{\x}{\myresult}\myresult}}; 

If I could round to a certain decimal place or truncate in either scenario, I'd be happy. precision isn't doing what I expected though.

Any suggestions?

1
  • 2
    In the last part of your question \pgfmathprintnumber[fixed,precision=1]{\x} would do it.
    – percusse
    Sep 7, 2013 at 7:38

1 Answer 1

11

You need to process the value \x using one of the PGF math functions and then set \pgfmathresult:

enter image description here

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}

\noindent 
\begin{tikzpicture}
  \node[anchor=south west,inner sep=0pt] (image) at (0,0) {\includegraphics[width=4.5in]{example-image-a}};
  \begin{scope}[x={(image.center |- {(0,0)})},y={(image.center -| {(0,0)})}]
      \draw[help lines,xstep=0.1,ystep=0.1,yellow!20] (0,0) grid (2,2);
      \pgfkeys{/pgf/number format/precision=1}
      \foreach \x in {0,0.1,...,2} 
        { 
          \node [anchor=north] at (\x,0) {\rotatebox{-90}{\pgfmathroundtozerofill{\x}\pgfmathresult}}; 
        }
  \end{scope}
  \draw[line width=4pt,red] (image.south west) rectangle (image.north east);
\end{tikzpicture}

\end{document}

Above I've used \pgfmathroundtozerofill{\x}\pgfmathresult} which processed \x and pads it to 1 zero.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .