为图形列表中的条目着色

为图形列表中的条目着色

我有一个打印图像的命令,将在特定环境中使用。

首先我运行测试,看看文件是否存在并且是否是有效的图像文件(参见这个问题的定义\imagetest)。
如果可以,则只打印图像,否则我将打印一个带有信息性消息的框。

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

到目前为止,它工作正常,但我想还打印标题红色_在列表中...
我发现的是

\captionsetup{font={color=red}}

我可以更改标题的外观,但只能更改文档中当前位置的标题。如果我这样做

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

进而

\captionsetup{listformat=mystyle}

它实际上改变了 \listof,但只有参考编号被着色。

所以问题是:我该如何为这种浮动环境的列表条目着色?
实际上,我也会接受其他合适的方法来突出显示此类条目

答案1

你的\DeclareCaptionListFormat代码让 LaTeX 可以编写

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

.lof文件中。使用\color{red}#1#2不会有帮助,因为的参数\numberline是按组排版的,因此颜色设置不会传播到标题文本。

可以使用一个技巧:

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

因此第一个标题将导致写作

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

.lof文件中。当 LaTeX 排版时,\makered会变成\makeredaux用红色打印数字,然后\aftergroup\makeredaux会在\numberline组完成时执行,也就是当 LaTeX 排版标题文本(在图片列表中)时。

在此处输入图片描述

(注意:图像中的图形位于顶部,因为此示例文档中未指定定位参数。)

相关内容