我正在写一篇论文,试图将图片列表重新用作图例部分。我希望每个图片底部都有类似“图 X:标题”的内容,图片列表条目包含“图 X:标题”,后面是新行上的图例。
我几乎已经使用 tocloft 包得到了我想要的东西,如下所示:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage[titles]{tocloft}
\addtocontents{lof}{\cftpagenumbersoff{figure}}
\renewcommand\listfigurename{Figure Legends}
\renewcommand\cftfigpresnum{\bfseries Figure }
\renewcommand\cftfigaftersnum{\bfseries :}
\renewcommand\cftfigaftersnumb{\\}
\renewcommand{\cftdotsep}{\cftnodots}
\setlength\cftbeforefigskip{12pt}
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering \includegraphics{image1}
\caption[Normally a much longer legend for the first figure.]{Title for first fig}
\end{figure}
\lipsum[2]
\clearpage
\listoffigures
\end{document}
请注意,我已将图例放在“短”标题中,将标题放在“长”标题中。我还没搞清楚的一件事是如何在图表列表中同时包含“短”和“长”标题。
我考虑使用 cftXaftersnum 命令将短标题作为变量包含在内,但我不确定如何执行此操作。
更新:
答案很精彩,但我忽略了一个额外的要求:我需要 doublecaption 与子图配合使用。我有一个带有两个子图的图,计数器会双倍增加。以下是使用该解决方案的示例:
\documentclass{article}
\usepackage{amsmath}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{subcaption}
\usepackage[titles]{tocloft}
\addtocontents{lof}{\cftpagenumbersoff{figure}}
\renewcommand\listfigurename{Figure Legends}
\renewcommand\cftfigpresnum{\bfseries Figure }
\renewcommand\cftfigaftersnum{\bfseries : }
\renewcommand{\cftdotsep}{\cftnodots}
\setlength\cftbeforefigskip{12pt}
\newlength\mylength
\settowidth\mylength{\cftfigpresnum}
\addtolength\cftfignumwidth{\mylength}
\makeatletter
\def\doublecaption{%
\ifx\@captype\@undefined
\@latex@error{\noexpand\caption outside float}\@ehd
\expandafter\@gobble
\else
\refstepcounter\@captype
\expandafter\@firstofone
\fi
{\@dblarg{\@doublecaption\@captype}}%
}
\long\def\@doublecaption#1[#2]#3{%
\par
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #3\newline#2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering \includegraphics{image1}
\doublecaption[Normally a much longer legend for the first figure.]{Title for first fig}
\end{figure}
\lipsum[2]
\begin{figure}[ht]
\centering
\begin{subfigure}{0.45\textwidth}
\includegraphics{image2}
\caption{Case one}
\end{subfigure}
\begin{subfigure}{0.45\textwidth}
\includegraphics{image3}
\caption{Case two}
\end{subfigure}
\doublecaption[Not very long legend for fig with subfigs.]{Title for fig with subfigs}
\end{figure}
\lipsum[3]
\clearpage
\listoffigures
\end{document}
我可以在 doublecaption 定义中看到计数器增加了:
\refstepcounter\@captype
我猜想 subcaption 包必须采取某种措施来防止计数器双重递增,但我不确定如何做到这一点。我总是可以在子图之后、但在图的双重标题之前明确减少列表计数器,但我怀疑有更好的方法?
答案1
这是一种方法。让我们定义一个新命令\doublecaption
,用于在 LoF 中实现该行为:
\makeatletter
\def\doublecaption{%
\ifx\@captype\@undefined
\@latex@error{\noexpand\caption outside float}\@ehd
\expandafter\@gobble
\else
\refstepcounter\@captype
\expandafter\@firstofone
\fi
{\@dblarg{\@doublecaption\@captype}}%
}
\long\def\@doublecaption#1[#2]#3{%
\par
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #3\newline#2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
该定义取自正常定义\caption
,并经过修改以满足您的需要。
在tocloft
设置中,删除
\renewcommand\cftfigaftersnumb{\\}
并添加
\newlength\mylength
\settowidth\mylength{\cftfigpresnum}
\addtolength\cftfignumwidth{\mylength}
梅威瑟:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage[titles]{tocloft}
\addtocontents{lof}{\cftpagenumbersoff{figure}}
\renewcommand\listfigurename{Figure Legends}
\renewcommand\cftfigpresnum{\bfseries Figure }
\renewcommand\cftfigaftersnum{\bfseries :}
%\renewcommand\cftfigaftersnumb{\\}
\renewcommand{\cftdotsep}{\cftnodots}
\setlength\cftbeforefigskip{12pt}
\newlength\mylength
\settowidth\mylength{\cftfigpresnum}
\addtolength\cftfignumwidth{\mylength}
\makeatletter
\def\doublecaption{%
\ifx\@captype\@undefined
\@latex@error{\noexpand\caption outside float}\@ehd
\expandafter\@gobble
\else
\refstepcounter\@captype
\expandafter\@firstofone
\fi
{\@dblarg{\@doublecaption\@captype}}%
}
\long\def\@doublecaption#1[#2]#3{%
\par
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #3\newline#2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering \includegraphics{image1}
\doublecaption[Normally a much longer legend for the first figure.]{Title for first fig}
\end{figure}
\lipsum[2]
\begin{figure}[ht]
\centering \includegraphics{image2}
\caption{Title for second fig}
\end{figure}
\clearpage
\listoffigures
\end{document}
输出:
编辑
下次请发布包含所有相关加载包的示例...
为了使其能够正常工作subcaption
,我们将定义两个命令:
\doublecaption
,用于没有子图的图。\doublesubcaption
,用于带有子图的图形中。
将上面的代码以下列代码开头\makeatletter
和结尾替换\makeatother
为以下内容(需要包etoolbox
):
\makeatletter
\let\doublecaption\caption%
\patchcmd{\doublecaption}%
{\@caption}%
{\@doublecaption}%
{}%
{}%
\let\@doublecaption\@caption%
\patchcmd{\@doublecaption}%
{\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}}%
{\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #3\newline#2}}}%
{}%
{}%
\let\doublesubcaption\doublecaption%
\patchcmd{\doublesubcaption}%
{\refstepcounter\@captype}%
{\relax}%
{}%
{}%
\makeatother
梅威瑟:
\documentclass{article}
\usepackage{amsmath}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{subcaption}
\usepackage[titles]{tocloft}
\addtocontents{lof}{\cftpagenumbersoff{figure}}
\renewcommand\listfigurename{Figure Legends}
\renewcommand\cftfigpresnum{\bfseries Figure }
\renewcommand\cftfigaftersnum{\bfseries : }
\renewcommand{\cftdotsep}{\cftnodots}
\setlength\cftbeforefigskip{12pt}
\newlength\mylength
\settowidth\mylength{\cftfigpresnum}
\addtolength\cftfignumwidth{\mylength}
\makeatletter
\let\doublecaption\caption%
\patchcmd{\doublecaption}%
{\@caption}%
{\@doublecaption}%
{}%
{}%
\let\@doublecaption\@caption%
\patchcmd{\@doublecaption}%
{\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}}%
{\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #3\newline#2}}}%
{}%
{}%
\let\doublesubcaption\doublecaption%
\patchcmd{\doublesubcaption}%
{\refstepcounter\@captype}%
{\relax}%
{}%
{}%
\makeatother
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering \includegraphics{image1}
\doublecaption[Normally a much longer legend for the first figure.]{Title for first fig}
\end{figure}
\lipsum[2]
\begin{figure}[ht]
\centering
\begin{subfigure}{0.45\textwidth}
\includegraphics{image2}
\caption{Case one}
\end{subfigure}
\begin{subfigure}{0.45\textwidth}
\includegraphics{image3}
\caption{Case two}
\end{subfigure}
\doublesubcaption[Not very long legend for fig with subfigs.]{Title for fig with subfigs}
\end{figure}
\lipsum[3]
\clearpage
\listoffigures
\end{document}
输出: