如何在数学模式下为分数线着色?

如何在数学模式下为分数线着色?

当我使用这里给出的解决方案,这就是我得到的:

截屏

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

答案1

我更喜欢这个\cbfrac版本。这里我根据这个版本做了一些修改\genfrac

请注意,分母中的颜色与当前颜色相同,但 并非如此\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}

在此处输入图片描述

答案2

您需要在 的定义中使用另一对括号\Frac来界定 的范围\above。如果您不想缩小分母和分子,则必须同时添加\displaystyle两者(此处实现为\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}

在此处输入图片描述

注意区别:

在此处输入图片描述

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

相关内容