1

Was working on getting line wrapping working for code, and came across a solution which was working for me - until I tried to add a table of contents to my code. The issue seems to be with using the new \texttt{} inside a (sub)section heading. Lots of errors, and the TOC is broken (on Overleaf, for the MWE) - in my actual project, it won't even compile because of the same errors.

I'm using Steven Segletes solution to line-wrapping on specific characters in \texttt{} from here: Line break in texttt

The error stack starts with "Improper alphabetic constant." twice, then goes on to "Missing $ inserted.", "Missing } inserted.", "Missing $ inserted.", and so on.

I'm writing in markdown and compiling with pandoc to tex, so I would really rather not have to have two versions of \texttt (i.e. make a \textttwrap or \textttnowrap or similar).

My MWE (it's using scrarticle, because that's what I'm using, but the issue is the same with article).

\documentclass{scrarticle}
\usepackage[utf8]{inputenc}

\catcode_=12 % \renewcommand{\texttt}[1]{% \begingroup \ttfamily \begingroup\lccode~=/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}% \begingroup\lccode~=[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}% \begingroup\lccode~=.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}% \begingroup\lccode~=_\lowercase{\endgroup\def~}{_\discretionary{}{}{}}% \begingroup\lccode~=:\lowercase{\endgroup\def~}{:\discretionary{}{}{}}% \catcode/=\active\catcode[=\active\catcode.=\active\catcode_=\active\catcode:=\active \scantokens{#1\noexpand}% \endgroup } \catcode`_=8 %

\title{subsection-texttt-mwe} \author{c.angustaylor } \date{January 2021}

\begin{document}

\maketitle

\tableofcontents

\section{Introduction}

\texttt{this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_this_is_long_but_wraps_properly_}

\subsection{\texttt{this_is_tt_subsection}}

Doesn't work with no escapes for _, which is fine in texttt{} with the solution otherwise.

% \subsection{\texttt{this_is_tt_subsection}}

% Also doesn't work with escapes for _

\end{document}

1 Answers1

2

Protect the \texttt

\subsection{\protect\texttt{this_is_tt_subsection}}

Or make the definition generally robust with

\DeclareRobustCommand{\texttt}[1]{%

or with (this requires a new LaTeX, in older LaTeX add \usepackage{xparse}):

\RenewDocumentCommand{\texttt}{m}{%
Ulrike Fischer
  • 327,261
  • Fantastic, thanks so much - \DeclareRobustCommand works perfectly.From doing some digging, seems like that's preferred for data that will be written into aux files and read back, so in this case, the TOC. – Charles Angus Jan 21 '21 at 14:58