Is there an easy way to capture the math class (\mathord, \mathrel, …) before boxing a term and then reapply it to the box when using it?
In the following example I’d like to have the same spacing in both equations.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{ \highlight }{ m }{
\hbox_set:Nn \l_tmpa_box { #1 }
\box_use:N \l_tmpa_box
}
\ExplSyntaxOff
\begin{document}
\[ a + b \]
\[ a \highlight{+} b \]
\end{document}
I know that bm.sty knows a trick (see also David’s answer on Non-invasive replacement for \fbox?) to do it but I didn’t get how it works and could figure out how to transfer it to my code.
Note: I know that my example is kind of wrong as a box doesn’t allow math content and it only works because + can also be in text mode (\int e.g. would fail here). In my real code if have a test for the correct mode and also math style while saving the box, but I omitted it here to keep the example as short as possible. I’m just looking for a solution to capture the class and apply it like \mathCLASS { \box_use:N \l_tmpa_box }.
Background: I need this for the following: The \highlight should color it’s content but as I use a light font, for the highlight three should also be a bolder font choosen. To make it non-invasive I used something like
\NewDocumentCommand{ \highlight }{ m }{
\begingroup
\color { red }
\bfseries
#1
\endgroup
}
but this won't work for math as \bfseries is forbidden in math mode. There is \boldmath but it can’t be applied for only a part of an equation so I cam up with code like this
\NewDocumentCommand{ \highlight }{ m }{
\tobi_set_text_or_math_hbox:Nnn \l_tmpa_box { \color { red } \bfseries \boldmath } { #1 }
\box_use:N \l_tmpa_box
}
Where \tobi_set_text_or_math_hbox cares for setting the box content in the right font and mode and also applies the right math style if necessary. But then the spacing in math mode doesn’t work anymore so I’m searching for a way to add the correct \mathord, \mathrel etc.
These are the definitions of my math style saving (requires LuaTeX) and boxing macros:
\cs_new_eq:NN \tobi_saved_math_style: \textstyle
\cs_new:Npn \tobi_save_math_style: {
\int_case:nn { \mathstyle } {
{ \displaystyle } { \cs_set_eq:NN \tobi_saved_math_style: \displaystyle }
{ \textstyle } { \cs_set_eq:NN \tobi_saved_math_style: \textstyle }
{ \scriptstyle } { \cs_set_eq:NN \tobi_saved_math_style: \scriptstyle }
{ \scriptscriptstyle } { \cs_set_eq:NN \tobi_saved_math_style: \scriptscriptstyle }
}
}
% #1 = box register
% #2 = font macros (always outside math mode)
% #3 = box content
\cs_new:Npn \tobi_set_text_or_math_hbox:nnn #1#2#3 {
\mode_if_math:TF {
\tobi_save_math_style:
\hbox_gset:Nn #1 {
#2
\(
\m@th
\tobi_saved_math_style:
#3
\)
}
} {
\hbox_set:Nn #1 {
#2 #3
}
}
}


+is also allowed in text mode but it comes from the text font not the math font and has text (no) spacing. Perhaps you just want{ $#1$ }in the definition but hard to be sure of the exact spec that you expected. – David Carlisle May 15 '17 at 21:16\mathXX{ \box_use:N \l_tmpa_box }– Tobi May 15 '17 at 21:21\highlight {a+b}anda+bdoesn't have a class. If your syntax just took a single symbol it would perhaps have a clearer answer. also where do you want to store the class (you can't store it in the box (not really). so you would have to store it in some macro and then access it by some wrapper for\useboxthat retrieved the box and the saved class in a macro associated with each saved box – David Carlisle May 15 '17 at 21:26\textcolor{red}{\bm{#1}}why box at all? – David Carlisle May 15 '17 at 21:42\textcoloralso seems to interrupt the math spacing mechanism and the content becomesOrd. – Tobi May 15 '17 at 22:01${}#1{}$is a way of saying "and I assume that something surrounds#1on both sides, in math mode." – Steven B. Segletes May 16 '17 at 19:49bmpackages is somewhat able to preserve the class for bold symbols and I wonder how it works and how I can use it in my macro … – Tobi May 16 '17 at 21:35