Re: Redefine underscore in math mode



Nikolaj Fogh wrote:

Something like that, but it replaces all underscores, even those in labels. It is only underscores in the math environment which should be redefined.

Ok. Then "_" must be active only in math mode. That's that? In fact \mathcode`_="8000 (i.e. "active math") in plain TeX and LaTeX. To use this mathcode, the character must have catcode 11 or 12. So "_" can be use in text!


\documentclass{article}
\usepackage{amsmath}

\let\sub_
\catcode`\_=12
\begingroup
\catcode`\_=\active
\gdef_#1{\sub{\text{#1}}}
\endgroup
\begin{document}
\section{A test}
\label{sec_test}
$P_{with a text}$ in section~\ref{sec_test}. We can typeset
_. Strange, isn't it? In equation~\ref{eq_test}, label works too.

\begin{equation}
  \label{eq_test}
  P_{with a text}
\end{equation}

\end{document}

  Jean-Côme Charpentier
.