但是他们使用 KOMA,我使用report
class。我理解它应该只需简单地使用:
\usepackage[compatibility=false]{caption}
\makeatletter
\let\x@caption\caption % original \caption
\def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}} % with optional arg
\def\x@@@caption#1{\x@caption[{#1}]{#1}} % without optional arg
\def\caption{\@ifnextchar[\x@@caption\x@@@caption} % new \caption
\makeatother
但事实并非如此。尽管我收到了警告(正如预期的那样)Package caption Warning: Forced redefinition of \caption since the unsupported(!) package option `compatibility=false' was given.
,但生成的文档并未显示可选文本。
这是软件包的某些最新更新caption
,不再适用于给定的代码吗?以下是真的最小工作示例。我还没有添加子标题或任何其他可能破坏它的自定义。
\documentclass[12pt]{report}
\usepackage{graphicx}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}
\makeatletter
\let\x@caption\caption % original \caption
\def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}} % with optional arg
\def\x@@@caption#1{\x@caption[{#1}]{#1}} % without optional arg
\def\caption{\@ifnextchar[\x@@caption\x@@@caption} % new \caption
\makeatother
\begin{document}
\listoffigures
\begin{figure}
\includegraphics[width=.5\linewidth]{example-image-a}
\caption[Optional]{mandatory}
\end{figure}
\end{document}
答案1
标题包有一些\AtBeginDocument
钩子,可以检测\caption
命令是否已从其原始定义发生改变。
一种解决方法是重新定义\caption
\AtBeginDocument
:
\documentclass[12pt]{report}
\usepackage{graphicx}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}
\makeatletter
\let\x@caption\caption % original \caption
\long\def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}}% with optional arg
\def\x@@@caption#1{\x@caption[{#1}]{#1}}% without optional arg
\AtBeginDocument{%
\def\caption{\@ifnextchar[\x@@caption\x@@@caption}% new \caption
}
\makeatother
\begin{document}
\listoffigures
\begin{figure}
\includegraphics[width=.5\linewidth]{example-image-a}
\caption[Optional]{mandatory}
\end{figure}
\end{document}