9

I am drawing an tikz picture using standalone class, but there is weird blank space on the left side. My code is

\documentclass{standalone}

\usepackage{kerkis}
\usepackage{tikz}

\begin{document}

\usetikzlibrary{positioning}

\tikzset{
  gold/.style = {
    top color=gray!60, 
    bottom color=gray!20, 
    minimum width=0.25cm, 
    minimum height=2cm, 
    anchor=west,
  },
  boron/.style = {
    top color=gray!80, 
    bottom color=gray!40,
    minimum width=1cm, 
    minimum height=2cm, 
    anchor=west,
  },
  telescope/.style = {
    right color=black!70, 
    left color=black!20,
    minimum width=1.2cm,
    minimum height=.4cm,
    sloped,
    pos=1,
    rotate=90,
  },
  E/.style = {
     top color=black!80, 
     bottom color=black!40,
     minimum width=1.2cm,
     minimum height=1.4cm, 
     sloped,
     pos=1,
     rotate=90,
   },
}

 \begin{tikzpicture}

  \coordinate (beam left) at (-5,0);
  \coordinate (beam right) at (2,0);

  % Incident Beam
  \draw[->, very thick] (beam left) -- (beam right);
  \node[above right] at (beam left) (proton) {$p^+$};

  % Target : Boron + Au
  \node[gold, label=95:$Au$] at (6,0) (gold) {};% Au
  \node[boron, right=0mm of gold, label=80:$^{nat}B$] (boron) {};

  \coordinate (hit) at (gold.west);

  % Telescopes
  \path (hit) -- +(170:8) 
        node[telescope, label=right:$6\mu m$] (telescope1) {$\Delta E$};
  \path (hit) -- +(-120:8)
        node[telescope, label=left:$22\mu m$] (telescope2) {$\Delta E$};
  \path (hit) -- +(170:9)
        node[E, label=20:$1000\mu m$] {$\mathbf{E}$};
  \path (hit) -- +(-120:9)
        node[E, label=left:$1000\mu m$]  {$\mathbf{E}$};

  % Angles
  \draw[dashed, gray, thick] (beam right) -- (hit);
  \draw[dashed, gray, thick] (hit) -- (telescope1);
  \draw[dashed, gray, thick] (hit) -- (telescope2);
  \draw[<->,thick] (hit)  +(180:2) arc (180:170:2);
  \path (hit) +(175:2) node[left] {$10^\circ$};
  \draw[<->,thick] (hit) +(180:2) arc (-180:-120:2);
  \path (hit) +(210:2) node[left] {$60^\circ$};
 \end{tikzpicture}

\end{document}

My output is the following

enter image description here

Any idea on why this is happening and how can it be fixed?

3
  • \usetikzlibrary should go before \begin{document}
    – egreg
    Jul 10, 2014 at 8:48
  • @egreg : You are right and that was the problem... Why did this happen?
    – Thanos
    Jul 10, 2014 at 8:55
  • \usetikzlibrary takes up horizontal space I guess.
    – 1010011010
    Jul 10, 2014 at 8:58

2 Answers 2

12

Here's a minimalistic example:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{} A
\end{document}

enter image description here

This shows that the space between \tikzset{} and A is not ignored. It would be the same for

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{}
A
\end{document}

So you have several of these spurious spaces, because tikzlibrarypositioning.code.tex starts with

\tikzset{above/.code=\tikz@lib@place@handle@{#1}{south}{0}{1}{north}{1}}
\tikzset{above left/.code=\tikz@lib@place@handle@{#1}{south east}{-1}{1}{north west}{0.707106781}}

and so on for several lines.

The fact is that standalone puts TeX in LR mode (horizontal mode in proper TeX language), so spaces are not ignored.

You don't get any space if you add the varwidth option to standalone:

\usepackage[varwidth]{standalone}

(but you get an overfull box warning, because of the picture size).

Load libraries and do \tikzset commands before \begin{document}.

7

If you put a \fbox{} around your tikzpicture you get:

Result

Which shows that the extra space is not inside the tikzpicture. So it has to be produced by the code which is before it.

If you move \usetikzlibrary{positioning} to the preamble, as egreg suggested in a comment, you get now:

Result2

Which still has a (very small) gap to the left. This is due to the \tikzset settings, I'm not sure why (even removing all blank lines, this gap remains).

Moving \tikzset also to the preamble gives the desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.