![¿Cómo obtengo la altura de una figura en LaTeX?](https://rvso.com/image/330740/%C2%BFC%C3%B3mo%20obtengo%20la%20altura%20de%20una%20figura%20en%20LaTeX%3F.png)
Tengo algo de código LaTeX
\begin{figure}
ABC
\caption{def}
\end{figure}
Y me gustaría poder encontrar su altura en una pdf
salida. El método descritoaquíno funciona, arrojando el error:
! LaTeX Error: Not in outer par mode.
Consideré la idea de usar \write18
y de alguna manera medir el tamaño de la figura por sí misma usando trucos para capturar píxeles, pero si el documento tuviera un tamaño de título cambiado globalmente, eso arruinaría las cosas.
Me preguntaba si había una forma válida de hacer esto.
Respuesta1
Aquí hay una solución
\documentclass{article}
\usepackage{capt-of}
\newsavebox\mybox
\begin{document}
\sbox\mybox{\parbox[b]{\textwidth}{ABC\captionof{figure}{bla bla}}}
\the\ht\mybox
\end{document}
Aquí hay otra solución:
\documentclass{article}
\begin{document}
\the\pagetotal
\begin{figure}[h]
ABC
\caption{def}
\end{figure}
\the\pagetotal
\end{document}
Actualizar En este ejemplo intentamos reproducir la posición de flotación a mano en la primera página y en la segunda página dejamos que el látex haga el trabajo.
Nota:Como se puede ver, para la primera posición no hay problema. Reproducir la posición inferior es difícil (¿si no imposible?)
\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}
Respuesta2
La figura está almacenada en una caja para que pueda medirla (tendría que trabajar un poco más para h
los flotadores, ya que pueden usarse antes de que se realice el informe si se hace después del medio ambiente, como aquí)
\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}
Produce un registro de
** This float + caption has height + depth:
** 30.77776pt
o recogido hasta el 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
**