3

I want pgfplot add a grid with absolute coordinate automatically, for example, below image original pdf is 1.17x1.19in, so I want x is (0,1.17in) and y is (0,1.19in).

Now I hardcode it to Nx/Ny, can we do it by detect the image file automatically, then I no need to change it if image changed.

After update according to reply, it works fine now.

The MWE now is:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}

\def\imgfilename{tiger}
\def\u{72.26999} % 28.4526 -> pt to cm; 72.26999 -> pt to inch
\newbox\tempfig \newlength\tempwidth \newlength\tempheight
\setbox\tempfig=\hbox{{\includegraphics{\imgfilename}}}
\tempwidth\wd\tempfig \tempheight\ht\tempfig

\pgfmathparse{\tempwidth/\u}  \pgfmathsetmacro\Nx{\pgfmathresult}
\pgfmathparse{\tempheight/\u} \pgfmathsetmacro\Ny{\pgfmathresult}

\begin{tikzpicture}
    \begin{axis}[
            grid=both,minor tick num=2,major grid style={gray!80},
            width=10cm,
            scale only axis,
            enlargelimits=false,
        axis on top]
        \addplot graphics [xmin=0,xmax=\Nx,ymin=0,ymax=\Ny,
            includegraphics={},
        ] {\imgfilename};
    \end{axis}
    \draw(current bounding box.north east)node[above left]{$(\Nx\times\Ny\ in)$};
\end{tikzpicture}

\end{document}

The output is:

cute tiger

Original tiger.png is:

cute tiger

Beatlej
  • 1,723
  • refer http://tex.stackexchange.com/questions/8260/what-are-the-various-units-ex-em-in-pt-bp-dd-pc-expressed-in-mm for pt to cm convertion – Beatlej Jan 08 '15 at 03:24

1 Answers1

2

See How do I get the exact dimension of a picture in LaTeX? and Getting length as number? for more details.

\documentclass{article}
    \usepackage{tikz,pgfplots}
    \usetikzlibrary{external}\tikzexternalize
\begin{document}
    \foreach\n in{0,...,4}{
        \tikz\draw[ball color=white,rotate=30](0,0)rectangle(1+\n,2+\n*\n);
    }
    \newbox\tempfig \newlength\tempwidth \newlength\tempheight
    \foreach\n in{0,...,4}{
        \setbox\tempfig\hbox{\includegraphics{\jobname-figure\n.pdf}}
        \tempwidth\wd\tempfig \tempheight\ht\tempfig
        \tikzset{external/export next=false}
        \begin{tikzpicture}
            \pgfmathsetmacro\Nx{\tempwidth} \pgfmathsetmacro\Ny{\tempheight}
            \begin{axis}[grid=both,enlargelimits=false,axis on top]
                \addplot graphics[xmin=0,xmax=\Nx,ymin=0,ymax=\Ny]{\jobname-figure\n.pdf};
            \end{axis}
            \draw(current bounding box.north east)node[above left]{$(\Nx\times\Ny)$};
        \end{tikzpicture}
    }
\end{document}
Symbol 1
  • 36,855
  • Thanks, after change code according to your example, \Nx seems fine but \Ny is 7.5. what's the unit of the value? – Beatlej Jan 07 '15 at 18:08
  • Should be pt. You can write \pgfmathsetmacro\Ny{\tempheight*72.27} to convert it. – Symbol 1 Jan 08 '15 at 00:33
  • Thanks, it works fine now as updated in question. root cause of the Ny issue is "\usetikzlibrary{external}\tikzexternalize", after remove this line, it works fine. What's the purpose of this line? why it doesn't work on MacOSX? – Beatlej Jan 08 '15 at 03:21