如何在LaTeX文档中直接获取TikZ图片的Bounding Box坐标?
直接获取TikZ图片的Bounding Box坐标
不用单独编译再用GhostScript,直接在LaTeX文档里就能拿到TikZ图的边界框坐标,下面是几种实用方法:
方法1:用TikZ自带的current bounding box节点
TikZ内置了current bounding box这个特殊节点,它的范围就是整个tikzpicture的边界。你可以通过它提取坐标,还能直接计算宽高比:
修改你的MWE,添加代码把结果写入辅助文件(不干扰主文档排版):
\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); % 提取边界框坐标并计算宽高、宽高比 \pgfmathsetmacro{\bboxMinX}{\pgfpointanchor{current bounding box}{south west}\x} \pgfmathsetmacro{\bboxMinY}{\pgfpointanchor{current bounding box}{south west}\y} \pgfmathsetmacro{\bboxMaxX}{\pgfpointanchor{current bounding box}{north east}\x} \pgfmathsetmacro{\bboxMaxY}{\pgfpointanchor{current bounding box}{north east}\y} \pgfmathsetmacro{\bboxWidth}{\bboxMaxX - \bboxMinX} \pgfmathsetmacro{\bboxHeight}{\bboxMaxY - \bboxMinY} \pgfmathsetmacro{\aspectRatio}{\bboxWidth / \bboxHeight} % 把结果写入.aux文件,编译后打开就能看到 \immediate\write\pgfutil@auxout{ --- Figure 1 Bounding Box Data --- Min X: \bboxMinX, Min Y: \bboxMinY Max X: \bboxMaxX, Max Y: \bboxMaxY Width: \bboxWidth pt, Height: \bboxHeight pt Aspect Ratio: \aspectRatio } \end{tikzpicture} \end{center} \caption{\lipsum[1][1-3]} \end{figure} \lipsum[1] \end{document}
编译完成后,打开生成的.aux文件,就能看到边界框的详细数据,单位是LaTeX默认的pt。
方法2:用tikzscale包简化提取
tikzscale包本来是用来缩放TikZ图适配容器的,但它提供的\gettikzxy命令能直接提取节点坐标,用起来更简洁:
\documentclass[10pt,a4paper]{article} \usepackage{tikz} \usepackage{tikzscale} \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} % 提取边界框的西南和东北角坐标 \gettikzxy{current bounding box}{\minX}{\minY} \gettikzxy{current bounding box.north east}{\maxX}{\maxY} \pgfmathsetmacro{\bboxWidth}{\maxX - \minX} \pgfmathsetmacro{\bboxHeight}{\maxY - \minY} % 写入辅助文件 \immediate\write\pgfutil@auxout{ --- Figure 1 Bounding Box (tikzscale) --- Min X: \minX, Min Y: \minY Max X: \maxX, Max Y: \maxY Width: \bboxWidth pt, Height: \bboxHeight pt Aspect Ratio: \pgfmathparse{\bboxWidth/\bboxHeight}\pgfmathresult } \end{center} \caption{\lipsum[1][1-3]} \end{figure} \lipsum[1] \end{document}
方法3:临时调试用——直接在文档中显示数据
如果只是临时看一下边界框数据,不想折腾辅助文件,可以直接把信息放在图的下方:
\begin{tikzpicture} \node[draw,fill,red,circle,radius=5cm] at (0,0) {}; \draw[fill,green,] (2,0) rectangle (4,1); % 计算宽高和宽高比 \pgfmathsetmacro{\w}{\pgfpointdiff{\pgfpointanchor{current bounding box}{south west}}{\pgfpointanchor{current bounding box}{north east}}\x} \pgfmathsetmacro{\h}{\pgfpointdiff{\pgfpointanchor{current bounding box}{south west}}{\pgfpointanchor{current bounding box}{north east}}\y} % 在图下方打印信息 \node[below,font=\small] at (current bounding box.south) {Width: \w pt, Height: \h pt, Aspect Ratio: \pgfmathparse{\w/\h}\pgfmathresult}; \end{tikzpicture}
这种方式会影响排版,只适合调试阶段用。
额外提示
- 所有方法都是在主文档编译时直接获取边界框,和单独编译的结果完全一致,不会出现逻辑偏差。
- 如果需要把
pt转换成cm,可以用\pgfmathparse{\w/28.45274}(1cm≈28.45274pt)来计算。
内容的提问来源于stack exchange,提问作者Eduard Tetzlaff
相关产品推荐
相关产品推荐

