# How to change vertical and horizontal unit based on a fraction of width and height of an image?

I have to overlay some texts on a PDF form. As converting the PDF form to EPS with pdftops produces unsatisfying result, I will not use PSTricks code below.

\documentclass[pstricks,border=12pt]{standalone}

\def\M{4}% columns
\def\N{4}% rows
\def\scale{1}% scale
\def\filename{example-image-a}% filename

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

{
gridcolor=yellow,
subgridcolor=gray,
subgriddiv=10,
griddots=0,
subgriddots=5,
gridwidth=0.4pt,
subgridwidth=0.2pt,
}

\psset
{
xunit=0.5\dimexpr\wd\IBox/\M,
yunit=0.5\dimexpr\ht\IBox/\N,
}

\begin{document}
\begin{pspicture}[showgrid=top](-\M,-\N)(\M,\N)
\rput(0,0){\usebox\IBox}
\rput[bl](1,1){This is a text.}
\end{pspicture}
\end{document}


The output I want to achieve looks like the following.

Instead I will use TikZ. My problem is how to set the horizontal and vertical unit in TikZ. More precisely, how to do the following in TikZ?

\psset
{
xunit=0.5\dimexpr\wd\IBox/\M,
yunit=0.5\dimexpr\ht\IBox/\N,
}


## MWE in TikZ

\documentclass[tikz,12pt,dvipsnames,border=12pt]{standalone}

\def\M{4}% columns
\def\N{4}% rows
\def\scale{1}% scale
\def\filename{example-image-a.pdf}% filename

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

\begin{document}
\begin{tikzpicture}[inner sep=0,x=0.5\wd\IBox/\M\relax,y=0.5\ht\IBox/\N\relax]
\node (image) at (0,0) {\usebox\IBox};
\node at (1,1) {some text goes here};
\draw[help lines,red,step=1](-\M,-\N) grid (\M,\N);
\end{tikzpicture}
\end{document}


DONE! Problem solved!

-
More general, how to mimic the PSTricks output with TikZ? – kiss my armpit Aug 29 '14 at 18:16
Never mimic others! ;-). BTW problem solved? – Harish Kumar Aug 30 '14 at 1:18

You don't have to convert pdf to ps, since pstricks code can be compiled with pdflatex: you just have to load pstricks with option pdf and launch the compiler with the --enable-write18 switch (MiKTeX) or --shell-escape (TeXLive, MacTeX). Then pstricks will launch auto-pst-pdf and you'll get a pdf image.

Code modified:

\documentclass[border=12pt]{standalone}%

\def\M{4}% columns
\def\N{4}% rows
\def\scale{1}% scale
\def\filename{example-image-a}% filename

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

\usepackage[pdf]{pstricks}

{
gridcolor=yellow,
subgridcolor=gray,
subgriddiv=10,
griddots=0,
subgriddots=5,
gridwidth=0.4pt,
subgridwidth=0.2pt,
}

\psset
{
xunit=0.5\dimexpr\wd\IBox/\M\relax,
yunit=0.5\dimexpr\ht\IBox/\N\relax,
}

\begin{document}

\begin{pspicture}[showgrid=top](-\M,-\N)(\M,\N)
\rput(0,0){\usebox\IBox}
\rput[bl](1,1){This is a text.}
\end{pspicture}

\end{document}


-
Are you sure no EPS generated on the fly? – kiss my armpit Aug 29 '14 at 18:39
I don't know, really. I suppose there is an intermediate .eps file that is generated, then epstopdf and pdfcrop are launched and finally the generated .pdf image is incorporated in the main file. There remains no .eps file in the aux directory. – Bernard Aug 29 '14 at 18:45
There will be no eps file. – Herbert Aug 29 '14 at 21:01
@Herbert: Didn't know that. Thanks for the information. – Bernard Aug 29 '14 at 21:22