6

Is it possible to change the caption color in a single figure without changing the color labels of other figures?

I have used this code Change color of figure caption text but every figure is changing its color. I want to do it for only one figure.

My code is mainlyfrom Change color of figure caption text

\documentclass[12pt,twoside]{report}
\usepackage{xcolor}
\usepackage{caption}
\usepackage[labelfont={color=red}]{caption}

\begin{document}

\chapter{Test chapter}

\begin{figure}
\centering
A
\caption{Test figure A}
\end{figure}

\begin{figure}
\centering
B
\caption{Test figure B}
\end{figure}

\end{document}
  • Could you post what you've tried? – Bernard Feb 14 '19 at 19:42
  • Do you want to change only color of caption or also the Figure and number? For 1st case, \caption[Test figure A]{\textcolor{red}{Test figure A}} should work. – Sigur Feb 14 '19 at 19:47
  • Both the caption and the number, but I mostly care about the number (with Fig.). – zdm Feb 14 '19 at 19:48
  • To change everything, you can try {\color{red}\caption{text}}. – Sigur Feb 14 '19 at 20:10
5

In my point of view \captionsetup{labelfont={color=blue}} etc. in the local environment is sufficient:

enter image description here

\documentclass[12pt,twoside]{report}
\usepackage{xcolor}
\usepackage[labelfont={color=red}]{caption}

\begin{document}

\chapter{Test chapter}

\clearpage

\begin{figure}
\captionsetup{labelfont={color=blue}}
A
\caption{Test figure A}
\end{figure}

\begin{figure}
B
\caption{Test figure B}
\end{figure}

\end{document}
  • 1
    Glad that removed my answer before you answer this... Already feel somehow stupid (+1 already) – koleygr Feb 14 '19 at 21:18
  • 1
    @koleygr: Thanks, no need to feel stupid, however. Good packages (and caption is a very good one, in my point of view) provide such local group-aware \...setup like macro, that simplifies customization... – user31729 Feb 14 '19 at 21:24
4

Here's a start. For the caption itself, you could do as the comments suggest and place a \textcolor in the \caption argument. The number (like "1.2" is contained in \thefigure, while the descriptor word "Figure" is kept in \figurename.

kolygr correctly points out that I don't really need to renew \thefigure, because the color change applied before \figurename will carry through. I will leave the code as is though, in case one gets ideas about changing \figurename and \thefigure to two separate colors.

\documentclass[12pt,twoside]{report}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{caption}
\let\svthefigure\thefigure
\let\svfigurename\figurename
\newcommand\figcolor[1]{%
  \renewcommand\thefigure{\bfseries\sffamily\color{#1}\svthefigure}
  \renewcommand\figurename{\bfseries\sffamily\color{#1}\svfigurename}
}
\begin{document}

\chapter{Test chapter}

\begin{figure}
\centering
A
\caption{Test figure A}
\end{figure}
\begin{figure}
\figcolor{blue}
\centering
B
\caption{Test figure B}
\end{figure}
\begin{figure}
\centering
B
\caption{Test figure B}
\end{figure}

\end{document}

enter image description here

kolygr also points out that the above code will retain the color in \ref attributes. That can be avoided by storing the figure markup in a separate markup macro, and \protecting it so that the expansion (with color) doesn't get written to the aux file:

\documentclass[12pt,twoside]{report}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{caption}
\let\svthefigure\thefigure
\let\svfigurename\figurename
\renewcommand\thefigure{\protect\myfigmarkup\svthefigure}
\renewcommand\figurename{\protect\myfigmarkup\svfigurename}
\let\myfigmarkup\relax
\newcommand\figcolor[1]{%
  \def\myfigmarkup{\bfseries\sffamily\color{#1}}%
}
\begin{document}
In Figure~\ref{fg1.2}...

\chapter{Test chapter}

\begin{figure}
\centering
A
\caption{Test figure A}
\end{figure}
\begin{figure}
\figcolor{blue}
\centering
B
\caption{Test figure B}
\label{fg1.2}
\end{figure}
\begin{figure}
\centering
B
\caption{Test figure B}
\end{figure}

\end{document}
  • You may replace \color by \textcolor or a similar change because the color will be maintained in a possible reference.... (+1... I learn something new again from you!) – koleygr Feb 14 '19 at 20:24
  • I didn't thought about that... I just tested with a previous version of mine by adding "Figure~\ref{label} is red" and the text "is red" was red too... So, you need to enclose \color command inside braces or use \textcolor instead to avoid the color be maintained after a possible reference (that is also another problem if the op want it in black and not colored]) – koleygr Feb 14 '19 at 20:40
  • @koleygr I see. Let me think on that more. – Steven B. Segletes Feb 14 '19 at 20:52
  • @koleygr Thank you again for pointing that out. I added an addendum to address it, depending on what the OP wants. – Steven B. Segletes Feb 14 '19 at 20:57
  • Just realized how close was my code to yours and deleted my answer... I didn't mentioned that your \svthefigure was defined by a \let command like mine.... I thought it had to do with something internal to the figure and tried to learn from it... But finally my answer had nothing more to offer than yours and deleted – koleygr Feb 14 '19 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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