9

I want to change the text according to its current font color.

I can test argument against font family

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage[scaled=0.84]{beramono}
\usepackage{pdftexcmds}

\makeatletter
\newcommand{\MyChange}[1]{%
\ifnum\pdf@strcmp{\f@family}{\ttdefault}=\z@%
{\bfseries #1}%
\else%
{\sffamily #1}%
\fi%
}%
\makeatother

\begin{document}
\noindent       First:\MyChange{Normal should become SF}\\
{\color{red}    Second:\MyChange{Red should become SF}}\\
{\tt            Third:\MyChange{tt should be bold}}\\
{\tt\color{red} Fourth:\MyChange{Red tt should be bold}}
\end{document}

I wish I could have something like this

\makeatletter
\newcommand{\MyChange}[1]{%
\ifnum\pdf@strcmp{\f@color}{red}=\z@%
{\bfseries\large #1}%
\else%
{\sffamily\scriptsize #1}%
\fi%
}%
\makeatother

But I get error

! Undefined control sequence.
\MyChange #1->\ifnum \pdf@strcmp {\f@color 
                                           }{red}=\z@ {\bfseries \large #1}\...
l.19 ...  First:\MyChange{Normal should become SF}
                                                  \\
1

2 Answers 2

11

Package xcolor can extract the color definition in a macro: \extractcolorspec{<color>}{<macro>}. This can be used to define a test to compare the current color . with red:

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage[scaled=0.84]{beramono}

\makeatletter
\newcommand{\MyChange}[1]{%
  \extractcolorspec{.}\MyChange@CurrentColor
  \extractcolorspec{red}\MyChange@TestColor
  \ifx\MyChange@CurrentColor\MyChange@TestColor
    {\bfseries\large #1}%
  \else
    {\sffamily\scriptsize #1}%
  \fi%
}
\makeatother

\begin{document}
\noindent       First: \MyChange{Normal should become SF and small}\\
{\color{red}    Second: \MyChange{Red should become bold and large}}\\
{\tt            Third: \MyChange{tt should be SF and small}}\\
{\tt\color{red} Fourth: \MyChange{Red tt should be bold and large}}
\end{document}

Result

5

You can use the fact that a color's definition is kept in the macro

\csname\string\color@<colorname>\endcsname

and you can inspect the current color with . for <colorname>, as explained in https://tex.stackexchange.com/a/36163/4427

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage[scaled=0.84]{beramono}
\usepackage{pdftexcmds}

\makeatletter
\newcommand{\color@check}[1]{%
  \expandafter\meaning\csname\string\color@#1\endcsname
}
\newcommand{\MyChange}[1]{%
  \ifnum\pdf@strcmp{\color@check{.}}{\color@check{red}}=\z@
    {\bfseries\large #1}%
  \else
    {\sffamily\scriptsize #1}%
  \fi
}
\makeatother

\begin{document}
\noindent       First:\MyChange{Normal should become SF}\\
{\color{red}    Second:\MyChange{Red should become bold and large}}\\
{\tt            Third:\MyChange{tt should be SF}}\\
{\tt\color{red} Fourth:\MyChange{Red tt should be bold and large}}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .