4

This question is kind of follow up question of the following

There are some suggestions, but I wasn’t able to get the following MWE to work with the \ThisStyle{...\SavedStyle...} construct. I want to save some content math or text in an hbox while keeping the correct math style. At the moment everything is in double script style if it is put in my box.

% !TeX program = xelatex
\documentclass[11pt]{scrartcl}

\usepackage{xparse,scalerel,parskip}

\ExplSyntaxOn\makeatletter

% - #1: Box-Register
% - #2: Content
\cs_new:Npn \tobiw_set_text_or_math_hbox:nn #1#2 {
   \mode_if_math:TF {
      \ThisStyle {
      \hbox_gset:Nn #1 {
         \(
            \SavedStyle
            #2
         \)
      }
      }
   } {
      \hbox_gset:Nn #1 {
         #2
      }
   }
}
\cs_generate_variant:Nn \tobiw_set_text_or_math_hbox:nn { Nn, NN }


\NewDocumentCommand { \boxtest } { m } {
   \tobiw_set_text_or_math_hbox:Nn \l_tmpa_box { #1 }
   \box_use:N \l_tmpa_box
}

\makeatother\ExplSyntaxOff

\begin{document}

\boxtest{Box}

$a \boxtest{a}$

$a^2 a^{\boxtest{2}}$

$\frac{2}{3} \boxtest{\frac{2}{3}}$

\[\frac{2}{3} \boxtest{\frac{2}{3}}\]

\end{document}

As I need to use mathspec I’m bound to XeTeX and can't use LuaTeXs \mathstyle primitive.

2

I just put \ThisStyle as the first thing of \boxtest, while leaving \SavedStyle where it had been.

There is no need for the \SavedStyle and \ThisStyle to be part of the same macro, as long as \SavedStyle is effectively called within the argument to \ThisStyle.

It took me a bit to see what the actual problem was with Tobi's code. To reiterate from my comment below, the OP is generating the \hbox within the confines of the \ThisStyle, but is trying to employ it outside the argument of \ThisStyle. The macro \ThisStyle is a glorified version of \mathchoice, which will generate boxes in all mathstyles and choose which one to use at the last moment. By using the generated \hbox outside the \ThisStyle argument (i.e., \box_use:N \l_tmpa_box), the OP is using it outside the \mathchoice and thus just using the last of the 4 \hboxes generated, which will always be \scriptscriptstyle.

% !TeX program = xelatex
\documentclass[11pt]{scrartcl}

\usepackage{xparse,scalerel,parskip}

\ExplSyntaxOn

% - #1: Box-Register
% - #2: Content
\cs_new:Npn \tobiw_set_text_or_math_hbox:nn #1#2 {
   \mode_if_math:TF {
%      \ThisStyle {
      \hbox_gset:Nn #1 {
         \(
            \SavedStyle
            #2
         \)
      }
%      }
   } {
      \hbox_gset:Nn #1 {
         #2
      }
   }
}
\cs_generate_variant:Nn \tobiw_set_text_or_math_hbox:nn { Nn, NN }


\NewDocumentCommand { \boxtest } { m } {\ThisStyle{%
   \tobiw_set_text_or_math_hbox:Nn \l_tmpa_box { #1 }
   \box_use:N \l_tmpa_box
}}

\ExplSyntaxOff

\begin{document}

\boxtest{Box}

$a \boxtest{a}$

$a^2 a^{\boxtest{2}}$

$\frac{2}{3} \boxtest{\frac{2}{3}}$

\[\frac{2}{3} \boxtest{\frac{2}{3}}\]

\end{document}

enter image description here

  • Hi, although this works I don’t think this is a good solution as one always must remember to use \tobiw_set_text_or_math_hbox inside a \ThisStyle. Do you know why it doesn’t work when \ThisStyle is part of the definition of the boxing macro? – Tobi May 14 '17 at 14:00
  • @Tobi I do not understand LaTeX3 to know where the problem arises when all the calls are self contained in the \tobiw_set_text_or_math_hbox macro. I suppose you could put a LaTeX2e wrapper around the macro, i.e., define a macro which only invokes \ThisStyle and calls on \tobiw_set_text_or_math_hbox... – Steven B. Segletes May 14 '17 at 22:21
  • @Tobi A little play testing reveals inside \tobiw_set_text_or_math_hbox, that the scalerel mode switch \m@switch, which carries the math style in the form of D, T, S, or s, is properly set just prior to the \hbox_gset, but becomes s inside of the \hbox_gset, thus putting everything into \scriptscriptstyle, regardless of value just prior to the \hbox_gset. If I knew anything about LaTeX3, I might suspect a bug, for changing macro values, but as I said, I don't understand the LaTeX3 syntax at all. – Steven B. Segletes May 14 '17 at 22:40
  • Thanks for investigation further. This seems to be a strange behaviour. Maybe someone of the L3 team can tell us more … @Joseph Wright I’d be glad if you could take a look, please :-) – Tobi May 15 '17 at 6:31
  • @Tobi I realize what the problem is. You are generating the \hbox within the confines of the \ThisStyle, but you are trying to employ it outside the argument of \ThisStyle. The macro \ThisStyle is a glorified version of \mathchoice, which will generate boxes in all mathstyles and choose which one to use at the last moment. By using it outside the \ThisStyle argument (\box_use:N \l_tmpa_box), you are using it outside the \mathchoice and thus just using the last of the 4 \hboxes generated, which will always be \scriptscriptstyle. – Steven B. Segletes May 15 '17 at 10:49
1

Since the \ThisStyle-\SavedStyle-solution requires to much thinking on the authors/users side (i.e. me…), I decided to use LuaTeX, which has a simple primitive \mathstyle holding an integer for the current math style. Then it is quite easy to save the current math style:

% !TeX program = lualatex
\documentclass[11pt,parskip]{scrartcl}

\usepackage{xparse}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{tikz}

\ExplSyntaxOn\makeatletter

\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 switches (always outside math)
% - #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
      }
   }
}
\cs_generate_variant:Nn \tobi_set_text_or_math_hbox:nnn { Nnn, NNN, NnN, NNn }


\tikzset {
   highlight~node/.style = {
      fill = yellow, inner~sep = \z@,
   },
}

% - #1: content (text or math)
\NewDocumentCommand{ \highlight }{ m }{
   \tobi_set_text_or_math_hbox:Nnn \l_tmpa_box { \bfseries \boldmath } { #1 }
   \begin{tikzpicture} [ baseline = (N.base) ]
      \node [ highlight~node ] (N) {
         \box_use:N \l_tmpa_box
      };
   \end{tikzpicture}
}


\ExplSyntaxOff\makeatother

\begin{document}

\highlight{High}light

$super^{\highlight{super}^{\highlight{script}}}$

$\highlight{\frac{text}{style}}$

\[\highlight{\frac{display}{style}}\]

\end{document}

See Value of LuaTeX’s \mathstyle in \frac (de)nominator for a follow-up question.

  • IMHO \tobi_saved_math_style: should be a variable – you're using it like one – clemens May 17 '17 at 17:50
  • @clemens: good point, so you would use \l_tobi_saved_math_style_tl? – Tobi May 17 '17 at 18:06
  • probably yes, and then just set it \tl_set:Nn \l_tobi_saved_math_style_tl { \displaystyle } – clemens May 17 '17 at 18:19
  • @clemens: Seems reasonable :-) thanks – Tobi May 18 '17 at 10:51

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.