# How to color a fraction bar in mathematical mode?

when I use the solution given here, this is what I get:

\documentclass{article}

\usepackage[output-decimal-marker={,}]{siunitx}
\usepackage[right]{eurosym}

% from https://tex.stackexchange.com/a/110979/138900
\usepackage{amstext} % for \text
\DeclareRobustCommand{\officialeuro}{%
\ifmmode\expandafter\text\fi
{\fontencoding{U}\fontfamily{eurosym}\selectfont e}}

% from https://tex.stackexchange.com/q/149731/138900
\usepackage{xcolor}
\def\Frac#1#2{ #1 \color{blue}\above 0.4pt \normalcolor #2}
\makeatletter
\DeclareRobustCommand{\cbfrac}[3][OrangeRed]{{\begingroup#2\endgroup\color{#1}\@@over\normalcolor #3}}
\makeatother
\begin{document}
$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\frac{\EUR{18}}{\SI{1}{kg}}$
\bigskip

The same with color and Frac
\bigskip

$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\Frac{\EUR{18}}{\SI{1}{kg}}$
\end{document}

• What's the purpose in using the wrongly defined \Frac command? – egreg Nov 14 at 22:18
• @egreg What are you talking about? – AndréC Nov 14 at 22:22
• @AndréC Well, \cbfrac works a lot better. – egreg Nov 14 at 22:44
• @egreg I'll take your word for it. It emulates \frac who writes the text in small and I would like to be able to emulate \dfrac to write the text in larger. How do you do that? – AndréC Nov 14 at 22:55

I'd prefer the \cbfrac version. Here I present a slight modification based on \genfrac.

Note that the color in the denominator is the same as the current color, which would not be true with \normalcolor.

\documentclass{article}

\usepackage{amsmath}
\usepackage[output-decimal-marker={,}]{siunitx}
\usepackage[right]{eurosym}

% text doesn't mind if it's called in text mode
\DeclareRobustCommand{\officialeuro}{%
\text{\fontencoding{U}\fontfamily{eurosym}\selectfont e}%
}

% from https://tex.stackexchange.com/q/149731/138900
\usepackage{xcolor}
\makeatletter
\DeclareRobustCommand{\cgenfrac}[5]{%
\def\@tempa{#2#3}%
\edef\@tempb{%
\@nx\@cgenfrac{#1}%
\@mathstyle{#5}%
\csname
@@%
\ifx @#4@over\else above\fi
\ifx\@tempa\@empty\else withdelims\fi
\endcsname
}%
\@tempb{#2#3#4}%
}
\newcommand\@cgenfrac[6]{%
{#2{\colorlet{current}{.}\begingroup #5\endgroup\color{#1}#3#4\relax\color{current}#6}}%
}
\makeatother

\newcommand{\cbfrac}[3][blue]{\cgenfrac{#1}{}{}{}{}{#2}{#3}}
\newcommand{\cbdfrac}[3][blue]{\cgenfrac{#1}{}{}{}0{#2}{#3}}

\begin{document}
$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\frac{\EUR{18}}{\SI{1}{kg}}$
\bigskip

The same with color and cbfrac
\bigskip

$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\cbfrac{\EUR{18}}{\SI{1}{kg}}= \cbdfrac{\EUR{18}}{\SI{1}{kg}}= \dfrac{\EUR{18}}{\SI{1}{kg}}$

\bigskip

\textcolor{red}{$\cbfrac[green]{1}{2}$}

\end{document}


You need another pair of braces in the definition of \Frac to delimit the scope of \above. If you don't want to shrink the denominator and the numerator, you have to add \displaystyle to both (implemented here as \FRAC):

\documentclass{article}
\usepackage{siunitx}
\usepackage[right]{eurosym}
\DeclareRobustCommand{\officialeuro}{%
\ifmmode\expandafter\text\fi
{\fontencoding{U}\fontfamily{eurosym}\selectfont e}}
\usepackage{xcolor}
\def\Frac#1#2{{#1\color{blue}\above0.4pt\normalcolor#2}} % <<< Note the double braces
\def\FRAC#1#2{{\displaystyle#1\color{blue}\above0.4pt\normalcolor\displaystyle#2}}
\begin{document}
$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\Frac{\EUR{18}}{\SI{1}{kg}}$

$\SI{18}{\euro\highlight{blue}\per\kilo\gram}=\FRAC{\EUR{18}}{\SI{1}{kg}}$
\end{document}


Note the difference:

\documentclass{article}
\parindent0pt
\begin{document}
With \verb"\def\Frac#1#2{#1\above1pt #2}",\\
\verb"$A\Frac{B}{C}D$" typesets as
\def\Frac#1#2{#1\above1pt #2}$A\Frac{B}{C}D$.
\bigskip

With\verb"\def\Frac#1#2{{#1\above1pt #2}}",\\
\verb"$A\Frac{B}{C}D$" typesets as
\def\Frac#1#2{{#1\above1pt #2}}$A\Frac{B}{C}D$.
\end{document}

• Thank you for your interest in this issue. This macro now actually changes the color of the bar correctly, but the characters are written as with the macro \frac whereas I would like them to remain written in bulk as with the macro \dfrac. Is that possible? – AndréC Nov 14 at 21:40
• @AndréC I have updated the answer. Just add twice \displaystyle in the definition of \Frac. – gernot says Reinstate Monica Nov 14 at 22:01
• Thank you can you give the code for both versions in your answer, the one emulating \frac and the one emulating \dfrac so that it is exhaustive? – AndréC Nov 14 at 22:20