如何定义 ReDeclareLargeMathOperator

如何定义 ReDeclareLargeMathOperator

我想通过应用宏来更改现有的数学运算符。在 MWE 中,\ReDeclareLargeMathOperator应用\ProcessSymbol宏,在本例中,宏会更改符号的颜色,并创建指向 Wikipedia 页面的超链接。

然而,我尝试改编下面列出的第一个参考文献时,需要进行一些修改才能获得适合运算符的最小尺寸\sum。但是,由于样式已改变,此宏确实可以正常工作。

那么,最好的定义方法是什么\ReDeclareLargeMathOperator,以便我可以调整符号但又具有与默认相同的间距?

笔记:

  • 下面的黑色文本是默认运算符,红色文本是用宏定义的运算符\ReDeclareLargeMathOperator

和:水平间距与默认符号不对齐。即使在内联模式下,符号前面似乎也添加了一些额外的水平间距。

在此处输入图片描述


不可缺少的:积分界限间距不正确,积分符号样式不匹配。

在此处输入图片描述

参考:

代码:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage[colorlinks=false, pdfborder={0 0 1}, allbordercolors=magenta]{hyperref}

\newcommand*{\ProcessSymbol}[2]{%
    \color{red}\href{#2}{#1}%
}%


%% Adapted from https://tex.stackexchange.com/questions/23432/how-to-create-my-own-math-operator-with-limits
\newcommand*{\ReDeclareLargeMathOperator}[3]{%
    % #1 = name of operator
    % #2 = symbol 
    % #3 = web link
    % ---------------------
    \renewcommand#1{%
        \vphantom{\OldSum}%
        \mathop{\mathchoice%
            {\vcenter{\hbox{\ProcessSymbol{\huge$#2$}{#3}}}}%
            {\vcenter{\hbox{\ProcessSymbol{\Large$#2$}{#3}}}}%
            {\vcenter{\hbox{\ProcessSymbol{$#2$}{#3}}}}%
            {\vcenter{\hbox{\ProcessSymbol{$\scriptstyle#2$}{#3}}}}%
        }\displaylimits%
    }%
}%

%% So that we can test things and also ensure that limit placement matches
%%  the height of where the original definition of \sum placed things.
\let\OldSum\sum
\let\OldInt\int

\ReDeclareLargeMathOperator{\sum}{\Sigma}{https://en.wikipedia.org/wiki/Summation}
\ReDeclareLargeMathOperator{\int}{\intop}{https://en.wikipedia.org/wiki/Integral}

\newcommand{\dx}{\mathrm{d}x}%

\begin{document}  
\section{Sum}
In inline math $\OldSum_{i=0}^n i$, and in display math it is:
\begin{flalign*}
    &\displaystyle\OldSum_{i=0}^n i
    \textstyle\OldSum_{i=0}^n i
    \scriptstyle\OldSum_{i=0}^n i
    \scriptscriptstyle\OldSum_{i=0}^n i
    \quad%% so that we can view vertical spacing.
    \displaystyle\sum_{i=0}^n i
    \textstyle\sum_{i=0}^n i
    \scriptstyle\sum_{i=0}^n i
    \scriptscriptstyle\sum_{i=0}^n i &
\end{flalign*}
\noindent
In inline math $\sum_{i=0}^n i$, and in display math it is:
\begin{flalign*}
    &\displaystyle\sum_{i=0}^n i
    \textstyle\sum_{i=0}^n i
    \scriptstyle\sum_{i=0}^n i
    \scriptscriptstyle\sum_{i=0}^n i
    &
\end{flalign*}


% ----------------------------------------------------------------

\section{Integral}
In inline math $\OldInt_a^b y\dx$, and in display math it is:
\begin{flalign*}
    &\displaystyle\OldInt_a^b  y\dx
    \textstyle\OldInt_a^b  y\dx
    \scriptstyle\OldInt_a^b  y\dx
    \scriptscriptstyle\OldInt_a^b  y\dx
    \quad%% so that we can view vertical spacing.
    \displaystyle\int_a^b  y\dx
    \textstyle\int_a^b  y\dx
    \scriptstyle\int_a^b  y\dx
    \scriptscriptstyle\int_a^b  y\dx &
    &
\end{flalign*}
\noindent
In inline math $\int_a^b  y\dx$, and in display math it is:
\begin{flalign*}
    &\displaystyle\int_a^b  y\dx
    \textstyle\int_a^b  y\dx
    \scriptstyle\int_a^b  y\dx
    \scriptscriptstyle\int_a^b  y\dx
    &
\end{flalign*}

\end{document}

答案1

\sum为什么将的符号改为\Sigma?这肯定是错误的。

诸如\sum或 之类的运算符\bigcup可以用更简单的方式处理;对于积分,如果要保留字距,则需要做更多工作。因此,新\int命令必须吸收可能的\limits标记,然后吸收(可选)极限。然后,红色积分被排版为 的一部分\href,并以当前颜色添加极限。

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage[colorlinks=false, pdfborder={0 0 1}, allbordercolors=magenta]{hyperref}

%% Adapted from http://tex.stackexchange.com/questions/23432/how-to-create-my-own-math-operator-with-limits
\newcommand*{\ReDeclareLargeMathOperator}[2]{%
  % #1 = name of operator
  % #2 = web link
  % ---------------------
  \cslet{\string#1}#1%
  \renewcommand#1{%
    \mathop{%
      \mathpalette{\ProcessSymbol}{{\csuse{\string#1}}{#2}}%
    }\displaylimits
  }%
}
\newcommand*{\ProcessSymbol}[2]{\doProcessSymbol{#1}#2}
\newcommand*{\doProcessSymbol}[3]{%
  \vcenter{\hbox{\color{red}\href{#3}{$#1#2$}}}%
}
%% for integrals the above can't work
\makeatletter
\let\linkedint@int\intop
\DeclareRobustCommand{\linkedint}{%
  \let\linkedint@limits\nolimits % default
  \let\linkedint@lower\@empty
  \let\linkedint@upper\@empty
  \colorlet{linkedint@color}{.}%
  \@ifnextchar\limits{\let\linkedint@limits\limits\linkedint@checksub}{\linkedint@checksub}%
}
\newcommand{\linkedint@checksub}{%
  \@ifnextchar_{\linkedint@sub}{\linkedint@checksup}%
}
\newcommand{\linkedint@checksup}{%
  \@ifnextchar^{\linkedint@sup}{\linkedint@do}%
}
\newcommand\linkedint@sub[2]{%
  \def\linkedint@lower{#2}\linkedint@checksup
}
\newcommand\linkedint@sup[2]{%
  \def\linkedint@upper{#2}\linkedint@do
}
\newcommand{\linkedint@do}{%
  \mathop{\mathpalette\linkedint@final{https://en.wikipedia.org/wiki/Integral}}%
}
\newcommand\linkedint@final[2]{%
  \vcenter{\hbox{\color{red}%
    \href{#2}{$#1%
      \linkedint@int\linkedint@limits
        _{\textcolor{linkedint@color}{\linkedint@lower}}%
        ^{\textcolor{linkedint@color}{\linkedint@upper}}%
    $}%
  }}%
}
\makeatother

%% So that we can test things and also ensure that limit placement matches
%%  the height of where the original definition of \sum placed things.
\let\OldSum\sum
\let\OldIntop\intop
\def\OldInt{\OldIntop\nolimits}

\ReDeclareLargeMathOperator{\sum}{https://en.wikipedia.org/wiki/Summation}
%\ReDeclareLargeMathOperator{\intop}{https://en.wikipedia.org/wiki/Integral}
\let\int\linkedint

\newcommand{\dx}{\mathrm{d}x}%

\begin{document}  

\section{Sum}
In inline math $\OldSum_{i=0}^n i$, and in display math it is:
\begin{flalign*}
    &\displaystyle\OldSum_{i=0}^n i
    \textstyle\OldSum_{i=0}^n i
    \scriptstyle\OldSum_{i=0}^n i
    \scriptscriptstyle\OldSum_{i=0}^n i
    \quad%% so that we can view vertical spacing.
    \displaystyle\sum_{i=0}^n i
    \textstyle\sum_{i=0}^n i
    \scriptstyle\sum_{i=0}^n i
    \scriptscriptstyle\sum_{i=0}^n i &
\end{flalign*}
\noindent
In inline math $\sum_{i=0}^n i$, and in display math it is:
\begin{flalign*}
    &\displaystyle\sum_{i=0}^n i
    \textstyle\sum_{i=0}^n i
    \scriptstyle\sum_{i=0}^n i
    \scriptscriptstyle\sum_{i=0}^n i
    &
\end{flalign*}


% ----------------------------------------------------------------

\section{Integral}
In inline math $\OldInt_a^b y\dx$, and in display math it is:
\begin{flalign*}
    &\displaystyle\OldInt_a^b  y\dx
    \textstyle\OldInt_a^b  y\dx
    \scriptstyle\OldInt_a^b  y\dx
    \scriptscriptstyle\OldInt_a^b  y\dx
    \quad%% so that we can view vertical spacing.
    \displaystyle\int_a^b  y\dx
    \textstyle\int_a^b  y\dx
    \scriptstyle\int_a^b  y\dx
    \scriptscriptstyle\int_a^b  y\dx &
    &
\end{flalign*}
\noindent
In inline math $\int_a^b  y\dx$, and in display math it is:
\begin{flalign*}
    &\displaystyle\int^b_a  y\dx
    \textstyle\int_a^b  y\dx
    \scriptstyle\int_a^b  y\dx
    \scriptscriptstyle\int_a^b  y\dx
    &
\end{flalign*}

\end{document}

在此处输入图片描述

相关内容