I want alter an existing math operator by applying a macro to it. In the MWE the \ReDeclareLargeMathOperator
applies the \ProcessSymbol
macro which in this example changes the color of the symbol as well make a hyperlink to a Wikipedia page.
However, my attempt to adapt the first reference listed below required a few a hack to get the smallest size to be correct for the \sum
operator. But, this macro does work properly for the integral as the style is altered.
So, what is the best way to define \ReDeclareLargeMathOperator
so that I can tweak the symbol yet have the same spacing as the default?
Notes:
- The black text below is the default operator and the red text is the one that is the one defined with the
\ReDeclareLargeMathOperator
macro.
Sum: The horizontal spacing does not align with the default symbol. Even in inline mode there seems to be some additional horizontal spacing added prior to the symbol.
Integral: The spacing of the limits of integration are incorrect and the style of the integral symbol does not match.
References:
- How to create my own math operator with limits?
- How to redefine a command using \DeclareMathOperator
Code:
\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}
\DeclareMathOperator
has a precise semantic, so the name you're choosing is bad. But it's not the real important thing. You surely don't want to apply\huge
andLarge
.\sum
and\bigcup
there's no problem; there is with\int
, because you won't get the kerning.