1

I want to keep the same colour and font size being used prior to entering a \tcboxmath. The objective is to get the right side of the output image while having the x^2+1 highlighted.

Output

\documentclass{article}
\usepackage[theorems]{tcolorbox}

\newcommand{\mymath}[2][]{%
  \tcboxmath[colback=blue!20,size=minimal]{#2}%
}
\begin{document}
\[\color{red}Z^{\mymath{x^2+1}} \qquad Z^{x^2+1}\]
\end{document}

1 Answer 1

3

The current color is stored in . (in a color context). The size in math mode is chosen by \mathchoice (see The mysteries of \mathpalette

\documentclass[varwidth]{standalone}
\usepackage[theorems]{tcolorbox}

\newcommand{\mymath}[2][]{%
  \mathchoice%
  {\tcboxmath[colback=blue!20,colupper=.,size=minimal]{\displaystyle#2}}%
  {\tcboxmath[colback=blue!20,colupper=.,size=minimal]{\textstyle#2}}%
  {\tcboxmath[colback=blue!20,colupper=.,size=minimal]{\scriptstyle#2}}%
  {\tcboxmath[colback=blue!20,colupper=.,size=minimal]{\scriptscriptstyle#2}}%
}
\begin{document}
\[\color{red}Z^{\mymath{x^2+1}} \qquad Z^{x^2+1}\]
\end{document}

1
  • 1
    Thank you for the answer and appreciate introducing me to \mathpalette. May 9, 2023 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .