![Como obtenho a altura de uma figura em LaTeX](https://rvso.com/image/330740/Como%20obtenho%20a%20altura%20de%20uma%20figura%20em%20LaTeX.png)
Eu tenho algum código LaTeX
\begin{figure}
ABC
\caption{def}
\end{figure}
E eu gostaria de poder encontrar sua altura em uma pdf
saída. O método descritoaquinão funciona, gerando o erro:
! LaTeX Error: Not in outer par mode.
Considerei a ideia de usar \write18
e de alguma forma medir o tamanho da figura por si só, usando truques de captura de pixels, mas se o documento tivesse um tamanho de legenda alterado globalmente, isso bagunçaria as coisas.
Eu queria saber se havia uma maneira válida de fazer isso.
Responder1
Aqui está uma solução
\documentclass{article}
\usepackage{capt-of}
\newsavebox\mybox
\begin{document}
\sbox\mybox{\parbox[b]{\textwidth}{ABC\captionof{figure}{bla bla}}}
\the\ht\mybox
\end{document}
Aqui está outra solução:
\documentclass{article}
\begin{document}
\the\pagetotal
\begin{figure}[h]
ABC
\caption{def}
\end{figure}
\the\pagetotal
\end{document}
Atualizar Neste exemplo tentamos reproduzir a posição float manualmente na primeira página e na segunda página deixamos o latex fazer o trabalho.
Observação:como se pode ver para a posição superior não há problema. Reproduzir a posição inferior é difícil (se não impossível?)
\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}
Responder2
A figura é armazenada em uma caixa para que você possa medi-la (você precisaria trabalhar um pouco mais para h
os carros alegóricos, pois eles podem ser usados antes que o relatório aconteça, se feito após o ambiente como aqui)
\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}
Produz um registro de
** This float + caption has height + depth:
** 30.77776pt
ou coletados até o final:
\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
**