# color an entry in the list of figures

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

-

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).

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

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