# How do you make the underscore active in environments but preserve its orignal use in math mode?

I'm experimenting with creating a formatting shorthand package. I want to use the underscore to delimit an underlined text span, like so

some text _underlined text_ more text


Simply making the underscore active defining a macro

\catcode_=\active%
\def_#1_{\underline{1}}%


is not viable since this obviously collides with the use of the underscore in math mode.

Is it possible to have both work? If so, how?

-
The answer to your question title is yes. The answer to your question body is move complicated. Could you re-word your title to match your document (something more specific)? –  Werner Mar 17 '14 at 19:56
@Werner I changed the title to be more specific. –  FK82 Mar 17 '14 at 20:26

You can do it, but it's better not to. Really, and not only because underlining is considered bad practice in typography. You gain nothing.

\documentclass{article}

\def\ustextormath{\ifmmode\expandafter_\else\expandafter\usunderline\fi}

\begingroup\lccode~=_
\lowercase{\endgroup
\def\usunderline#1~{\underline{#1}}
\protected\def~{\ustextormath}
}

\AtBeginDocument{\catcode\_=\active}

\begin{document}

\section{Some _underlined_ text and $a_1$}

some text _underlined text_ more text and $a_1$

\end{document}


-
What do you mean by "You gain nothing."? –  FK82 Mar 17 '14 at 21:20
@FK82 You clutter your text with strange characters that don't really show the logical markup. –  egreg Mar 17 '14 at 21:24
Ok. Sure. You got a point. –  FK82 Mar 17 '14 at 21:34

Good evening, please try this if it fits your needs. Technically, underscore is an active character from category 8 in my example during typesetting. I used \sb in the math mode and condition for closing \it (\itshape) in a group in the text mode. The \mycondition is returning to false automatically when leaving a group.

Thank you for useful comments and tips egreg, alexis and FK82! It was fun!

\catcode\_=13% Or use \active; from category 8.
\def\false{false}\def\mycondition{false}%
\def_{\ifmmode\sb
\else
\ifx\mycondition\false
\begingroup\def\mycondition{true}\it%\itshape for LaTeXists
\else
\endgroup
\fi
\fi}
Text _more of text_ ToDo $abc_i^2 math$ even more _text_ etc.
%\bye


Bonus: This is a one-liner with \newif involved:

\catcode_=13\newif\ifmyif\myiffalse\def_{\ifmmode\sb\else\ifmyif\endgroup\else\begingroup\myiftrue\it\fi\fi}Text _more of text_ ToDo $abc_i^2 math$ even more _text_ etc.%\bye


-
The usual category code of _ is 8, not 13. –  egreg Mar 17 '14 at 20:57
Oh, and changing the category code of ^^J is definitely not a good idea. Use \sb which is a perfect substitute for the usual _ as a subscript prefix. –  egreg Mar 17 '14 at 21:07
@FK82 First: you can't use a command such as \underline (well, you should never underline text, but that's by way of example). Second, a command such as \itshape should always be used in a group, so the end of the group would reestablish the previous conditions without an explicit font change that might be wrong. Third: try the command in a section title and you'll see. –  egreg Mar 17 '14 at 21:20
Another improvement, for more elegant code: Increment \mytemp immediately after \begingroup, and the \endgroup will restore it to the zero value automatically. –  alexis Mar 17 '14 at 22:05
But you still set it to false manually... Anyway, why don't you just use \newif\emphasis? Then you can say things like \ifemphasis and \emphasistrue. –  alexis Mar 17 '14 at 22:21