2

I want to write some text - not within a \verb - in which underscores can appear and will be treated just like the letters. This requires some low-level TeX magic which I don't know - maybe something like \makeatletter and \makeatother? Anyway, can someone help me with the appropriate snippet? An environment for doing this would be nice, but a starting and ending command for this state is also ok.

  • Well, don’t forget that you can always use \_… :-) – GuM Feb 25 '17 at 0:45
  • @GustavoMezzetti: That's exactly what I want to forget. Think "copy-pasting". – einpoklum Feb 25 '17 at 7:07
4

It's sufficient to change the catcode of _ from 8 into 'letter', i.e. 11.

If it is done inside a group such as provided by an environment, the catcode change does not leak outside.

In order to make in a macro work, the cat code has to be change before the macro argument is read, i.e. this is a job for moving arguments actually, see the \inactive command.

\documentclass{article}

\usepackage[T1]{fontenc}
\newenvironment{inact}{%
  \catcode`\_11%
}{%
}


\newcommand{\inactive}{\begingroup\catcode`\_11 \inactiveinternal}%

\newcommand{\inactiveinternal}[1]{%
  #1\endgroup%
}



\begin{document}
\begin{inact}
Some stuff with_underline_characters
\end{inact}

\inactive{Use this with nice _ characters}

\end{document}

enter image description here

  • Can you also mention what the catcode of an underscore is, normally? Also, does the 11 really need to be "hard-coded" like that? Also, if I put the \catcode command in a brace-group - is that enough to avoid the leak? – einpoklum Feb 24 '17 at 21:27
  • 2
    @einpoklum what would you expect to use instead of a literal 11? if you do not want _ always to be letter but have interpretation depending on some other factor you can of course do that, but you asked for _ to be like a letter, which is 11. – David Carlisle Feb 24 '17 at 21:34
  • 1
    @einpoklum it's possible (\chardef\catcodeofletter=11...\catcode`\_=\catcodeofletter) but doesn't really clarify much I think? – David Carlisle Feb 25 '17 at 1:59
  • 1
    I would use 12 instead of 11 - in case that the underscore is behind some command. – Ulrike Fischer Feb 25 '17 at 15:24
  • 1
    @einpoklum: Knuth himself explains that “[he] has intentionally kept the category codes numeric instead of mnemonic, in order to discourage people from making extensive use of \catcode changes except in unusual circumstances” (The TeXbook, p. 48, eighth to sixth line from the bottom). – GuM Mar 1 '17 at 22:44

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.