2

From my small experience with tikz in Latex, it seems that the boundary of a tikz image is set automatically based on its contents. This caused me some trouble when I was trying to scale and align multiple tikz images.

Is there a way to set the boundary manually? For instance perhaps I want an image with nodes at (1,1) and (2,1) and a boundary which is rectangular with corners at (0,0) and (3,2). How would this be achieved?

Erewhon
  • 25

3 Answers3

2

It is possible to use use as bounding box (see page 175 of the 3.0.1a manual) and its shortcut \useasboundingbox (see page 164)

Example taken from page 236 of the manual: use as bounding box

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[auto]
\draw[help lines,use as bounding box] (0,-.5) grid (4,5);
\draw (0.5,0) .. controls (9,6) and (-5,6) .. (3.5,0)
node foreach \pos in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1}
[pos=\pos,swap,fill=red!20] {\pos}
node foreach \pos in {0.025,0.2,0.4,0.6,0.8,0.975}
[pos=\pos,fill=blue!20] {\pos};
\end{tikzpicture}
\end{document}
AndréC
  • 24,137
1

use of clip you can see from the following example from TikZ & PGF Manuall, v 3.0.1A, page 140:

\begin{tikzpicture}
\clip (-2,-2) rectangle (2,2);
\draw [name path=curve 1] (-2,-1) .. controls (8,-1) and (-8,1) .. (2,1);
\draw [name path=curve 2] (-1,-2) .. controls (-1,8) and (1,-8) .. (1,2);
\fill [name intersections={
of=curve 1 and curve 2,
by={[label=center:a],[label=center:...],[label=center:i]}}];
\end{tikzpicture}

description you can find in chapter 15 Actions on Paths, page 164 of aforementioned manual.

Zarko
  • 296,517
1

Just include the desired margin coordinates in any path and the resulting tikzpicture will include it in final bounding box. In following picture, a phantom path is drawn and included in final box (shown in black with show background rectangle).

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, backgrounds}

\begin{document}
\begin{tikzpicture}[show background rectangle]
\draw[red] (1,1) rectangle ++(1,1);
\path(0,0) -- (3,2);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588