Bounding box of a TikZ picture - Stack Overflow

The question is very simple: I have TikZ pictures inside a LaTeX document and I need to determine the h

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
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745609546a4635867.html

相关推荐

  • Bounding box of a TikZ picture - Stack Overflow

    The question is very simple: I have TikZ pictures inside a LaTeX document and I need to determine the h

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信