2

When I write math in xelatex, I like notation such as:

$x \overset{ℝ}{+} y$  
$x \overset{f}{↦} y $  
$x \overset{(1)}{=} y$

However, as I type things like this so often, I would like to type it faster, and to keep the source code better readable (in the sense of quick scanning the code). I thought about three things, from which I don't how to do it (or if it is possible):

  1. Defining #1^#2 as \overset{#2}{#1} when #1∈{+,↦,=,→} and maybe more
  2. Defining #1^^#2 as \overset{#2}{#1} for all #1
  3. Defining #1↖#2 as \overset{#2}{#1} for all #1

I prefer option 1, but I'm not sure if this is possible. I think it should be possible as \sum, \int and \prod already act in this way.

  • You can do #1 with the same process as \sum, \int and so on but it would be tricky to sort out the cases where those characters (especially +) is not used as a relation or binary symbol. Options 2 and 3 are nearly impossible because TeX would have to look back which it usually doesn’t. – Qrrbrbirlbel Nov 6 '13 at 23:21
  • @Qrrbrbirlbel But it does also look back for ^ and _ right ? I get the result that I wanted with $\mathop{#1}^{#2}$, could I do something as define #1 as \mathop{#1} for #1∈{+,↦,=,→} ? – Kasper Nov 6 '13 at 23:45
  • @Kasper No, it doesn't look back in those cases. But \mathop{+}^{\mathbb{R}} would give wrong spacing around the symbol. – egreg Nov 6 '13 at 23:57
5

I'm not sure if you really want this; however, here it is.

\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math}

\ExplSyntaxOn
\NewDocumentCommand{\defineoversetchar}{m}
 {
  \clist_map_inline:nn { #1 } { \kasper_define_overset_char:n { ##1 } }
 }
\cs_new_protected:Nn \kasper_define_overset_char:n
 {
  \cs_new_protected:cn { kasper_#1_active: }
   {
    \peek_catcode_remove_ignore_spaces:NTF \c_math_superscript_token
     {
      \kasper_overset:nn { #1 }
     }
     {
      \use:c { kasper_#1_inactive: }
     }
   }
  \char_set_active_eq:Nc #1 { kasper_#1_active: }
  \AtBeginDocument
   {
    \cs_new:cx { kasper_#1_inactive: } { \Umathcharnum \the\Umathcodenum`#1 }
    \char_set_mathcode:nn { `#1 } { "8000 }
   }
 }
\cs_new_protected:Nn \kasper_overset:nn
 {
  \overset{#2}{\use:c { kasper_#1_inactive: }}
 }
\ExplSyntaxOff

\defineoversetchar{+,↦,=,→}

\begin{document}

$x ℝ^{+} y$

$x ↦^f y ↦ z$

$x →^f y → z$

$x =^{(1)} y = z$

\end{document}

enter image description here

Note that a special character used in a superscript (or subscript) should be in braces.

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.