# TikZ in standalone not centering

I have a diagram with the code below. When I compile this code, I have an extra empty space on the left side. Why does that happen and what can I do to fix it?

\documentclass{standalone}

\usepackage[showframe,pass]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\tikzstyle{b} = [rectangle, draw, node distance=2.6cm, text width=6em, text centered, rounded corners, minimum height=8em, minimum width=6em, thick, font=\small, fill=black!10]

\begin{tikzpicture}[auto]

\node [b] (1) {block 1 \\ block 1};
\node [b, right of=1] (2) {block 2 \\ block 2};
\node [b, right of=2] (3) {block 3 \\ block 3};
\node [b, right of=3] (4) {block 4 \\ block 4};
\node [b, right of=4] (5) {block 5 \\ block 5};

\node [b, below = 2mm of 1] (6) {block 6 \\ block 6};
\node [b, below = 2mm of 2] (7) {block 7 \\ block 7};
\node [b, right of=7] (8) {block 8 \\ block 8};
\node [b, right of=8] (9) {block 9 \\ block 9};

\draw[-, thick] (1)--(2)--(3)--(4)--(5);
\draw[-, thick] (2)--(7)--(8)--(9)--(4);

\end{tikzpicture}

\end{document}


It looks like this:

It's the end of line after the \tikzstyle declaration. Move it before \begin{document}.

By the way, \tikzstyle is deprecated and I changed it to the currently preferred format.

\documentclass{standalone}

\usepackage[showframe,pass]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
b/.style = {
rectangle,
draw,
node distance=2.6cm,
text width=6em,
text centered,
rounded corners,
minimum height=8em,
minimum width=6em,
thick,
font=\small,
fill=black!10,
},
}

\begin{document}

\begin{tikzpicture}[auto]

\node [b] (1) {block 1 \\ block 1};
\node [b, right of=1] (2) {block 2 \\ block 2};
\node [b, right of=2] (3) {block 3 \\ block 3};
\node [b, right of=3] (4) {block 4 \\ block 4};
\node [b, right of=4] (5) {block 5 \\ block 5};

\node [b, below = 2mm of 1] (6) {block 6 \\ block 6};
\node [b, below = 2mm of 2] (7) {block 7 \\ block 7};
\node [b, right of=7] (8) {block 8 \\ block 8};
\node [b, right of=8] (9) {block 9 \\ block 9};

\draw[-, thick] (1)--(2)--(3)--(4)--(5);
\draw[-, thick] (2)--(7)--(8)--(9)--(4);

\end{tikzpicture}

\end{document}


• Oh, so that's the explanation. An easier fix if he really needs the local definition is to add a % after the ] then. – T. Verron Sep 20 '17 at 9:36

use

\documentclass[tikz]{standalone}


and it will delete the trailing space.

• You should always use this optional arguments when using tikz code! – user2478 Sep 20 '17 at 10:17

Another possibility to get rid of the newlines (and the deprecated \tikzstyle) which cause this problem is moving the tikz style into the optional argument of \begin{tikzpicture}:

\begin{document}
\begin{tikzpicture}[
auto,
b/.style={rectangle, draw, node distance=2.6cm, text width=6em, text centered, rounded corners, minimum height=8em, minimum width=6em, thick, font=\small, fill=black!10},
]
...


In case you cannot change the preamble for any reason, the newlines can be ignored by ending each line between \begin{document} and \begin{tikzpicture} with a comment:

\begin{document}%
%
\tikzstyle{b} = [rectangle, draw, node distance=2.6cm, text width=6em, text centered, rounded corners, minimum height=8em, minimum width=6em, thick, font=\small, fill=black!10]%
%
\begin{tikzpicture}[auto]\begin{document}%
...