3

I'm trying to include a background image of the correct size based on the two variables \IApaperheight and \IApaperwidth.

To do this, I'm converting the lengths to a rounded text value and concatenating them with a prefix in another variable.

I can render the full path correctly, but when I try to pass it to \includegraphics it is complaining because (as far as I can figure out) the path isn't a string, but instead a bunch of macros.

Is there any way I can "stringify" the path name so that I can use it as a literal?

I am using this to convert lengths to rounded integers:

% \IArndmm{\length} returns the length in mm rounded to an integer
\makeatletter%
\def\l@nunitperpt{0.351459}\def\l@nunits{mm}%
\def\@round#1.#2\@empty{#1}%
\newcommand{\IArndmm}[1]{%
    \setlength{\@tempdimc}{\l@nunitperpt #1}%
      \addtolength{\@tempdimc}{0.5pt}%
    \edef\@@round{\strip@pt\@tempdimc}%
    \expandafter\@round\@@round.\@empty%
}%
\makeatother%
%

Then this to include the image:

\newcommand{\IABGImageName}[3]{#1/#2x#3.jpg}%
%
\includegraphics[
    width=\IApaperwidth,height=\IApaperheight,keepaspectratio]{%
        \IABGImageName{\IABGImage}%
                  {\IArndmm{\IApaperwidth}}%
                  {\IArndmm{\IApaperheight}}%
}}%
Martin Scharrer
  • 262,582
DrTech
  • 133
  • You need to make your macros fully expandable or modify a macro instead. ATM the \setlength and \addtolength as well as \edef stops the macro \IArndmm to be fully expandable. In short everything which requires assignments isn't expandable. – Martin Scharrer Jun 01 '11 at 18:46
  • OK - any idea how to achieve the rounding without using those? – DrTech Jun 01 '11 at 18:49

1 Answers1

4

If you use macros like this than they must be fully expandable, i.e. expand to only a string by themselves. However assignments aren't expandable. e-TeX help here by providing \dimexpr which can be used to build dimension expressions which can include additions. This primitive reads the following text until something appears which is not a valid dimension are arithmetic sign etc. You can also terminate it using \relax which is then removed automatically. If you place a \the in front of \dimexpr you will get the dimension as string as usual. You can also use \dimexpr internally for the argument so that this doesn't have to be a length register. Note that there is also \numexpr for integer expressions. See the etex manual for details.

\documentclass{article}
\usepackage{graphicx}

% \IArndmm{\length} returns the length in mm rounded to an integer
\makeatletter%
\def\l@nunitperpt{0.351459}\def\l@nunits{mm}%
\def\@round#1.#2\@empty{#1}%
\newcommand{\IArndmm}[1]{%
    \expandafter\@round
    \the\dimexpr\l@nunitperpt\dimexpr#1\relax + 0.5pt\relax\@empty
}%
\makeatother%


\begin{document}

\newcommand{\IABGImageName}[3]{#1/#2x#3.jpg}%

% Dummy values
\newcommand{\IABGImage}{someimage}
\let\IApaperheight\paperheight
\let\IApaperwidth\paperwidth

\includegraphics[
    width=\IApaperwidth,height=\IApaperheight,keepaspectratio]{%
        \IABGImageName{\IABGImage}%
                  {\IArndmm{\IApaperwidth}}%
                  {\IArndmm{\IApaperheight}}%
}%

\end{document}

Another way would be to process the input first and write it into a macro which is then expandable, something like:

\ProcessIAImageName{...}% saves name in \IAimage
\includegraphics[
    width=\IApaperwidth,height=\IApaperheight,keepaspectratio]{%
    \IAimage
}%
Martin Scharrer
  • 262,582