Change Category Code
For those not interested the functionality of the underscore in LaTeX and just want it to function like any other character, the category code of the underscore can be changed to make it work as might be expected. Here, we assign the underscore the same category code that most punctuation has. Choose one of the following syntaxes according to your preference:
\catcode`\_=12 % alphabetic constant syntax
\catcode`_=12 % alphabetic constant syntax
\catcode"5F=12 % unicode decimal syntax
\catcode'137=12 % octal syntax
\catcode95=12 % decimal syntax
This way, you can go on about your happy life using the underscore as you normally would. If you need to subset text in math mode, the macro \sb can still be used in math mode for subscript.
Revert Category Code Change
This explanation would not be complete without stating how to restore the underscore to its original category code. Pick one of the following syntaxes according to your preference:
\catcode`\_=8 % alphabetic constant syntax
\catcode`_=8 % alphabetic constant syntax
\catcode"5F=8 % unicode decimal syntax
\catcode'137=8 % octal syntax
\catcode95=8 % decimal syntax
See egreg's answer: Is it safe to set underscore to a non-active character?
If you use the
underscorepackage, then you don't need to escape the_in text mode.FWIW, in ConTeXt,
– Aditya Mar 20 '12 at 03:17_has a letter catcode in text mode, so simply typingSamp_Distt_Corrworks.underscoreworks, but Vim still highlights it as an error. To avoid, editvimXX/syntax/tex.vim:texOnlyMath "[_^]"becomes"[\^]". – Evgeni Sergeev Jul 16 '13 at 04:40underscorepackage is that it's not bold in\textbf{a_b}. – Evgeni Sergeev Jul 30 '13 at 07:34\includegraphics{filename_with_underscore}when theunderscorepackage is used. – Evgeni Sergeev Aug 01 '13 at 02:27\usepackage[Q=yes,pverb-linebreak=no]{examplep}and then\Q{identifier_typeset_in_monospace}. This suits me, because all my underscores occur in code: variables, function names, filenames, etc., all of which could be typeset in a verbatim-like environment. – Evgeni Sergeev Aug 23 '13 at 04:00_do, except annoy all of us? – Aaron McDaid Nov 04 '14 at 12:51The smallest $n such that $2^n>1000$ is~10.[…] TeX will automatically insert a$before the^, and you will get an error message. In this way the computer has gotten back into synch, and the rest of the document can be typeset as if nothing had happened.” – ShreevatsaR Jun 29 '17 at 01:39\usepackage{underscore}. I do not recommend the usage of this package and the upvoting of the comment suggesting the use of it. – tommy.carstensen Nov 28 '17 at 11:29works, as well as_{--}` for a longer dash – verdy_p May 19 '19 at 03:42\_does not work at all (invalid syntax encountered) in many simple TeX implementations, even within the block of\text{...}or\mbox{...}, and\textttis also not working (requires also an additional package whose transclusion by\usepackageis restricted/forbidden) – verdy_p May 19 '19 at 03:50{}before the `metacharacter for subscripts. So a full substitute of the underscore is{}-, or{}{select your dash here}`. – verdy_p May 19 '19 at 15:41\_or_-in the font package definition you use for the monospaced style, but_—(using the em-dash U+2014) should be OK. – verdy_p May 19 '19 at 15:53_(and other characters) to work in text mode by default and don't need to use the math mode: see Handling of special LaTeX characters in text - TeX - LaTeX Stack Exchange – user202729 Jan 05 '22 at 12:34