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?

  • 2
    give a complete example! – user2478 Dec 22 '18 at 17:30
  • 3
    \clip (-0,05) rectangle (3,2); welcome to tex.se! – Zarko Dec 22 '18 at 17:30
  • 1
    \path[use as bounding box] (0,0) rectangle (1,1); See p. 175 of the pgfmanual for more details. – marmot Dec 22 '18 at 17:46
  • Thanks all! Zarko's answer did the trick. @Zarko: Did you want to move your comment to an answer? – Erewhon Dec 22 '18 at 17:51
  • of course i can, but this can be easy to do if you provide some mwe ... :-) – Zarko Dec 22 '18 at 17:52
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.

1

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}
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

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.