The question is very simple: I have TikZ pictures inside a LaTeX document and I need to determine the heights and widths of their bounding boxs to calculate the aspect ratios.
I know how to do that for pdf, eps files and whatnot in ghostscript, and one possible solution is to just compile the TikZ code for each picture as standalone and then use GS.
However, this seems neither direct nor elegant and even might give wrong values, depending on what the standalong compilation does.
So how do I get the bounding box coordinates directly?
MWE - I'd like to get the coordinates of the bounding box of Figure 1:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\node[draw,fill,red,circle,radius=5cm] at (0,0) {};
\draw[fill,green,] (2,0) rectangle (4,1);
\end{tikzpicture}
\end{center}
\caption{\lipsum[1][1-3]}
\end{figure}
\lipsum[1]
\end{document}
The question is very simple: I have TikZ pictures inside a LaTeX document and I need to determine the heights and widths of their bounding boxs to calculate the aspect ratios.
I know how to do that for pdf, eps files and whatnot in ghostscript, and one possible solution is to just compile the TikZ code for each picture as standalone and then use GS.
However, this seems neither direct nor elegant and even might give wrong values, depending on what the standalong compilation does.
So how do I get the bounding box coordinates directly?
MWE - I'd like to get the coordinates of the bounding box of Figure 1:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\node[draw,fill,red,circle,radius=5cm] at (0,0) {};
\draw[fill,green,] (2,0) rectangle (4,1);
\end{tikzpicture}
\end{center}
\caption{\lipsum[1][1-3]}
\end{figure}
\lipsum[1]
\end{document}
Share
Improve this question
edited Nov 18, 2024 at 16:22
Eduard Tetzlaff
asked Nov 18, 2024 at 16:00
Eduard TetzlaffEduard Tetzlaff
495 bronze badges
2
- Please add a compilable minimal reproducible example – samcarter_is_at_topanswers.xyz Commented Nov 18, 2024 at 16:11
- @samcarter_is_at_topanswers.xyz thanks, I included a MWE. – Eduard Tetzlaff Commented Nov 18, 2024 at 16:38
1 Answer
Reset to default 1You could place the tikzpicture
in a box and measure the size of the box:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usepackage{lipsum}
\newsavebox{\quack}
\begin{document}
\lipsum[1]
\begin{figure}[h!]
\begin{center}
\sbox{\quack}{%
\begin{tikzpicture}
\node[draw,fill,red,circle,radius=5cm] at (0,0) {};
\draw[fill,green,] (2,0) rectangle (4,1);
\end{tikzpicture}%
}
\usebox{\quack}
Width: \the\wd\quack
Height: \the\ht\quack
Depth: \the\dp\quack
\end{center}
\caption{\lipsum[1][1-3]}
\end{figure}
\lipsum[1]
\end{document}