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 Answer 1

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}{%
1
  • 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. Commented Jan 21, 2021 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .