# How to prevent last period in captions from appearing in the list of figures?

Is there any simple way of removing the last character of an argument to a command (or environment)? I'm looking for a simple way to remove the punctuation mark from a caption when it appears in the contents list without having to write the same caption twice.

-

If you are using the caption package, you can use the option textformat=period which will add a period to the caption itself but not to the list entry, e.g.:

\documentclass{article}
\usepackage[textformat=period]{caption}

\begin{document}

\listoffigures

\begin{figure}
\centering
\fbox{$$\sin^2x+\cos^2x\equiv1$$}
\caption{An identity}
\end{figure}

\end{document}

-
Changed to using this instead as I ran into problems with an unwanted space being introduced to the \captionperiod command. – gablin Sep 7 '11 at 20:04

Another solution is to patch \@caption, using tools provided by the etoolbox and xstring packages:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xstring}

\makeatletter
\patchcmd{\@caption}{%
\ignorespaces#2%
}{%
\protecting{\noexpandarg\StrGobbleRight}{\ignorespaces#2}{1}%
}{}{}
\makeatother

\begin{document}

\listoffigures

\begin{figure}
\centering
\fbox{$$\sin^2x+\cos^2x\equiv1$$}
\caption{A \emph{very important} identity!}
\end{figure}

\end{document}


The resulting output is:

Note that you must patch \@memold@caption instead of \@caption if you use the memoir document class without the hyperref package. Furthermore, the patch might have to be adapted if the caption package were used. However, as follows from the answer and the comment of Axel Sommerfeldt, a patch is actually not needed in this case since the caption package allows to configure the format of the caption text anyway.

-
When using the caption package the best thing would be defining an own text format with \DeclareCaptionTextFormat. – Axel Sommerfeldt Sep 6 '11 at 17:16
@mhp, could you please clarify (if possible) how to make your solution work together with capton package? All my subfigures are now visible in the LOF too :–) – Alexander Kachkaev Aug 26 '15 at 19:40

The safest way, in my opinion, is to use a command for the final period:

...
\DeclareRobustCommand{\captionperiod}{.}
...
\begin{document}
...
\begingroup
\renewcommand{\captionperiod}{}
\listoffigures
\listoftables
... % other lists
\endgroup
...
\begin{figure}
...
\caption{A caption\captionperiod}
\end{figure}
...
\end{document}


In this way you have a precise control on what you type.

-
That's freaking genius! Never thought of that! Thanks! – gablin Sep 6 '11 at 12:34
Hm, doesn't appear to be working when I'm specifying the caption as argument to a wrapper environment, probably due to expansion. Nope, it's not even working when specified directly into caption. – gablin Sep 6 '11 at 12:44
I made a mistake; check the edited message. – egreg Sep 6 '11 at 12:47

\def\removelast#1{\directlua{tex.print((string.gsub([==[#1]==], ".\$", "")))}}