在每个悬挂标题的末尾强制使用句号

在每个悬挂标题的末尾强制使用句号

我正在尝试在我的 LaTeX 文档中的每个标题末尾强制使用句号。我发现这个帖子基本上有相同的问题,但是在使用\usepackage{caption}以及悬挂字幕时它会中断。

我的目标是

  1. 在每个标题后附加“。”(当标题未以句号结尾时)
  2. 使用悬挂字幕

我尝试了不同的方法,比如重新定义\caption命令。我能得到的最接近的方法是复制粘贴hanggitlab 中的原始标题声明并手动添加句号。请注意,我使用“ABC”来进一步强调它 - 最后我可以简单地将其替换为\@addpunkt{.}。此解决方案的问题是,它将“ABC”写入新行,这当然不是我想要的。

\usepackage{caption} % Use Caption Package
\usepackage{amsthm} % Adds a fullstop at the end of each caption automatically (if there is none)

\makeatletter
% Copied and adapted from "https://gitlab.com/axelsommerfeldt/caption/-/blob/master/source/caption3.dtx"
% Custom caption format based on the original hang format with "ABC" appended
\DeclareCaptionFormat{customhang}{%
  \caption@iflabelseparatorwithnewline
    {\caption@Error{%
       The option `labelsep=\caption@labelsep@name' does not work\MessageBreak
       with `format=customhang'}}%
    {\@hangfrom{#1#2}%
     \advance\caption@parindent\hangindent\relax
     \advance\caption@hangindent\hangindent\relax
     \caption@@par#3ABC\par}} % "ABC" is just a placeholder for "\@addpunct{.}"
\makeatother
\captionsetup{format=customhang}

这是一个简单的例子:

\documentclass{article}
\usepackage{graphicx} % Necessary if you are dealing with figures

\usepackage{amsthm} % Adds a fullstop at the end of each caption automatically (if there is none)
\usepackage{caption} % Use Caption Package
\usepackage{xltabular}  % For captions within xltabular testing

\makeatletter
% Custom caption format based on the original hang format with "ABC" appended
\DeclareCaptionFormat{customhang}{%
  \caption@iflabelseparatorwithnewline
    {\caption@Error{%
       The option `labelsep=\caption@labelsep@name' does not work\MessageBreak
       with `format=customhang'}}%
    {\@hangfrom{#1#2}%
     \advance\caption@parindent\hangindent\relax
     \advance\caption@hangindent\hangindent\relax
     \caption@@par#3ABC\par}} % "ABC" is just a placeholder for "\@addpunct{.}"
\makeatother
\captionsetup{format=customhang}

\begin{document}


\begin{figure}
    \centering
    \caption{This is an example caption}
    \caption{Another sample with a very long caption and we hope that it will properly break into the next line with proper hanging...}
\end{figure}

您有什么建议吗?或者我能否更简单地实现此目的?谢谢

答案1

主要问题是,#3in\DeclareCaptionFormat指的是一组相当复杂的标记,包含标题文本,但嵌入在几个命令中。

我的建议是重新定义\caption以通过最终考试\@addpunct{.}

\documentclass{article}
\usepackage{graphicx} % Necessary if you are dealing with figures

\usepackage{amsthm} % Adds a fullstop at the end of each caption automatically (if there is none)
\usepackage{caption} % Use Caption Package

\makeatletter
% Custom caption format based on the original hang format with "ABC" appended
\DeclareCaptionFormat{customhang}{%
  \caption@iflabelseparatorwithnewline
    {\caption@Error{%
       The option `labelsep=\caption@labelsep@name' does not work\MessageBreak
       with `format=customhang'}}%
    {\@hangfrom{#1#2}%
     \advance\caption@parindent\hangindent\relax
     \advance\caption@hangindent\hangindent\relax
     \caption@@par#3}}
\NewDocumentCommand{\HIGHNOONcaption}{sO{#3}m}{%
  \IfBooleanTF{#1}{%
    \CAPTIONcaption*[#2\@addpunct{.}]{#3\@addpunct{.}}%
  }{%
    \CAPTIONcaption[#2\@addpunct{.}]{#3\@addpunct{.}}%
  }%
}
\makeatother

\captionsetup{format=customhang}


\AtBeginDocument{%
  \NewCommandCopy\CAPTIONcaption\caption
  \RenewCommandCopy\caption\HIGHNOONcaption
}

\begin{document}

\begin{figure}
    \centering
    %\includegraphics{example-image} % we don't really need it for the example
    \caption{This is an example caption}
    \caption{Another sample with a very long caption and we hope 
      that it will properly break into the next line with proper hanging.}
    \caption{Another sample with a very long caption and we hope 
      that it will properly break into the next line with proper hanging\dots}
    \caption{Another sample with a very long caption and we hope 
      that it will properly break into the next line with proper hanging?}
\end{figure}

\end{document}

在此处输入图片描述

相关内容