2

This is an extension of Highlight terms in equation mode,

The problem is, with

\newcommand{\highlight}[1]{%
\colorbox{red!50}{$\displaystyle#1$}}

In, $x^{\highlight{\alpha}}$, \alpha is enlarged abnormally, unlike a proper superscript.

Is there a way to highlight specific variables within math-mode whilst maintaining subscripts and superscripts?

5
  • Does \scriptsize\alpha work for you? – user193767 May 8 '20 at 12:06
  • $a^{\highlight{\scriptsize\alpha}} does not work – RevoGen May 8 '20 at 12:11
  • 1
    $a^{\scriptsize\highlight{\alpha}}$, maybe? It works here. – user193767 May 8 '20 at 12:12
  • Yes it works! Thank you very much. – RevoGen May 8 '20 at 12:14
  • No problem! Stay safe ❤️ – user193767 May 8 '20 at 12:15
4

Your \highlight command doesn't work in superscripts because (1) the equation in the \colorbox doesn't know it's supposed to be typeset in \scriptstyle and (2) because of the \displaystyle in its definition explicitly telling TeX to typeset #1 in display style (using a full size font and things like large summation/integral symbols).

This is pretty much exactly what the \mathpalette command is designed to solve. A detailed description of how \mathpalette works can be found here.

Here is a simple re-implementation that uses `\mathpalette. (An improved version may be found below)

\documentclass{article}

\usepackage{xcolor}

\newcommand{\highlight}[1]{\mathpalette\highlightwithstyle{#1}}
\newcommand{\highlightwithstyle}[2]{\colorbox{red!50}{$#1#2$}}

\begin{document}

\[
    a^{\highlight{\alpha}} + \highlight{\sum}
\]

\end{document}

output

What \highlight{\alpha} now does is expand to \highlightwithstyle{<current math style>}{\alpha}, where <current math style> is either \displaystyle, \textstyle, \scriptstyle or \scriptscriptstyle (whichever is appropriate). This in turn turns into

\colorbox{red!50}{$\scriptstyle\alpha$}

when used in a super-/subscript, and into

\colorbox{red!50}{$\displaystyle\alpha$}

when used in the main display equation etc. (The other two cases are \textstyle for inline equations and \scriptscriptstyle for nested super-/subscripts.)


Addendum (improved version)

Here is another version of the same command that creates a slightly tighter box and doesn't in most (but not all) circumstances affect the spacing of your equation. It also lets you specify a colour.

\documentclass{article}

\usepackage{xcolor}

\newcommand{\highlight}[2][red!50]{\mathpalette{\highlightwithstyle[#1]}{#2}}
\newcommand{\highlightwithstyle}[3][red!50]{
  \begingroup                         %% <- limit scope of \box0 and \fboxsep assignment
    \sbox0{$\mathsurround 0pt #2#3$}% %% <- typeset content in box 0
    \setlength{\fboxsep}{.5pt}        %% <- set (smaller) framebox margins
    \sbox2{\hspace{-.5pt}%            %% <- create box 2, undo margin
      \colorbox{#1}{\usebox0}%        %% <- print the contents of box 0 in a \colorbox
    }%
    \dp2=\dp0 \ht2=\ht0 \wd2=\wd0     %% <- set dimensions of box 2 to match box 0
    \box2                             %% <- print box 2
  \endgroup                           %% <- revert old definitions of the boxes and \fboxsep
}

\begin{document}

\[
    a^{\highlight{\alpha}} + \highlight{\sum_{k=1}^\infty \frac{\fbox{1}}{\color{blue}k!}} + \frac{\highlight[green]{x}}{2}
\]
\[
    a^{\alpha} + \sum_{k=1}^\infty \frac{\fbox{1}}{\color{blue}k!} + \frac{x}{2}
\]

\end{document}

output

I've tried to explain what each of these lines do in the comments, except for \mathsurround. Setting \mathsurround to 0pt does nothing here, but it would matter if your document uses non-standard spacing around inline equations (which is unlikely).

1
  • 1
    I've added an improved version of \highlight that allows you to choose a colour and shouldn't affect spacing in most circumstances. – Circumscribe May 10 '20 at 10:44

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.