# Forcing \savebox to use correct mode

I found myself wanting to use a construct of the following type:

\newsavebox{\mybox}
\newcommand{\mycommand}[1]{\savebox{\mybox}{#1}\reflectbox{\usebox{\mybox}}}


The problem is that I want the content of the box to be typeset in the "correct" mode. In other words, if \mycommand is used in math mode, #1 should automatically be typeset in math mode. Furthermore, if \mycommand is used inside a subscript in math mode, then #1 should be typeset as a subscript, etc. Essentially, if it weren't for the presence of \reflectbox, \mycommand should be completely "transparent".

• why the savebox if you always use it ? rather than \newcommand{\mycommand}[1]{\reflectbox{#1}} ? (I ask as making a command that saves to a box, preserving the current tex/subscript style, probably is not possible. (without a lot of effort) May 16, 2015 at 21:45
• Well, I might want to do something more complicated with the box. Anyway, just using \reflectbox doesn't work since it falls back into text mode (and forgets about subscripts if forced back into math mode). May 16, 2015 at 21:48
• It's easy to force math mode in \reflectbox if \mycommand is used in math mode (and also respect the level). What “more complicated” things would you do with the box? May 16, 2015 at 21:56
• What I am really after is a more robust version of the \cev command given here, which doesn't work when used inside a subscript. You are right that in this case \savebox is probably overkill, but I thought it might be useful in other circumstances. May 16, 2015 at 22:09
• @MartinHairer I added a new version of the \cev command in the other question. May 16, 2015 at 23:13

The \ThisStyle{...\SavedStyle...} syntax of the scalerel package allows precisely this to be done.

In the MWE I show: text mode (row 1); textstyle and displaystyle math (row 2); scriptstyle and scriptscriptstyle (row 3); combined effect of normal math and subscripted \mycommand (row 4).

\documentclass{amsart}
\usepackage{scalerel}
\newsavebox{\mybox}
\newcommand{\mycommand}[1]{%
\ThisStyle{\ifmmode%
\savebox{\mybox}{$\SavedStyle#1$}%
\reflectbox{\usebox{\mybox}}%
\else%
\savebox{\mybox}{#1}%
\reflectbox{\usebox{\mybox}}%
\fi%
}}
\begin{document}
\mycommand{abc}

$\mycommand{\sum_{i=1}^N abc}\quad\displaystyle\mycommand{\sum_{i=1}^N abc}$

$\scriptstyle \mycommand{abc}\quad\scriptscriptstyle \mycommand{abc}$

$Z_{\mycommand{Z}}$
\end{document}