8

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.

enter image description here


Integral: The spacing of the limits of integration are incorrect and the style of the integral symbol does not match.

enter image description here

References:

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}
  • 1
    \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 and Large. – egreg Dec 14 '15 at 14:36
  • With operators such as \sum and \bigcup there's no problem; there is with \int, because you won't get the kerning. – egreg Dec 14 '15 at 14:49
6

Why changing the symbol for \sum into \Sigma? It's surely wrong.

Operators such as \sum or \bigcup can be dealt with in a simpler way; for the integral some more work is needed, if you want to preserve the kerning. Thus the new \int command must absorb the possible \limits token and then the (optional) limits. Then a red integral is typeset as part of \href, with the limits added in the current color.

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

enter image description here

  • There is a slight problem with using `int^b_a` (as in the last red display mode integral in the image above). But, not an issue for me as I always use `int_a^b`, – Peter Grill Dec 14 '15 at 21:19
  • So, what would be a better macro name to use here ? I don't know how the large operators are defined. Or was your comment related to the fact that I have changed the number of operators (which I agree is sufficient to warrant a different name). Also, is \int the only large operator that is the exception or are the other obvious ones that will need to be tweaked? – Peter Grill Dec 14 '15 at 21:21
  • Does using \cslet{\string#1}#1 and {\csuse{\string#1}} do some magic? Seems to me that later could be replaced with just {#1}? Oh, I am guessing it has to do with \doProcessSymbol[3] which only provided with what appears to be just 2 parameters at invocation. I have seen this sort of magic before, but do not grok it. – Peter Grill Dec 14 '15 at 21:29
  • @PeterGrill They define the macro \\sum (the second backslash is part of the name), the same trick as for commands with optional argument. – egreg Dec 14 '15 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.