[Tex/LaTex] Edges of tikz picture cut off when using shifted scope

bounding boxstandalonetikz-pgf

I'm drawing a standalone tikz picture and when I compile it one of the edges is cut off. I think it's because I'm using a shifted scope within the picture. (Edit, it may be due to the [x=2cm,y=2cm] scale I have applied to the picture)

\documentclass[tikz,varwidth,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm]
  \begin{scope}
    \draw (0,0) rectangle (3,3);
  \end{scope}

  \begin{scope}[shift={(3.5,0)}]
    \draw (0,0) rectangle (3,3);
  \end{scope}
\end{tikzpicture}
\end{document}

The furthest right part of my picture is being cut off, I suspect the varwidth, border=5pt] arguments aren't taking the shift (edit: or maybe the scale) into account.

So my question is either:

  • Is there a non-manual way of making sure the edges of my picture don't get cut off? (I'm aware of \useasboundingbox but would prefer not to use it)
  • Is there a better technique than shift to construct two pictures which are very similar but offset from one another by some amount? I don't want to have to draw the picture twice with different coordinates. From an editing point of view shift is much more efficient.

Best Answer

The problem is varwidth, which in the example you show is completely unnecessary, so just remove it.

With varwidth, the content is set in a varwidth environment that is \linewidth wide. By default, \linewidth corresponds to about 12cm. The content in your diagram is 7.5 * 2 = 14cm wide, so you get an Overfull \hbox (25.28575pt too wide). Because standalone sets the size of the PDF to that of the varwidth, part of the diagram is cut off.

Related Question