caption 包中的 width 对 \linewidth 没有反应

caption 包中的 width 对 \linewidth 没有反应

我观察到,当前版本的字幕包width=0.9\linewidth对或反应不正确width=0.9\columnwidth。以下是 MWE:

\documentclass{scrartcl}

\usepackage{blindtext}
\usepackage{graphicx}
\usepackage[width = .9\linewidth, font = small, labelfont = bf]{caption}

\begin{document}

\blindtext

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.9\columnwidth]{example-image-a}
    \caption{This is a long figure caption, to show the problem with the width option and variable length like columnwidth and linewidth}
    \label{figlabel}
\end{figure}

\blindtext

\end{document}

目前,我使用 选项minmargin = 0.05\linewidth而不是width=...

答案1

caption 包选项width会立即计算标题的宽度。因此,写入时的\captionsetup{width=.9\linewidth}结果取决于\linewidth此时的值,并且在文档期间不会发生变化。

(正如 John Kormylo 已经评论过的, \linewidth 在序言中尚未正确设置。)

但是,我们可以使用calcwidth.9\linewidth在每次需要标题宽度时重新计算:

\documentclass{scrartcl}

\usepackage{blindtext}
\usepackage{graphicx}
\usepackage[calcwidth = .9\linewidth, font = small, labelfont = bf]{caption}

\begin{document}

\blindtext

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.9\columnwidth]{example-image-a}
    \caption{This is a long figure caption, to show the problem with the width option and variable length like columnwidth and linewidth}
    \label{figlabel}
\end{figure}

\blindtext

\end{document}

如果目标是使标题宽度适合图形宽度,则可以改用\captionbox

\documentclass{scrartcl}

\usepackage{blindtext}
\usepackage{graphicx}
\usepackage[font = small, labelfont = bf]{caption}

\begin{document}

\blindtext

\begin{figure}[htbp]
    \centering
    \captionbox
      {This is a long figure caption, to show the problem with the width option and variable length like columnwidth and linewidth\label{figlabel}}
      {\includegraphics[width=0.9\columnwidth]{example-image-a}}
\end{figure}

\blindtext

\end{document}

calcwidth\captionbox在软件包版本 3.2(2011/07/30)中引入caption,但遗憾的是它们尚未在文档中提及,仅在变更日志 [¹] 中提及。(目前我正在更新文档,但进展缓慢。)

要了解其\captionbox工作原理,请查看subcaption文档。\subcaptionbox在那里描述,并且\subcaptionbox其行为与非常相似\captionbox,第一个用于排版子图形和子表的内容,第二个用于排版图形和表格的内容。

[1]http://mirror.ctan.org/macros/latex/contrib/caption/CHANGELOG

答案2

caption包和之间似乎存在冲突koomascript,它有自己的格式化标题的工具。如果您希望标题宽度等于图形宽度,则可以使用 的measuredfigure环境threeparttable

\documentclass{scrartcl}

\usepackage{lipsum}
\usepackage{graphicx}
\setkomafont{caption}{\small}
\setkomafont{captionlabel}{\bfseries}
\usepackage{threeparttable}

\begin{document}

\lipsum[2]
\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.9\columnwidth]{example-image-a}
  \caption{This is a long figure caption, to show the problem with the width option and variable length like columnwidth and linewidth}
  \label{figlabel}
\end{figure}

\lipsum[3]
\newpage
\lipsum[2]
\begin{figure}[!htbp]
  \centering
  \begin{measuredfigure}
    \includegraphics[width=0.9\columnwidth]{example-image-a}
    \caption{This is a long figure caption, to show the problem with the width option and variable length like columnwidth and linewidth}
    \label{figlabel}
  \end{measuredfigure}
\end{figure}
\lipsum[3]

\end{document} 

在此处输入图片描述 在此处输入图片描述

相关内容