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.

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}
share|improve this question
1  
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
2  
@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
add comment

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 1 down vote accepted

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
\usepackage[colorlinks]{hyperref}

\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}

enter image description here

share|improve this answer
    
OK, this works. Thanks! Is there a simple explanation why my redefinition did not work? –  Pygmalion Aug 10 '12 at 16:00
1  
@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
add comment

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