Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to redefine the underscore while within a particular command only? This is somewhat related to: Redefine underscore to produce roman subscript

I would like:

\mycmd       -->    S
\mycmd_0     -->    S_{modifier 0}
\mycmd_t     -->    S_{modifier t}
\othercmd_0  -->    X_0
Y_t          -->    Y_t

So I want the modification of the underscore to be restricted to \mycmd.

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

Yes, it is possible. You just have to define \mycmd the right way.

If modifier is a macro that takes an argument (like \mathrm), then you can simply do the following:

\makeatletter
\def\mycmd{%
    S% <- put the actual \mycmd here.
    \@ifnextchar_{_\expandafter\mathrm\@gobble}{}%
}
\makeatother

\@ifnextchar#1#2#3 is a macro from the LaTeX kernel, that simply checks whether the following character is equal to #1. If so, it executes #2, otherwise #3. In our case, we check if \mycmd is followed by an underscore (note that any whitespace is removed). If so, then _\expandafter\mathrm\@gobble is executed. Here \mathrm is the actual modifier. We want the underscore before the modifier, so we insert it at the beginning and remove the underscore the user typed with \@gobble (\@gobble simply removes its argument). The \expandafter makes sure that the underscore is removed before \mathrm looks for its argument (otherwise \mathrm would try to take \@gobble as its argument, leading to an error).

If \modifier is something that acts like \itshape or \color, one has to do something slightly more involved in order to get correct grouping:

\makeatletter
\def\myothercmd{%
    T%
    \@ifnextchar_{\myothercmd@}{}%
}
\def\myothercmd@_#1{%
    _{{\color{red}#1}}%
}
\makeatother

Here the underscore is removed while \myothercmd@ searches for its argument, which is specified as _#1. Then the group after the underscore (i.e. #1) is reinserted together with an underscore and the the modifier in front of it. Of course this solution would also work with a modifier of the first case, e.g. defining \myothercmd@ as _{\mathrm{#1}}.


If you also want to allow superscripts, you can use

\makeatletter
\def\mycmd{%
    T%
    \@ifnextchar_{\mycmd@d}{\@ifnextchar^{\mycmd@u}{}}%
}
\def\mycmd@d_#1{%
    _{{\color{red} #1}}%
    \@ifnextchar^{\mycmd@u}{}%
}
\def\mycmd@u^#1{%
    ^{\mathrm{#1}}%
    \@ifnextchar_{\mycmd@d}{}%
}
\makeatother
share|improve this answer
    
Very nice, but isn't this fragile to the order at which you specify the underscore. It would not work if I did: $\mycmd^3_2$. –  Tom Jul 27 '11 at 5:46
    
@Tom: You can test if next character is ^ and make similar definitions. –  Leo Liu Jul 27 '11 at 5:53
    
@Tom: See updated answer. Also I noticed that super- and subscripts are typeset in the same group. So the switch-like modifiers (e.g. \color) need an extra group. –  Caramdir Jul 27 '11 at 16:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.