[Tex/LaTex] Problem with positioning a figure environment inside \resizebox

boxesfontsizegraphicspdfpositioning

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

A screenshot showing the result.

Best Answer

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.