How to redefine the caption command with optional arguments

I'm writing a thesis that has double spacing on by default throughout the document. However, captions on figures need to be singlepaced. I can do that like this

\normalsize \singlespace \caption{ bla bla bla}


where singlespace is defined as:



This gets old, so I want to redefine caption, which I did like this:

\let\savedCaption=\caption
\renewcommand{\caption}[1]{\normalsize\singlespace\savedCaption{#1}}


This works fine, but sometimes I want to specify the optional tablename for my list of tables. So I do this,

\renewcommand{\caption}[2][]{\normalsize\singlespace\savedCaption[#2]{#1}}


Unfortunately, this will default the table name to be empty. The behavior I really want is this:

\renewcommand{\caption}[2][#1]{\normalsize\singlespace\savedCaption[#2]{#1}}


Can anyone help me get caption redefined properly?

-

migrated from stackoverflow.comMay 5 '11 at 19:04

This question came from our site for professional and enthusiast programmers.

Instead of redefining the \caption macro in order to use a custom \singlespace macro, I would use the setspace package. It combines double-spacing for "normal" text with single-spacing for captions.

\documentclass{article}

\usepackage[doublespacing]{setspace}

\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\blindtext

\begin{figure}[!ht]
\centering
\rule{1cm}{1cm}
\caption{\blindtext}
\end{figure}

\end{document}


(The blindtext package is only used to add some dummy text to the example.)

-

You can use also the Caption package, which gives you many options to customize the captions of floating environments.

Just as an example, I used in my thesis the following command in the preamble:

\usepackage[small,labelfont=bf,hang]{caption}


which made all the captions to be printed with a smaller font than the document, with bold font, and with the hang option I indented them so that they hang after the first line of text.

-

Lockstep showed already a solution for singlespaced captions in doublespaced text. However, I would like to answer your specific question about redefining \caption in a way such that optional arguments are allowed.

\let\savedCaption=\caption
\renewcommand*{\caption}[2][\shortcaption]{%
\def\shortcaption{#2}
\normalsize\singlespace
\savedCaption[#1]{#2}}


With this code, if no optional argument has been given, #1 will be \shortcaption which is defined later to have the value of #2. If an optional argument has been given, this would be used and \shortcaption would simply be ignored.

-
just a quick note to say that I get a blank line appearing in the output if I don't have some more comments inside this macro to eat the whitespace. works great otherwise! –  Sam Mason Oct 15 '13 at 11:25