# Underscores in words (text)

How can I produce the text Word_one_two in LaTeX?

I tried:

Samp\_Dist\_Corr


But, it doesn't quite look right. Also, I want it in the typewriter font, so actually, I'm doing:

\texttt{Samp\_Dist\_Corr}


I find it looks a bit like the underscore is merging in to the bottom of the "D", but maybe it's just because of the typewriter D?

-
This reminds me of one of my pet peeves : all these unneeded special characters in the text mode. If you use the underscore package, then you don't need to escape the _ in text mode. FWIW, in ConTeXt, _ has a letter catcode in text mode, so simply typing Samp_Distt_Corr works. – Aditya Mar 20 '12 at 3:17
underscore works, but Vim still highlights it as an error. To avoid, edit vimXX/syntax/tex.vim: texOnlyMath "[_^]" becomes "[\^]". – Evgeni Sergeev Jul 16 '13 at 4:40
A greater annoyance is that one cannot \includegraphics{filename_with_underscore} when the underscore package is used. – Evgeni Sergeev Aug 1 '13 at 2:27
The solution I've settled on was to \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 4:00
Why is it special? I can understand why \ is special, for example. But what does _ do, except annoy all of us? – Aaron McDaid Nov 4 '14 at 12:51

You may prefer the character from the tt font:

\documentclass{article}

\begin{document}

\texttt{Samp\_Dist\_Corr}

\verb|Samp_Dist_Corr|

\texttt{Samp\char_Dist\char_Corr}

\end{document}


Or probably better add \usepackage[T1]{fontenc} then all the above forms will use the character from the font.

-
I wonder why when _ is used in text mode LaTeX is not smart enough to send it to output directly? – PHPst Oct 11 '13 at 18:23
@PHPst This is not LaTeX it is very low level TeX behaviour and in general TeX doesn't do such switches, compare the behaviour of say \alpha also in the original TeX OT1 encoded TeX fonts, they typically didn't have a _ character so it isn't clear what "output directly" means, choice of switching to tt which did have, or using a rule or... – David Carlisle Oct 11 '13 at 18:27
' it isn't clear what "output directly" means,...' It could simply consider it similar to all other characters by default in non-math mode. – PHPst Oct 12 '13 at 5:11
@PHPst in the standard OT1 encoding, that wouldn't be useful try setting \catcode\_=12 (to make it a standard punctuation character) and you'll seee that unless in tt font you get a dot accent, same as < and > are not useful out of text mode and give spanish punctuation inverted ! and ? – David Carlisle Jan 5 '14 at 2:17

You can use \textunderscore also.

\documentclass{article}
%
\begin{document}
Samp\textunderscore Distt\textunderscore Corr

\texttt{Samp\textunderscore Distt\textunderscore Corr}
\end{document}


Underscore is not merging at the bottom of D actually. It is very close to it.

-
That \textunderscore thing didn't actually work for me: The underscore symbol \texttt{\char_} is a special variable that contains the result of the last printed value. The underscore symbol \texttt{\textunderscore} is a special variable that contains the result of the last printed value. produces minireference.com/static/textunderscore_not_shown_well.png – ivan Jul 22 '14 at 16:37

A fairly elementary way of stripping special meaning from things is to \detokenize them:

\documentclass{article}
\begin{document}
\texttt{\detokenize{Samp_Dist_Corr}}

\texttt{\detokenize{a@b\c_d&e~f g}}
\end{document}


Note how a space is inserted after a "control sequence". See What are the exact semantics of \detokenize?

-
I'm glad I was scrolling through these underscore-related questions: \detokenize is a godsend for basic, machine-generated documents. – Sean Allred Nov 12 '15 at 13:27

I was looking to get the underscore character inside a word in any font, and Google brought me here, so here's the solution I found:

{\_}

word{\_}one{\_}two


produces

_

word_one_two

-
This is already covered in the other answers. – Werner Sep 23 '14 at 2:11
Which answer covers this? All the other answers refer to tt, detokenize, textunderscore, and using underline. – dantiston Sep 23 '14 at 4:12
The original post asks for a typewriter font solution, which is provided in David's answer. Since it also works in other fonts, it makes this answer no different than his. – Werner Sep 23 '14 at 4:40
@dantiston I'm not sure in what sense do you think this is different from what the question says. I'm with Werner, I think this answer doesn't add anything. – Manuel Sep 23 '14 at 18:29
+1. I didnt even read the question fully. But Google brought me here on searching for "underscore in latex" and I feel this is the easiest solution – Pavan Manjunath Sep 16 '15 at 3:21

The solution I like best is to put \chardef\_=_ and use \_ to typeset an underscore. That is because:

• \verb doesn't work in macros,
• \char_ is tedious to write and looks confusing,
• \usepackage[T1]{fontenc} messes up all my fonts,
• \textunderscore doesn't work in \texttt,
• \detokenize looks promising, but I already use a lot of \_,
• {\_} doesn't work in \texttt,
• \underline{{ }{ }} looks really bad and \underline{{ }} still doesn't look quite right,
• \rule is a hack that doesn't look right.

Example:

\documentclass{article}

\begin{document}

\texttt{Samp\_Dist\_Corr} -- original

\texttt{Samp{\_}Dist{\_}Corr} -- brackets

\texttt{Samp\textunderscore Dist\textunderscore Corr} -- textunderscore

\texttt{Samp\underline{{ }{ }}Dist\underline{{ }{ }}Corr} -- double underline

\texttt{Samp\underline{{ }}Dist\underline{{ }}Corr} -- underline

\newcommand{\TextUnderscore}{\rule{.4em}{.4pt}}
\texttt{Samp\TextUnderscore{}Dist\TextUnderscore{}Corr} -- rule

\verb|Samp_Dist_Corr| -- verb

\texttt{Samp\char_Dist\char_Corr} -- char

\texttt{\detokenize{Samp_Dist_Corr}} -- detokenize

\chardef\_=_

\texttt{Samp\_Dist\_Corr} -- chardef

\end{document}


-

The easiest way to have an occasional underscore in text mode (without need to reprogram the whole Matrix...) IMHO is as following:

e.g.:

James\underline{{ }{ }}Bond


which produces a nice James_bond, with a correct spacing between letters

-
Can you explain to a newb what the two inner curly brace pairs do? I assume the outer curly brace pair delimits the "text" to be underlined, which here is just whitespace, and the two inner pairs define the amount of white space, making the underline longer than a simple sequence of spaces which seem to be collapsed into one, as usual. Does this simply define exactly two spaces, preventing them from collapsing, and accidentally underlining two looks good? (Because underlining just one looks as bad as a simple \_.) – Peter A. Schneider Nov 24 '15 at 17:01

In a pinch, a horizontal \rule may also suffice:

\documentclass{article}
\newcommand{\TextUnderscore}{\rule{.4em}{.4pt}}
\begin{document}
\texttt{Samp\textunderscore{}Dist\textunderscore{}Corr}\par
\texttt{Samp\_Dist\_Corr}\par
\texttt{Samp\TextUnderscore{}Dist\TextUnderscore{}Corr}
\end{document}


The width - .4em - can be adjusted to suit. The height - .4pt` - is the "typical" rule-thickness used elsewhere in the document.

-