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


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.






\section{Some _underlined_ text and $a_1$}

some text _underlined text_ more text and $a_1$


enter image description here

  • What do you mean by "You gain nothing."? – FK82 Mar 17 '14 at 21:20
  • 2
    @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.
      \begingroup\def\mycondition{true}\it%\itshape for LaTeXists
Text _more of text_ ToDo $abc_i^2 math$ even more _text_ etc.

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


  • 2
    The usual category code of _ is 8, not 13. – egreg Mar 17 '14 at 20:57
  • 1
    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
  • 1
    @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
  • 1
    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
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.