Graphics, Figures & TablesTikZ bounding box wrong position

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kobzol
Posts: 2
Joined: Wed Jun 14, 2023 1:11 pm

TikZ bounding box wrong position

Post by kobzol »

Hi!
I'm trying to use TikZ graphs to draw a simple graph, but I'm having an issue, where if I draw the graph, there will be a lot of whitespace (margin) below it, which creates mess. I visualized the bounding boxes and realized that TikZ draws outside of the box where LaTex expects it to, and I have no idea why.

Here is a MWE on Overleaf and also inline:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{positioning}

\title{Tikz margin}

\begin{document}

\begin{figure}
\fbox{\begin{tikzpicture}
\tikz {
%\tikzset{shift={(0.25,2.25)}}
\node at (0, 0) {X};
\graph {a -> {b, c, d}};
\draw [brown] (current bounding box.south west) rectangle (current bounding box.north east);
}
\end{tikzpicture}}
\caption{Foo}
\end{figure}

\end{document}


I have added fbox to the code to see where is the expected LaTex box, and also added drawing of the TikZ bounding box, which is way off. From the result it's obvious that TikZ simply draws outside of the LaTex box, which is why there is so much "margin" below the graph. I have also visualized a node at (0, 0) to see where the TikZ origin is.

Here is how the result looks like:

Image

Is there a way to make sure that the TikZ box will fit inside the LaTex box automatically? Now I have to use the commented tikzset shift command to move it there manually.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

TikZ bounding box wrong position

Post by Bartman »

Without the \tikz command there is no shift.
kobzol
Posts: 2
Joined: Wed Jun 14, 2023 1:11 pm

TikZ bounding box wrong position

Post by kobzol »

Oh my god. I didn't realize that tikzpicture and tikz shouldn't be combined. Thank you so much!
Post Reply