![如何在 LaTeX 中取得圖形的高度](https://rvso.com/image/330740/%E5%A6%82%E4%BD%95%E5%9C%A8%20LaTeX%20%E4%B8%AD%E5%8F%96%E5%BE%97%E5%9C%96%E5%BD%A2%E7%9A%84%E9%AB%98%E5%BA%A6.png)
我有一些 LaTeX 程式碼
\begin{figure}
ABC
\caption{def}
\end{figure}
我希望能夠在pdf
輸出中找到它的高度。概述的方法這裡不起作用,產生錯誤:
! LaTeX Error: Not in outer par mode.
我考慮過使用\write18
像素抓取技巧來使用並以某種方式測量圖形本身的大小,但如果文件具有全局更改的標題大小,那就會把事情搞砸。
我想知道是否有有效的方法可以做到這一點。
答案1
這是一個解決方案
\documentclass{article}
\usepackage{capt-of}
\newsavebox\mybox
\begin{document}
\sbox\mybox{\parbox[b]{\textwidth}{ABC\captionof{figure}{bla bla}}}
\the\ht\mybox
\end{document}
這是另一個解決方案:
\documentclass{article}
\begin{document}
\the\pagetotal
\begin{figure}[h]
ABC
\caption{def}
\end{figure}
\the\pagetotal
\end{document}
更新 在這個例子中,我們嘗試在第一頁中手動重現浮動位置,在第二頁我們讓乳膠完成這項工作。
筆記:正如人們所看到的,排名第一是沒有問題的。複製底部位置很困難(如果不是不可能的話?)
\documentclass{article}
\usepackage{capt-of}
\usepackage{lipsum}
\newsavebox\mybox
\newcommand\myfigure[1][(Manually)]{\rule{5cm}{1cm}\captionof{figure}{My figure #1}}
\begin{document}
\sbox\mybox{\parbox[b]{\linewidth}{\myfigure
\boxmaxdepth \maxdepth
\vbox{}
\vskip -\floatsep
\topfigrule
\vskip \textfloatsep}}
\noindent\parbox[b]{\linewidth}{\myfigure
\boxmaxdepth \maxdepth
\vbox{}
\vskip -\floatsep
\topfigrule
\vskip \textfloatsep}
\the\ht\mybox****\lipsum[3]
\sbox\mybox{\parbox[b]{\textwidth}{\myfigure}}
\noindent\parbox[b]{\textwidth}{\myfigure}
Height without any vspace:\the\ht\mybox****\lipsum[3-4]
bla bla
this is difficult, see value of the height in next page
\sbox\mybox{\parbox[b]{\linewidth}{\vskip \textfloatsep
\botfigrule
\vbox{}
\vskip -\floatsep
\myfigure}}
\noindent\parbox[b]{\linewidth}{\vskip \textfloatsep
\botfigrule
\vbox{}
\vskip -\floatsep
\myfigure}
\newpage\the\ht\mybox****\lipsum[3]
\begin{figure}[t]
\myfigure[(\LaTeX)]
\end{figure}
\lipsum[3-4]
\begin{figure}[b]
\myfigure[(\LaTeX)]
\end{figure}
\lipsum[3]
\end{document}
答案2
該數字儲存在一個盒子中,以便您可以測量它(您需要為h
浮點數付出更多努力,因為如果在此處的環境之後完成,它們可能會在報告發生之前使用)
\documentclass{article}
\begin{document}
\begin{figure}
ABC
\caption{def}
\xdef\thisfloat{\the\csname @currbox\endcsname}%
\end{figure}
\typeout{%
** This float + caption has height + depth:^^J**
\the\dimexpr\ht\thisfloat+\dp\thisfloat\relax}
\end{document}
產生一個日誌
** This float + caption has height + depth:
** 30.77776pt
或收集到最後:
\documentclass{article}
\begin{document}
\begin{figure}
ABC
\caption{def\label{z}}
\xdef\thisfloat{\the\csname @currbox\endcsname}%
\end{figure}
\edef\tmp{\noexpand\AtEndDocument{%
\noexpand\foo{z}{\the\dimexpr\ht\thisfloat+\dp\thisfloat\relax}}}%
\tmp
aaaa
\begin{figure}
ABC\\
XYZ
\caption{def\label{z2}}
\xdef\thisfloat{\the\csname @currbox\endcsname}%
\end{figure}
\edef\tmp{\noexpand\AtEndDocument{%
\noexpand\foo{z2}{\the\dimexpr\ht\thisfloat+\dp\thisfloat\relax}}}%
\tmp
aaaa
\def\foo#1#2{% whatever you want to do with the data
\typeout{**^^J%
** This float (#1) + caption has height + depth: #2^^J**
}}
\end{document}
**
** This float (z) + caption has height + depth: 30.77776pt
**
**
** This float (z2) + caption has height + depth: 42.77776pt
**