1

I am having a project in which I use Tikz for making my graphics. I "externalize" the coding by using a tikz-editor. I then simply save the achieved tikz-code in a separate .tex file in which only stands

\begin{tikz} <> \end{tikz}

I wrap this code inside a figure environment combined with \resizebox and by using \insert. I want that the picture directly starts at the left shown border and go completely to the right. However, (maybe because of nested \input?) every time I call \input, the image is having a wider margin to the right.

\documentclass{scrbook}
\usepackage{tikz}
\usepackage[showframe,includeheadfoot, left=3cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

\noindent native tikz-code:\\
\begin{figure}[h!]
\centering
\resizebox{\textwidth}{!}{%
\begin{tikzpicture}
\draw[red] (0,0) rectangle (10,.5);
\end{tikzpicture}
}
\end{figure}

\noindent input 1x:\\
\begin{figure}[h!]
\centering
\resizebox{\textwidth}{!}{%
\input{rectangle.tex}
}
\end{figure}

\noindent input 2x:\\
\begin{figure}[h!]
\centering
\resizebox{\textwidth}{!}{%
\input{rectangle_nested.tex}
}
\end{figure}

\noindent input 3x:\\
\begin{figure}[h!]
\centering
\resizebox{\textwidth}{!}{%
\input{rectangle_nested_2.tex}
}
\end{figure}
\end{document}

Compiling this, results in this image:

enter image description here

Including a .pdf via \includegraphics works, however I don't use the precompiled version, as I have to rerun the code from my main-file for creating citations I use in the graphic etc. So I just want to include the tikz-commands from an external file but not creating any margins. Many thanks.

EDIT: rectangle.tex just contains the tikz-code:

\begin{tikzpicture}
\draw[red] (0,0) rectangle (10,.5);
\end{tikzpicture}

and rectangle_nested.tex just:

\input{rectangle.tex}
5
  • 2
    You have spurious spaces after \input. \resizebox is taking those spaces into account.
    – campa
    Aug 20, 2018 at 13:54
  • How can I delete those spaces?
    – SRel
    Aug 20, 2018 at 13:57
  • Put a % after every \input: \input{whatever}%. New lines are treated like spaces.
    – campa
    Aug 20, 2018 at 13:58
  • Oh after \input I just put a % and this seems to solve it
    – SRel
    Aug 20, 2018 at 13:59
  • 1
    \noindent native tikz-code:\` never use \\ ` before a figure also you should almost never need \noindent in a latex document. Aug 20, 2018 at 14:39

1 Answer 1

3

New lines are treated like spaces (reduced MWE with just one example):

\documentclass{scrbook}
\usepackage{tikz}

\begin{document}

\resizebox{\textwidth}{!}{%
\begin{tikzpicture}
\draw[red] (0,0) rectangle (10,.5);
\end{tikzpicture} % <-- HERE THERE IS A SPACE
}

\resizebox{\textwidth}{!}{% <-- This is good!
\input{rectangle.tex} % <-- HERE THERE IS A SPACE
}
\end{document}

As a matter of fact, you correctly used % after opening the brace for the third argument of \resizebox.

4
  • are you sure about not needing % after \end{tikzpicture} ? I see a difference of \glue 3.65 plus 1.825 minus 1.21666 in the output if I add a % there. Aug 20, 2018 at 14:46
  • Uhm, I thought so, but now I'm not so sure...
    – campa
    Aug 20, 2018 at 14:50
  • I am sure :-)... Aug 20, 2018 at 14:51
  • Can't test now but I trust you :-).
    – campa
    Aug 20, 2018 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .