最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Bounding box of a TikZ picture - Stack Overflow

matteradmin5PV0评论

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} 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far