2

I'm using the following command to modify texttt{} so that it automatically breaks long lines (without inserting a hyphen), and it's very helpful when writing documents where I have to write lengthy URLS, command-line examples, etc.

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

I can't seem to enable highlighting for texttt{} via soul or the hl{} commands with this functionality. How do I enable both this line-breaking functionality and highlighting?

I've attempted this via:

\usepackage{soul}
...
  \scantokens{\hl{#1}\noexpand}%

However, this causes the text to be truncated mid-sentence.

Thank you.

1 Answer 1

3

Since the question clearly references the answer at this question: Line break in texttt, I'll take a stab.

For whatever reason, the use of \hl inside of the revised \texttt causes truncation by the amount of special/active characters (/, [, .) inside the string. The only solution is to pad the input string by that number of extra tokens, so that \hl eats up the desired string, while leaving the padding behind.

While I did not yet find a way to do this on-the-fly inside of the \texttt, I did figure out how to do it if alerted before entering \texttt. Thus, the macro \HLtt{} is used to highlight the complete \texttt string (rather than just a piece of it).

It works (before invoking \textt) by passing the argument to listofitems to count the special characters. Then it adds one x to \tmp for each occurrence of a special character. Finally it invokes \textt with the original argument plus the padding.

\documentclass[a4paper]{article}
\usepackage{listofitems}
\setsepchar[?]{/||[||.}% SET THE CHARS TO SEARCH FOR
\newcommand\HLtt[1]{%
  \readlist\tmplist{#1}% FIND THE SEARCHED FOR CHARS (CALL IT n)
  \def\tmp{}%
  \foreachitem\x\in\tmplist[]{% CREATE A STRING OF n-1 TOKENS
    \ifnum\xcnt=1\relax\else%
      \expandafter\def\expandafter\tmp\expandafter{\tmp x}\fi}%
  \def\tmpA{#1}%
  \expandafter\expandafter\expandafter\texttt%
  \expandafter\expandafter\expandafter{%
  \expandafter\expandafter\expandafter\hl%
  \expandafter\expandafter\expandafter{%
  \expandafter\tmpA\tmp}}%
}


\renewcommand{\texttt}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active
  \scantokens{#1\noexpand}%
  \endgroup
}
\usepackage{soul,xcolor}
\begin{document}
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

If an unrecoverable error occurs during the transformation, then a
\HLtt{\$GLOBALS['TCA']['tt\_address']['feInterface']['fe\_admin\_fieldList']}

\HLtt{\$GLOBALS['TCA']['tt\_address']['feInterface']}

\HLtt{\$GLOBALS['TCA']['tt\_address']}

\HLtt{\$GLOBALS['TCA']}

\end{document}

enter image description here

9
  • I found the solution from your linked question some time back, but couldn't find the original post via Google. Thank you for taking the time to answer this.
    – Cloud
    Commented Jan 29, 2019 at 18:42
  • @DevNull I see my answer is not universal. The number of added tokens (x in my MWE) should equal the total number of /, [ and . encountered. Commented Jan 29, 2019 at 18:48
  • In some cases, this works, but in other cases, it appends a xxxx literal to the text. Longer strings render find, but a short string (i.e. \HLtt{PUT}) renders as PUTxxxx.
    – Cloud
    Commented Jan 29, 2019 at 18:49
  • Typo above: longer strings render fine, but short strings have xxxx appended to them.
    – Cloud
    Commented Jan 29, 2019 at 18:55
  • @DevNull See my 2nd comment. The right number of filler to add depends on how many instances of the special characters appear. Commented Jan 29, 2019 at 18:58

You must log in to answer this question.

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