Saturday, September 22, 2012

redefining underscore in math mode to use rm font

So I use a lot of subscripts, and it annoyed me that they were in italics! I didn't want to call \textrm{} every time in math mode, and there had to be a better way!
I found this stackexchange post which described exactly what I wanted to do and this was their solution:
\catcode`_=\active
\newcommand_[1]{\ensuremath{\sb{\mathrm{#1}}}}
Unfortunately that redefines _ for ALL uses of it, including in file names etc. I had a lot of file names and folder that used that character, so that wasn't an option. Some more searching and I found:
\documentclass{article}
\usepackage{amsmath}

\let\sub_
\catcode`\_=12
\begingroup
\catcode`\_=\active
\gdef_#1{\sub{\text{#1}}}
\endgroup
\begin{document}
Which worked a charm! Now all my subscripts are automatically in roman font!