我想通过排版来测量标题的高度\savebox
,但遇到
除领导者外,您不能在这里使用'\hrule'。
下面的 MWE 中的有问题的行已被注释掉。
代码
\documentclass{article}
\usepackage{caption}
\usepackage{graphicx}
\newsavebox{\CaptionSavebox}
\captionsetup{width=0.20\linewidth}%
\newcommand*{\CaptionText}{This is a caption for the above figure}
\begin{document}
\begin{figure}
%\savebox{\CaptionSavebox}{\captionof{figure}{\CaptionText}}% <--- Why can't I do this?
\centering
\includegraphics[width=.25\linewidth]{example-image-a}
\usebox{\CaptionSavebox}%
\captionof{figure}{\CaptionText}% <-- don't need this if have the caption in the savebox
\label{fig: figure label}%
\end{figure}%
\end{document}
答案1
我猜是因为\savebox
将内容放在 中,\hbox
所以问题在于标题可能不止一行。你可以将其放在 中来解决这个问题\parbox
。
框中的标题下方多了一个空行。这似乎是为了\caption
确保它位于自己的一行而产生的。下面我删除了那行。
\documentclass{article}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{graphicx}
\newsavebox{\CaptionSavebox}
\captionsetup{width=0.20\linewidth}%
\newcommand*{\CaptionText}{This is a caption for the above figure}
\begin{document}
\lipsum[1]
\begin{figure}[htb]
%\savebox{\CaptionSavebox}{\captionof{figure}{\CaptionText}}% <--- Why can't I do this?
\centering
\includegraphics[width=.25\linewidth]{example-image-a}
%%
\savebox{\CaptionSavebox}{\parbox[b]{\captionwidth}{\caption{\CaptionText}\vspace{-\baselineskip}}}
\par
\usebox{\CaptionSavebox}%
\rlap{\rule{0.5pt}{\ht\CaptionSavebox} Height=\the\ht\CaptionSavebox Depth=\the\dp\CaptionSavebox}
%%\captionof{figure}{\CaptionText}% <-- don't need this if have the caption in the savebox
\label{fig: figure label}%
\end{figure}%
\lipsum[2]
\end{document}