Color fraction bar

Thanks to @Herbert, I was able to tap into the amsmath package documentation and come up with a modified version of the \frac command definition. The documentation has the following:

Bury the generalized fraction primitives \over, \atop, etc., because of their bizarre syntax, which is decidedly out of place in a LaTeX document.

\@saveprimitive\over\@@over

If someone insists on using \over, give a warning the first time and then resurrect the old definition. Laissez-faire policy.

\DeclareRobustCommand{\primfrac}[1]{% \PackageWarning{amsmath}{%
Foreign command \@backslashchar#1;\MessageBreak \protect\frac\space or
\protect\genfrac\space should be used instead% \MessageBreak }
\global\@xp\let\csname#1\@xp\endcsname\csname @@#1\endcsname
\csname#1\endcsname }

Hence the warning that LaTeX shouts out in @Herberts answer. The documentation continues by saying that

\frac calls \@@over directly instead of via \genfrac

and defines \frac as

\DeclareRobustCommand{\frac}[2]{{\begingroup#1\endgroup\@@over#2}}

This where I modified the definition of \frac slightly to come up with the command \cbfrac which stands for colored bar fraction just for my own reference. See the definition below:

\DeclareRobustCommand{\cbfrac}[3][OrangeRed]{{\begingroup#2\endgroup\color{#1}\@@over\normalcolor#3}}

Here is MWE illustrating how it works:

enter image description here

\documentclass[dvipsnames,letterpaper]{article}
\usepackage{amsmath,amssymb}
\usepackage{xcolor}
\def\Frac#1#2{ #1 \color{red}\above 0.4pt \normalcolor #2}
\makeatletter
\DeclareRobustCommand{\cbfrac}[3][OrangeRed]{{\begingroup#2\endgroup\color{#1}\@@over\normalcolor #3}}
\makeatother
\begin{document}
\noindent
$ \Frac{n+1}{n-1} $
$\displaystyle \Frac{n+1}{n-1} $\\[1ex]
$ \cbfrac{n+1}{n-1} $
$\displaystyle \cbfrac{n+1}{n-1} $
\end{document}

Commenting the command \Frac removes the warning.


\genfrac refers to \over or \above which are primitives. But you can define an own \frac which uses \above:

\documentclass[letterpaper]{article}
\usepackage{amsmath,amssymb}
\usepackage{xcolor}

\newcommand\Frac[3][]{ #1 #2\color{red}\above 0.4pt \normalcolor #1#3}

\begin{document}

$ \Frac{n+1}{n-1} $$\Frac[\displaystyle]{n+1}{n-1} $

\end{document}