Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 18 down vote accepted

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}
share|improve this answer
    
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
add comment

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.

share|improve this answer
4  
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
add comment

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.

share|improve this answer
    
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
    
I also made a mistake and didn't use the \begingroup ... \endgroup but tried \renewcommand{\captionperiod}{} ... \renewcommand{\captionperiod}{.}. But now it works! Thanks! –  gablin Sep 6 '11 at 13:06
add comment

Ideal task for luatex

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

\removelast{What punctuation?}

\bye

You can easily make it more robust and remove the last character only if it is a punctuation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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