# tikz with standalone has border at left

I always think standalone will center my picture. but below example not:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\usetikzlibrary{backgrounds}

\begin{document}
%jpg from http://www.murderousmaths.co.uk/magic/predictor%20cards%20template%20120.jpg

\newsavebox{\graph}\savebox{\graph}{\includegraphics{\infilename}}
\newlength\gh\setlength\gh{\heightof{\usebox\graph}}
\newlength\gw\setlength\gw{\widthof{\usebox\graph}}

\newcounter{DEBUG}
\setcounter{DEBUG}{1}
\setcounter{DEBUG}{0}

\def\BL{0.16\gw,0.106\gh} % bottom left corner
\def\TR{0.92\gw,0.86\gh} % top right corner

\begin{tikzpicture}[framed,>=latex,
image/.style={anchor=south west,inner sep=0,outer sep=0}]
\ifthenelse{\theDEBUG = 1}{
% do nothing for debug purpose
}{
\clip (\BL) rectangle (\TR);
}
\node[image] (NI) {\usebox\graph};
\ifthenelse{\theDEBUG = 1}{
\draw[red,line width=2pt] (\BL) rectangle (\TR);
\begin{scope}[x={(NI.south east)},y={(NI.north west)}]
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
\foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
\foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
\end{scope}
}{}
\end{tikzpicture}

\end{document}


Why there still has a white bar at the left of the clipped picture?

This happens because there are paragraphs and other whitespace before the actual image.

If you move the \begin{document} to be right before \begin{tikzpicture}, the problem disappears because whitespace is ignored in the preamble.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\usetikzlibrary{backgrounds}

%jpg from http://www.murderousmaths.co.uk/magic/predictor%20cards%20template%20120.jpg
\def\infilename{example-image}

\newsavebox{\graph}\savebox{\graph}{\includegraphics{\infilename}}
\newlength\gh\setlength\gh{\heightof{\usebox\graph}}
\newlength\gw\setlength\gw{\widthof{\usebox\graph}}

\newcounter{DEBUG}
\setcounter{DEBUG}{1}
\setcounter{DEBUG}{0}

\def\BL{0.16\gw,0.106\gh} % bottom left corner
\def\TR{0.92\gw,0.86\gh} % top right corner

\begin{document}
\begin{tikzpicture}[framed,>=latex,
image/.style={anchor=south west,inner sep=0,outer sep=0}]
\ifthenelse{\theDEBUG = 1}{
% do nothing for debug purpose
}{
\clip (\BL) rectangle (\TR);
}
\node[image] (NI) {\usebox\graph};
\ifthenelse{\theDEBUG = 1}{
\draw[red,line width=2pt] (\BL) rectangle (\TR);
\begin{scope}[x={(NI.south east)},y={(NI.north west)}]
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
\foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
\foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
\end{scope}
}{}
\end{tikzpicture}
\end{document}


Alternatively, you could place % signs in the empty lines and at ends of lines that cause problems, but in this case it's not necessary.

• Interesting, if it's a blank line, it should be show on the top of graph rather than sow it on the left side! :-) – beetlej Mar 23 '16 at 21:15
• I'm not completely certain what actually happens, but it may be that standalone ignores the paragraphs (they're just \par tokens which can easily be redefined), but the other whitespace still matters. Note that in many contexts, even single line breaks count as whitespace. – wrtlprnft Mar 23 '16 at 21:19