# Interaction of hyperref, capt-of and redefinition of @makecaption [duplicate]

Possible Duplicate:
Remove colon in the caption of a figure without using caption package

This is a very tough question and I have posted minimum example below.

1. Because I don't want my figures to float I use center environment and capt-of package.
2. I have tweaked @makecaption in a way that there is only figure name (no colon) displayed when figure name is empty.
3. I also use hyperref package.

When working all three together, I get bunch of "Extra \else." errors. Can you find the problem? Maybe I have redefined @makecaption badly?

Regards.

\documentclass{article}
\usepackage{capt-of}

\usepackage{hyperref}

\makeatletter
% tweaked article.cls @makecaption: no caption text no ":"
\newcommand*{\captionlabeldelim}{}
\long\def\@makecaption#1#2{%
\ifx #2\ignorespaces
\renewcommand*{\captionlabeldelim}{}
\else
\renewcommand*{\captionlabeldelim}{:\ }
\fi
\vskip\abovecaptionskip
\sbox\@tempboxa{#1\captionlabeldelim #2}%
\ifdim \wd\@tempboxa >\hsize
#1\captionlabeldelim #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
\makeatother

\begin{document}

\begin{center}
\includegraphics[clip]{bla.eps}
\captionof{figure}{x}
\end{center}%

\begin{center}
\includegraphics[clip]{bla.eps}
\captionof{figure}{}
\end{center}%

\end{document}

-

## marked as duplicate by Mico, Marco Daniel, cmhughes, percusse, Martin SchröderAug 14 '12 at 23:05

Why don't you use the package caption? It's much easier instead of redefining something. –  Marco Daniel Aug 10 '12 at 13:50
@MarcoDaniel I hate using the whole package with dozen of commands for only one small purpose... –  Pygmalion Aug 10 '12 at 14:23
@Pygmalion The reason people use packages is that the LaTeX kernel is very inflexible. Well-constructed packages address this and the interactions which can occur with other packages. Thus 'simple' changes are often anything but at the code level. –  Joseph Wright Aug 10 '12 at 15:25
@JosephWright OK, got the message. Yet, Mico's little solution is working. –  Pygmalion Aug 10 '12 at 16:04

As addressed in an answer to a similar, but not entirely identical question, you can achieve your objective of removing the colon if the argument of \caption (or \caption-of) is empty by re-defining \@makecaption. The following modified form of your MWE shows how this may be done.

\documentclass{article}
\usepackage{capt-of}
\usepackage[demo]{graphicx} % [demo] needed just for this MWE

\makeatletter
\long\def\@makecaption#1#2{% % #2 is argument of \caption and \captionof command
\vskip\abovecaptionskip
\sbox\@tempboxa{#2}        % place #2 into a scratch TeX box
\ifdim \wd\@tempboxa = 0pt % test if scratch box has zero width
\centering #1 \par       % if yes, typeset only #1 (the float's name and number)
\else                      % else, proceed with default definition
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\fi
\vskip\belowcaptionskip}
\makeatother

\begin{document}
\begin{center}
\includegraphics[clip]{bla.eps}
\captionof{figure}{xyz} \label{fig:bla1}
\end{center}%

\begin{center}
\includegraphics[clip]{bla.eps}
\captionof{figure}{} \label{fig:bla2}
\end{center}%

And here are cross-references to \autoref{fig:bla1} and \autoref{fig:bla2}.
\end{document}


-
OK, this works. Thanks! Is there a simple explanation why my redefinition did not work? –  Pygmalion Aug 10 '12 at 16:00
@Pygmalion -- the main problem with your MWE is that \ifx tests for equality of both character and category codes of the items being compared. However, \ignorespaces (which is, after all, a macro) cannot be equal, in this very specific sense, to #2 unless the argument of the \caption or \captionof command happens to be the macro \ignorespaces -- not likely to be the case, right? In contrast, my MWE tests whether a "box" containing #2 has zero width -- which will be true if the argument of \captionof is empty. –  Mico Aug 10 '12 at 17:04