I have a LaTeX file for a book chapter, which may need to be converted to Word at some point, because that is what the publishers use (sigh). This article doesn't have any math in it, but I'm still using LaTeX, because even when not writing mathematics, it is better than the alternatives. Unfortunately, I'm making heavy use of characters that are only supposed to appear inside a math environment, notably the underscore character (_). I do use the math environment in a couple of places, but only very briefly.

So, my question is, can I tell LaTeX to make an exception for this specific character, and actually treat the underscore as a underscore? This would make later conversion to Word or some other word processing format easier. The alternative is to use \_ in lots of places. Bonus points if there is some way to still use underscore inside a math environment, not sure how that would work. To be clear, I'd like a general technique that would work with any of the special LaTex characters, though I suppose remapping most of the other ones would cause more trouble than it was worth.

I'm not sure what tags to use here, so please feel free to add. Thanks.

EDIT: It turns out this is a FAQ - How to typeset an underscore character.

2 Answers 2


For the underscore it's quite easy:



Under_score but $a_{x}$

Actually the line \mathcode`_="8000 is redundant, but repeating it makes our intentions clear.

We make the character _ is "math active", i.e., it behaves like a macro, but only in math mode. The \begingroup\lccode... trick defines this macro to be equivalent to \sb which in turn is equivalent to the usual _ for introducing a subscript. In order that it's really seen as a math active character, we need to give it category code 12, which also makes it printable (outside math mode). However, we need a font that has an underscore in the right position, so we load the T1 output encoding.

Other special characters have to be treated in different ways. For example, the $ symbol can be "neutralized" by saying


in the preamble; the package is necessary because it "robustifies" the \( and \) commands. In-line formulas must now be input with these commands, of course.

For the &, one can say


and use \AM for marking alignment points in tabular environments.

Also the # character can be neutralized, as long as after saying


one doesn't try defining new commands.

However, I don't recommend to change catcodes (other than the underscore, perhaps). A "search and replace", in case of a conversion to other formats, is safer.

  • Why such behaviour is not the default (at least for underscore)? Maybe there should be some easy \usepackage for this to avoid inserting arcane lines to each tex file to keep them content-centric (not hacks-centric)?
    – Vi.
    Jul 22, 2015 at 22:43
  • @Vi. Because the underscore has been reserved in the kernel for denoting a subscript in math mode and most documents don't need to typeset several underscores.
    – egreg
    Jul 22, 2015 at 23:25
  • is it possible to limit this behavior to a special environment? in particular, I am trying to fix lstinline...
    – MariusM
    Feb 4, 2016 at 16:23
  • @MariusM Asking a full fledged question seems the best
    – egreg
    Feb 4, 2016 at 16:25
  • A note: since the release of LaTeX 2015/01/01, loading fixltx2e is not needed any more.
    – egreg
    Feb 20, 2016 at 9:30

The underscore package makes the _ behave as \textunderscore in text mode but does not affect the behavior in math mode.


Under_score but $a_{x}$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .