5

I need in \subsection string with underscore, like ABC_XYZ.

\subsection{Text ABC_XYZ}

When I try to compile with pdflatex:

Missing $ inserted ...ine {1.2}Text ABC_XYZ}{4}{subsection.1.1.2}
Extra }, or forgotten $ ...ine {1.2}Text ABC_XYZ}{4}{subsection.1.1.2}

I cannot write ABC\_XYZ. It is important for me to find this string ABC_XYZ automaticly. And I need this string in table of contents.

Option

\verb+ABC\_XYZ+ 

dont work.

Can you help me?

Ama
  • 361
  • 3
    Use \_ as in \subsection{ABC\_XYZ} or ABC\textunderscore XYZ –  Apr 01 '14 at 06:31
  • @Harish, I cannot use \_, I need ABC_XYZ in source text. – Ama Apr 01 '14 at 06:40
  • Why you want that in source? Can you please elaborate your use case? –  Apr 01 '14 at 06:44
  • 2
    Do you want to write xyz as the subscript of ABC in real pdf? –  Apr 01 '14 at 06:52
  • No, I need "normal" XYZ, not subscript. – Ama Apr 01 '14 at 06:59
  • 1
    Then why do you need ABC_XYZ in the source file? Please elaborate. –  Apr 01 '14 at 07:01
  • It is important for me to find this string ABC_XYZ automaticly. And I need this string in table of contents. – Ama Apr 01 '14 at 07:02
  • 1
    underscore package, perhaps? – Joseph Wright Apr 01 '14 at 07:49
  • @JosephWright, thank you. I will try without additionals packages, but when it is necessary, I use new package – Ama Apr 01 '14 at 07:59
  • Your issues are in no way related to the fact that "ABC_XYZ" occurs in the header of a subsection. They will arise every bit as much in ordinary running text. – Mico Apr 01 '14 at 08:28
  • @Mico, yes. But in ordinary running text I use \verb+ABC_XYZ+. In \subsection I cannot use \verb. – Ama Apr 01 '14 at 08:39
  • @Ama Then, as you said, your problem is using \verb inside a \section. http://tex.stackexchange.com/questions/83893/verbatim-inside-a-command might help. By the way, you always can \texttt{ABC\_XYZ}. – Manuel Apr 01 '14 at 09:48
  • I don't think that the posting "Verbatim inside a command" provides good answers to this posting. Importantly, the OP has said nothing about the strings that contain _ having to be typeset in typewriter/monospaced font -- which is what the various verbatim environments almost invariably do. – Mico Apr 01 '14 at 13:59
  • @Mico But OP said that “But in ordinary running text I use \verb+ABC_XYZ+. In \subsection I cannot use \verb”, which, at least to me, seems to ask for a monospaced/typewriter font. – Manuel Apr 01 '14 at 14:41

2 Answers2

4

I suppose that if you have underscore characters, you might also have other characters -- such as %, &, #, and $ -- that have a special meaning in TeX. If so, you may want to use the url package and create a command \purl (short for "protected url") so that the strings in question can be parts of "moving arguments" (e.g., when they figure in the argument of a \caption command):

enter image description here

\documentclass{article}
\usepackage{url}
\urlstyle{same} % omit this command if you want monospaced-font look
\newcommand\purl[1]{\protect\url{#1}} % "protected url"
\begin{document}
\tableofcontents
\subsection{Text \purl{ABC_XYZ}}
\end{document}
Mico
  • 506,678
2

I cannot really imagine the situation that requires you to be able to write _ in the title of a section, but of course, sometimes you are faced with obscure problems like that. One way of doing it is to replace

\subsection{Text ABC_XYZ}

by

\catcode`\_=13 
\def_{\textunderscore}
\subsection{Text ABC_XYZ}
\catcode`_=8

I will not swear that it does not have unwanted consequences, but it might be actually be the case, since it seems likely that the definition of \subsection does not use _.

Kristian
  • 1,131
  • 3
    I use _ frequently in section titles. To cite an example: installation guides. Script names may appear in titles and usually contain underscores. – Jonathan Komar Mar 22 '17 at 13:25