14

I know that it it possible to either escape every single underscore (\_), or apply \usepackage{underscore}.

Escaping is tedious, underscore package breaks \textbf{a_b} and \includegraphics{filename_with_underscore}.

Is there a better solution for math-free texts including multiple underscores (in my case - in URLs)?

  • 1
    If you use hyperref, you can go with \url{http://www.example.com}, the url package offers a similar command. – moewe Dec 22 '14 at 10:21
  • 1
    _ should work without problens in includegraphics – David Carlisle Dec 22 '14 at 10:25
  • 2
    Welcome to TeX.SX! If the document is math free, then \catcode`_=12 in the preamble will do, provided you also have \usepackage[T1]{fontenc} – egreg Dec 22 '14 at 10:26
  • 1
    it is better to mark URL with \url{http://.....} (url or hyperref) as then line breaking is better handled but if you want _ to be a normal character use \catcode`\_=12 make sure you have \usepackage[T1]{fontenc} as OT1 fonts do not have the _ character. – David Carlisle Dec 22 '14 at 10:27
  • @DavidCarlisle - \usepackage[T1]{fontenc} with \catcode`_=12 did what I wanted. Maybe hyperref with url{http://...} is the proper way, but it has obnoxious defaults (bright teal eyesore around every url - why? why?). – reducing activity Dec 22 '14 at 11:10
  • @DavidCarlisle - are you interested into promoting your comment to an answer? I would accept it. – reducing activity Dec 22 '14 at 11:14
  • @moewe url is not adding bright teal eyesore around every url like hyperref, but it is also not working out of the box - urls are refusing to respect margins, despite possibility to fit it by adding line break before url). – reducing activity Dec 22 '14 at 11:21
  • 1
    hyperref adds hyperlinks to URLs (these are by default marked with boxes), you can disable this behaviour or modify it (see, here for example), line breaking in URLs is a tricky issue (see for example here) – moewe Dec 22 '14 at 11:25

2 Answers2

16

It is better to mark URL with \url{http://.....} (url or hyperref) as then line breaking is better handled. Note that url package will just allow linebreaking in URLs and doesn't add any annotation. hyperref uses the same code for linebreaking but also makes the link an active link. By default this adds coloured decoration but the package has several options to control or disable that.

But if you want _ to be a normal character use

\catcode`\_=12 

make sure you have

\usepackage[T1]{fontenc} 

as OT1 fonts do not have the _ character.

David Carlisle
  • 757,742
15

You can have your underscores print as such outside math mode and doing their usual business in math mode:

\documentclass{article}
\usepackage[T1]{fontenc}

\catcode`_=12
\begingroup\lccode`~=`_\lowercase{\endgroup\let~\sb}

\begin{document}

Under_score and $a_{1}$.

\end{document}

enter image description here

For the problem of colored URLs, just load hyperref with the relevant option. With \urlstyle you can also change the default font.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{hyperref}

\hypersetup{colorlinks=false}

\catcode`_=12
\begingroup\lccode`~=`_\lowercase{\endgroup\let~\sb}

\begin{document}

Under_score and $a_{1}$, see at
\url{http://tex.stackexchange.com}


\bigskip

\urlstyle{rm}

This URL uses the normal font
\url{http://tex.stackexchange.com}

\end{document}

enter image description here

egreg
  • 1,121,712
  • It seems that Knuth has prepared the \mathcode of _ as math-active (which starts to be active if the catcode of _ is changed from 8 to 12), but this activity has had another purpose: to draw the underline character. Maybe the reason was that Knuth has had missing underline character in his fonts. This is very curious historical reason why we have not set the _ as math-active to do \sb and \catcode`_=12 by default in all formats. Unfortunately. – wipet Dec 23 '14 at 06:10
  • @wipet The fact that the glyph _ is not in cmr10 has certainly to do with this. Anyway, I stand with the choice of making _ category code 8, as it helps in catching syntax errors. – egreg Dec 23 '14 at 09:28