我对 LoF 做了一些修改,以便页码不在右边距处用虚线引出,而是在标题后留出一个短间隙。现在我想一标题以点结尾,大多数情况下应该打印在图形下方无点。为此,我设法重新定义\cftfigureformatpnum
了包含点。但我实际上希望点是有条件的,因为标题确实以点(或可能是其他标点符号)结尾,类似于如何biblatex
。
\documentclass{memoir}
\usepackage{caption}
\usepackage{calc}
%Some formatting of the figure captions
\DeclareCaptionLabelSeparator*{qquad}{\qquad}
\DeclareCaptionStyle{mystyle}%
{font=small,labelfont=sc,textfont=it,format=hang,labelsep=qquad}
\captionsetup{style=mystyle}
\renewcommand*{\figurename}{figure}
%Some formatting of the LoF
\renewcommand\printloftitle[1]{\section*{#1}}
\renewcommand\afterloftitle{\vspace{\baselineskip}{\small\textsc{Figure\quad Caption}\\}}
\setlength{\cftfigurenumwidth}{\widthof{\small{\textsc{Figure\quad}}}}
\renewcommand{\cftfigureleader}{}
\renewcommand{\cftfigureafterpnum}{\cftparfillskip}
%Make a dot before the page number
\makeatletter
\renewcommand*{\cftfigureformatpnum}[1]{%
\cftfigureformatpnumhook{#1}%
\hbox to \@pnumwidth{.\hfil{\cftfigurepagefont #1}}}
\makeatother
\begin{document}
\begin{figure}
\caption{something}
\end{figure}
\begin{figure}
\caption{something a bit longer, longer, longer, longer and longer, and longer, and longer, and longer}
\end{figure}
\begin{figure}
\caption{If I already have a dot it gets ugly.}
\end{figure}
\listoffigures
\end{document}
答案1
这样做\usepackage{amsthm}
有一个有趣的\@addpunct
宏,并将的当前值放在\spacefactor
里面\hbox
:
\usepackage{amsthm}
%Make a dot before the page number
\makeatletter
\renewcommand*{\cftfigureformatpnum}[1]{%
\edef\savespacefactor{\the\spacefactor}%
\cftfigureformatpnumhook{#1}%
\hbox to \@pnumwidth{%
\spacefactor\savespacefactor\@addpunct{.}%
\hfil{\cftfigurepagefont #1}}}
\makeatother
不过,我会将宏简化为
%Make a dot before the page number
\makeatletter
\renewcommand*{\cftfigureformatpnum}[1]{%
\@addpunct{.}\nolinebreak\hspace{1em}{\cftfigurepagefont #1}%
}
\makeatother
您感兴趣的是标题文本和页码之间的固定间距,而不是当页码有多个数字时该间距会减少。
默认情况下,\cftfigureformatpnumhook
不执行任何操作。
请注意,\@addpunct{.}
仅当不标点符号结束标题文本。因此,您可以使用问号、感叹号、冒号或分号,但不会添加句号。
答案2
我使用xstring
软件包解决了这个问题,该软件包包含大量处理字符串的功能。IfEndWith
和StrGobbleRight
命令特别有用。 包含软件包后,我caption
使用以下内容重新定义了该函数:
%Renew the caption command
\let\oldcaption=\caption
\renewcommand{\caption}[1]{%
\IfEndWith{#1}{.}{
\StrGobbleRight{#1}{1}[\output]
\oldcaption[\output]{\output{}.}
}{
\oldcaption{#1}
}
}
我已将您的 MWE 更新为以下内容:
\documentclass{memoir}
\usepackage{caption}
\usepackage{calc}
\usepackage{xstring}
%Some formatting of the figure captions
\DeclareCaptionLabelSeparator*{qquad}{\qquad}
\DeclareCaptionStyle{mystyle}%
{font=small,labelfont=sc,textfont=it,format=hang,labelsep=qquad}
\captionsetup{style=mystyle}
\renewcommand*{\figurename}{figure}
%Some formatting of the LoF
\renewcommand\printloftitle[1]{\section*{#1}}
\renewcommand\afterloftitle{\vspace{\baselineskip}{\small\textsc{Figure\quad Caption}\\}}
\setlength{\cftfigurenumwidth}{\widthof{\small{\textsc{Figure\quad}}}}
\renewcommand{\cftfigureleader}{}
\renewcommand{\cftfigureafterpnum}{\cftparfillskip}
%Renew the caption command
\let\oldcaption=\caption
\renewcommand{\caption}[1]{%
\IfEndWith{#1}{.}{
\StrGobbleRight{#1}{1}[\output]
\oldcaption[\output]{\output{}.}
}{
\oldcaption{#1}
}
}
%Make a dot before the page number
\makeatletter
\renewcommand*{\cftfigureformatpnum}[1]{%
\cftfigureformatpnumhook{#1}%
\hbox to \@pnumwidth{.\hfil{\cftfigurepagefont #1}}}
\makeatother
\begin{document}
\begin{figure}
\caption{something}
\end{figure}
\begin{figure}
\caption{something a bit longer, longer, longer, longer and longer, and longer, and longer, and longer}
\end{figure}
\begin{figure}
\caption{If I already have a dot it gets ugly.}
\end{figure}
\listoffigures
\end{document}
您会注意到,多余的部分.
已经消失在不应该存在的地方。