LaTeXで図の高さを取得するにはどうすればいいですか

LaTeXで図の高さを取得するにはどうすればいいですか

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}

アップデート この例では、最初のページではフロート位置を手動で再現し、2 番目のページでは LaTeX にその作業を任せます。

注記:ご覧のとおり、上の位置では問題ありません。下の位置を再現するのは困難です(不可能ではないにしても?) ここに画像の説明を入力してください

\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
** 

関連情報