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.

I would like to redefine the underscore _ to automatically set subscripts in math mode in roman type, as I currently find myself almost always using _\mathrm{}. I can't find a way of redefining what _ does:
\renewcommand_ gives

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 

and \show_ just tells me

subscript character _.

I found a description of how to make _ an active character on Anthony Lieken's website, but that just brings _ and ^ to text mode and does not change the behaviour in math mode.

Question: How do I make _x behave like _\mathrm{x}?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Making _ active was the right idea:

\documentclass{minimal}
\catcode`_=\active
\newcommand_[1]{\ensuremath{\sb{\mathrm{#1}}}}
\begin{document}
a_{text} $a_{text}$
\end{document}

Thanks to \ensuremath (and to Bruno) this can also be used in text mode. For the "old" _ you can then use \sb.

share|improve this answer
3  
And to write a normal subscript, you can use \sb. Also, if you write \ensuremath{\sb{\textrm{#1}}}, you have the added benefit of being able to use it in text mode. –  Bruno Le Floch Jan 20 '11 at 10:32
    
@Bruno: Thanks, I'll add that. –  Hendrik Vogt Jan 20 '11 at 10:33
    
Perfect! Thanks, Hendrik and Bruno! –  Jake Jan 20 '11 at 10:37
1  
After some testing, I would prefer \mathrm over \textrm: it scales better, for instance when writing $\sum\sb{n=n_{min}}\sp{n_{max}}$. –  Bruno Le Floch Jan 20 '11 at 10:44
1  
@Bruno, a late comment: If you load amsmath, then \mathrm and \textrm scale the same, but still, \mathrm is better since it gives the same output in an italic context. –  Hendrik Vogt Feb 15 '13 at 16:10
\documentclass[12pt]{article}

\catcode`\_=\active
\def_#1{\ensuremath{\sb{\mathrm{#1}}}}

\begin{document}

$f_{bar}$ f_x

\[ \int_a^b f(x) \mathrm{d}x \]
\[ \int\sb{a}^b f(x) \mathrm{d}x \]

\end{document}
share|improve this answer
    
Thanks Herbert! Could you explain why you would use \def in this case instead of \newcommand? Am I right in assuming that that's the only difference to Hendrik's/Bruno's answer? –  Jake Jan 20 '11 at 11:19
3  
@Jake: a matter of taste ... A personal decision, when working on TeX level I always use \def to make things clear, that it is no LaTeX specific code I am changing. And sometimes I am too lazy for typing long command names ... :-) –  Herbert Jan 20 '11 at 11:29

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.