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
- Append "." after each caption (when it doesn't already end with a full stop)
- 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

captionof, right? (at least as far as I can tell) – Highnoon Nov 16 '23 at 09:53xltabularwants to change the meaning of\captionand 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