cryptocode icon indicating copy to clipboard operation
cryptocode copied to clipboard

Check for mathmode in \gamechange not working as intended

Open nilsfleischhacker opened this issue 1 year ago • 0 comments

\gamechange is currently defined as

\newcommand{\gamechange}[2][gamechangecolor]{%
{\setlength{\fboxsep}{0pt}\colorbox{#1}{\ifmmode$\displaystyle#2$\else#2\fi}}%
}

It clearly tries to check whether or not it's being used in mathmode. Unfortunately this doesn't actually work. The problem is that the check is performed inside of the colorbox. But at that point \ifmmode will always be false and the argument will always be typeset in text mode. The check needs to be performed outside of the box.

Additionally, if it worked as intended, it would always default to displaystyle, which is unfortunate if e.g. used in superscript of subscript. I suggest the following definition instead:

\newcommand{\gamechange}[2][gamechangecolor]{%
{\setlength{\fboxsep}{0pt}\ifmmode%
\mathchoice{%
\colorbox{#1}{$\displaystyle#2$}%
}{%
\colorbox{#1}{$#2$}%
}{%
\colorbox{#1}{$\scriptstyle#2$}%
}{%
\colorbox{#1}{$\scriptscriptstyle#2$}%
}%
\else{#2}\fi}%
}

This way, the check for mathmode works correctly and the mathstyle is preserved. It is however a breaking change. I.e., any documents relying on it defaulting to text mode will break after the change.

nilsfleischhacker avatar May 17 '23 11:05 nilsfleischhacker