8

I have a tikz picture, which consists of a visible part on the left side and a white part on the right side (for some constructive reasons). Now I would like to redefine the size so that the new picture just consists of the left part (without scaling).

How do you do that?

1

2 Answers 2

12

You have to explicitly set the bounding box of the picture. TikZ provides the use as bounding box option to do that. For example

\documentclass{report}
\usepackage{tikz}

\begin{document}
a
\begin{tikzpicture}
    \path[use as bounding box, draw] (0,0) rectangle (2,2); % The draw option is given just for demonstration.
    \draw (0,0) circle [radius=1cm];
\end{tikzpicture}
b
\end{document}

If you want to shrink an already established bounding box, you have to reset it with \pgfresetboundingbox first:

\documentclass{report}
\usepackage{tikz}

\begin{document}
a
\begin{tikzpicture}
    \draw (0,0) circle [radius=1cm];
    \pgfresetboundingbox
    \path[use as bounding box, draw] (0,0) rectangle (2,2);
\end{tikzpicture}
b
\end{document}

Other useful options for modifying the bounding box are the trim left and trim right options to tikzpicture. Section 15.7 “Establishing a Bounding Box” of the TikZ manual (v2.10) has all the details.

2
  • 2
    +1 for \pgfresetboundingbox alone. Without it you don't have much change getting the bounding box you want, expect drawing everything you want count for it first, which isn't reasonable for larger pictures. Sep 18, 2011 at 18:17
  • This is very useful when drawing an arc, whose center lies outside of the rest of the picture. Normally the "invisible" part of circle would be included. The bounding box helps to erase it.
    – aiag
    Feb 26, 2017 at 2:26
8

You could use the \clip command to restrict your figure to a certain area (for further details, see Secion 15.8 Clipping and Fading (Soft Clipping) of the pgfmanual). A little example:

\documentclass{article}
\usepackage{tikz} 

\begin{document}

text \begin{tikzpicture}[line width=5pt]
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\draw (2,0) -- (3,0) -- (3,1) -- cycle;
\end{tikzpicture} text

text \begin{tikzpicture}[line width=5pt]
\clip (-0.2,-0.1) rectangle (1.2,1.2);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\draw (2,0) -- (3,0) -- (3,1) -- cycle;
\end{tikzpicture} text

\end{document}

enter image description here

0

You must log in to answer this question.

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