0

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:

\documentclass[a4paper,11pt]{article}
\usepackage{tocloft}
\usepackage{listings}


%% Setup for list of equations
%% ========================================
\newcommand{\listequationsname}{Equations}
\newlistof{equations}{equ}{\listequationsname}
\newcommand{\eqcaption}[1]{%
    \addcontentsline{equ}{equations}{\protect\numberline{\theequation}#1}\par%
}


%% Actual document
%% ========================================
\begin{document}
\listoffigures
\listoftables
\listofequations
\lstlistoflistings

%% Example elements with captions ending in a period
\begin{figure}[h]
    \caption{Some figure.}
\end{figure}\noindent

\begin{table}[h]
    \caption{Some table.}
\end{table}

\begin{equation}
\end{equation}\eqcaption{Some equation.}

\begin{lstlisting}[caption={Some code.}]
\end{lstlisting}

\end{document}
0

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]%
        \addcontentsline{equ}{equations}{\protect\numberline{\theequation}\result}%
    }{%
        \addcontentsline{equ}{equations}{\protect\numberline{\theequation}#1}%
    }\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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.