# Remove trailing period from captions in ToC

Every figure, table, formula and listing in my document has a caption that ends with a period. The problem is, that these periods are also displayed in the ToC, which they shouldn't.

I found a similar question which had the goal of removing trailing periods from the section numbers by preventing them from getting written to the .aux file:

\makeatletter
\def\remove@@dot\csname the#1\endcsname{\Roman{#1}}
\def\p@section{\remove@@dot}
\makeatother


However, I haven't figured out a way to do the same for the captions themselves - that being said, I am pretty new to LaTeX. Of course it would be possible to provide a second argument without the period to the \caption command to specify a short spelling, that is used for the ToC, like this: \caption[Some caption]{Some caption.}, but these are redundancies I'd rather avoid.

MWE:


So I found a solution that works, but definitely isn't perfect.

For figures and tables I override the default \caption command to gobble the trailing period, if present, but only for the ToC entry.

\usepackage{xstring}
\usepackage{etoolbox}

\let\Caption\caption
\renewcommand{\caption}[2][]{%
\ifstrempty{#1}{%
\IfEndWith{#2}{.}{%
\StrGobbleRight{#2}{1}[\result]%
\Caption[\result]{#2}%
}{%
\Caption[#2]{#2.}%
}%
}{\Caption[#1]{#2}}%
}


The same goes for the equation captions, with the only difference being that the command is already custom, so that wasn't a problem.

\newcommand{\eqcaption}[1]{%
\IfEndWith{#1}{.}{%
\StrGobbleRight{#1}{1}[\result]%
}{%
}\par%
}


Finally for the listings I tried to patch the underlying TeX macro via xpatch but couldn't get it to work, so instead I added a new command that defines two values which I use for the caption parameter - one with the period and the other one without it.

\newcommand{\lstcaption}[1]
{\IfEndWith{#1}{.}{%
\StrGobbleRight{#1}{1}[\result]%
\def\lsttoccap{\result}%
\def\lstcap{#1}}
{\def\lsttoccap{#1}%
\def\lstcap{#1.}}}

\lstcaption{Some code}
\begin{lstlisting}[caption={[\lsttoccap]\lstcap}]
\end{lstlisting}


I also added the functionality that a period will automatically be appended to the caption, if it wasn't explicitly specified.