# Problem with positioning a figure environment inside \resizebox

I'm writing a document including figures exported from Inkscape (in pdf+latex format). These figures use a figure environment containing all the text labels and a \includegraphics macro that imports the graphical part of the figure from the pdf file.

By default these figures use the same font as the rest of the document, which is too large for my needs. To work it around I have scaled the figure up, and then scaled it down to the original size using \resizebox. This doesn't alter the size of graphics, only the size of font used for rendering text labels, which is perfect.

However, there is one weird glitch that I can't get rid off. All figures are shifted to the right and are slightly smaller than figures included directly (that is, without wrapping with figure environment) in the \resizebox.

Below is a test document exposing this behavior:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage{color}

\begin{document}
\begin{figure}[!t]
\def\svgwidth{1.5\textwidth}%
\resizebox{\textwidth}{!}{%
\input{testfig.pdf_tex}%
}
\caption{Test with figure'' environment.}
\end{figure}

\begin{figure}[!t]
\resizebox{\textwidth}{!}{%
\includegraphics[width=1.5\textwidth]{testfig.pdf}%
}
\caption{Test without figure'' environment.}
\end{figure}

\vbox{}

\end{document}


A testfig.pdf_tex file exported from Inkscape (with comments removed):

\begingroup
\makeatletter
\providecommand\color[2][]{%
\errmessage{(Inkscape) Color is used for the text in Inkscape, but the package 'color.sty' is not loaded}
\renewcommand\color[2][]{}%
}
\providecommand\transparent[1]{%
\errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package 'transparent.sty' is not loaded}
\renewcommand\transparent[1]{}%
}
\providecommand\rotatebox[2]{#2}
\ifx\svgwidth\undefined
\setlength{\unitlength}{778.02519531pt}
\else
\setlength{\unitlength}{\svgwidth}
\fi
\global\let\svgwidth\undefined
\makeatother
\begin{picture}(1,0.36433855)%
\put(0,0){\includegraphics[width=\unitlength]{testfig.pdf}}%
\put(0.51462818,0.17761502){\color[rgb]{0,0,0}\makebox(0,0)[b]{\smash{Some text}}}%
\end{picture}%
\endgroup


-
Welcome to TeX.sx! As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. –  Martin Scharrer Jul 13 '11 at 13:47
Thanks, Martin. I've edited my question to add the link. –  Andrzej Jul 13 '11 at 13:58
Unfortunately the \resizebox* seems to do the exact same thing as \resizebox, unless I'm missing something. Is there any documentation for this macro? The * makes searching for it difficult. –  Andrzej Jul 13 '11 at 14:04
I was wrong about the \resizebox* here. This macro in its normal ans star form is described in the grfguide. –  Martin Scharrer Jul 13 '11 at 14:16

The problem is in the end-of-line characters after, for example, \providecommand\rotatebox[2]{#2}. When not used in \resizebox, these end-of-line characters are ignored because TeX is in vertical mode; inside \resizebox TeX is in horizontal mode and they count as spaces.

\begin{figure}[!t]
\def\svgwidth{1.5\textwidth}%
\begingroup\endlinechar=-1
\resizebox{\textwidth}{!}{%
\input{testfig.pdf_tex}%
}\endgroup
\caption{Test with figure'' environment.}
\end{figure}


Setting \endlinechar=-1 we are able to ignore them at least for the duration of the \input.

You may also define

\newcommand{\noendlineinput}[1]{\begingroup
\endlinechar=-1 \input{#1}\endgroup}


and say

\begin{figure}[!t]
\def\svgwidth{1.5\textwidth}%
\resizebox{\textwidth}{!}{%
\noendlineinput{testfig.pdf_tex}%
}
\caption{Test with figure'' environment.}
\end{figure}


You should check that the end-of-line is never used in the input file to separate words in the picture environment.

-
I just was going to post the same. :-) What do you think from \resizebox{\textwidth}{!}{\endlinechar=-1\relax\input{testfig.pdf_tex}} (in case he hasn't full control over the source; because of automatic generation).  –  Martin Scharrer Jul 13 '11 at 14:07
@Martin That's equivalent (or maybe better). It's the input file that's automatically generated, I believe. However I prefer using the new command, that can come handy also in other cases. –  egreg Jul 13 '11 at 14:12
Thanks guys! All the advices are absolutely brilliant. –  Andrzej Jul 13 '11 at 14:20
@Martin, what is the purpose of \relax? The document renders the same way without it. –  Andrzej Jul 13 '11 at 14:20
@Andrzej: It's an endmarker (in this case), which tells LaTeX that the number -1 is finished. LaTeX otherwise would expand the next macro to see if there are more digits. In order to avoid this I use a \relax`, however it might not always be necessary. –  Martin Scharrer Jul 13 '11 at 14:22
show 1 more comment