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.

I have a command that prints an image and will be used inside a certain environment.

First I run a test if the file exists and is a valid image file (see this question for the definition of \imagetest).
If it is ok then the image is just printed, otherwise I print a box with an informative message.

\newcommand{\musicSFE}[1]{
    \imagetest{#1}
    {% If it is a valid image do:

        \noindent
        \includegraphics{#1}

    }
    {% If it isn't a valid image do:

        \bigskip
        %TODO: Make the entry in the \listof...s red
        \framebox{\colorbox{yellow}
            {Image #1 missing}}

        \bigskip
    }
}

So far it works ok, but I would like to also print the caption red _in the list of ...
What I found is that with

\captionsetup{font={color=red}}

I can change the appearance of the caption, but only that at the current place in the document. If I do

\DeclareCaptionListFormat{mystyle}{\textcolor{red}{#1 #2}}

and then

\captionsetup{listformat=mystyle}

It actually changes the \listof, but only the reference number gets colored.

So the question is: How can I color the list entry of such a floating environment?
Actually I'd also accept a suitable other way to highlight such an entry

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Your \DeclareCaptionListFormat code makes LaTeX write

\contentsline {figure}{\numberline {\textcolor{red}{1}}{\ignorespaces This will be red\relax }}{1}

in the .lof file. Using \color{red}#1#2 wouldn't help, because the argument of \numberline is typeset in a group, so the color setting doesn't propagate to the caption text.

One can use a trick:

\documentclass{article}
\usepackage{color}
\usepackage{caption}

\DeclareRobustCommand{\makered}{\makeredaux\aftergroup\makeredaux}
\newcommand\makeredaux{\color{red}}

\DeclareCaptionListFormat{mystyle}{\makered#1 #2}
\begin{document}
\listoffigures
\begin{figure}
\captionsetup{listformat=mystyle}
\caption{This will be red}
\end{figure}
\begin{figure}
\caption{This will be black}
\end{figure}
\end{document}

Thus the first caption will cause writing

\contentsline {figure}{\numberline {\makered 1}{\ignorespaces This will be red\relax }}{1}

in the .lof file. When LaTeX typesets it, \makered becomes \makeredaux that prints the number in red, followed by \aftergroup\makeredaux that will be executed when the \numberline group is finished, that is, when LaTeX is typesetting the caption text (in the list of figures).

enter image description here

(Note: in the image the figures are at the top, because no positioning argument has been specified in this example document.)

share|improve this answer
    
Thank you, works perfectly. And I even think I understand what's working ;-) –  uli_1973 Feb 21 '13 at 0:13

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.