[Tex/LaTex] Including TikZ image (with external inputs) inside adjustbox adds horisontal spacing, why

inputtikz-pgf

I'm working on my masters thesis and so I have a bunch of tikz files that I want to include in the thesis. This I do by using adjustbox to limit the size to \textwidth as such
\adjustbox{max width=\textwidth,bgcolor=blue}{\input{test.tikz}}.

Now my problem is that I have created external files containing common draw commands in order to creating multiple UML like diagrams and memory diagrams easier… this however, for some reason, adds padding to the including picture! Why, and what can I do to avoid it?

Example:
The test file "test.ext" used in the example only contains 100×5 '%'s so nothing really should be included in the final image?

Code:

\adjustbox{max width=\textwidth,bgcolor=blue}{
  \input{test.ext}                              <-- this causes the problem
  \begin{tikzpicture}[]
    \draw[fill=blue!20] (0,0) rectangle (3,1);
  \end{tikzpicture}
}

Result:

here.

Best Answer

The linebreak after \input gives the extra space, which can be removed by adding a % at the end of that line. Similarly, you can add % after \adjustbox{ and \end{tikzpicture} to remove the remaining space.

enter image description here

\documentclass{article}
\usepackage{tikz,adjustbox}
\begin{document}
\adjustbox{max width=\textwidth,bgcolor=blue}{
   \input{testinp}
  \begin{tikzpicture}[]
    \draw[fill=blue!20] (0,0) rectangle (3,1);
  \end{tikzpicture}
}
\adjustbox{max width=\textwidth,bgcolor=blue}{%
   \input{testinp}%
  \begin{tikzpicture}[]
    \draw[fill=blue!20] (0,0) rectangle (3,1);
  \end{tikzpicture}%
}
\end{document}

As commented by Martin, the adjustbox environment ignores spaces at its beginning and end, so the following will give the same result as the second \adjustbox in the above example.

\begin{adjustbox}{max width=\textwidth,bgcolor=blue}
   \input{testinp}%
  \begin{tikzpicture}[]
    \draw[fill=blue!20] (0,0) rectangle (3,1);
  \end{tikzpicture}
\end{adjustbox}

For reference, a few questions related to end-of-line %s:

Related Question