1

I am trying to enforce a full stop at the end of each caption in my LaTeX document. I found this post with basically the same question, but it breaks when using \usepackage{caption} as well as hanging captions.

So my goal is to

  1. Append "." after each caption (when it doesn't already end with a full stop)
  2. Make it work with hanging captions

I tried different things like redefining the \caption command. The closest I could get was by copy-pasting the original hang caption declaration from gitlab and adding the fullstop manually. Note that I'm using "ABC" to emphasise it even further - at the end I can simply replace that with \@addpunkt{.}. The issue with this solution is, that it writes the "ABC" into a new line which is of course not what I am looking for.

\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 withformat=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}

Here's a minimal example:

\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 withformat=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}

Do you have any suggestions, or can I maybe achieve this even simpler? Thanks

Highnoon
  • 45
  • 6

1 Answers1

1

The main problem is that #3 in \DeclareCaptionFormat refers to a quite complicated set of tokens that contains the caption text, but embedded in several commands.

My suggestion is to redefine \caption to pass a final \@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 withformat=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}

enter image description here

egreg
  • 1,121,712
  • Omg thats so clever, thank you very much! That also properly deals with captionof, right? (at least as far as I can tell) – Highnoon Nov 16 '23 at 09:53
  • Sorry, one quick follow-up: It doesn't seem to work when using it inside xltabular, which is quite weird from my POV? I've added this to the MWE of the above. – Highnoon Nov 16 '23 at 10:00
  • @Highnoon Well, xltabular wants to change the meaning of \caption and obviously conflicts arise. Please, never change the question to invalidate existing answers: if you have new problems, open a new topic. – egreg Nov 16 '23 at 10:15
  • Yeah, you're absolute right. I'll do it, sorry! – Highnoon Nov 16 '23 at 11:42