admin管理员组文章数量:1026989
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}
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}
本文标签: Bounding box of a TikZ pictureStack Overflow
版权声明:本文标题:Bounding box of a TikZ picture - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609545a2158922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论