Yes, it is possible. You just have to define
\mycmd the right way.
modifier is a macro that takes an argument (like
\mathrm), then you can simply do the following:
S% <- put the actual \mycmd here.
\@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
#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 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).
\modifier is something that acts like
\color, one has to do something slightly more involved in order to get correct grouping:
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
If you also want to allow superscripts, you can use