我正在使用以下命令进行修改,texttt{}
以便它自动中断长行(不插入连字符),这在编写必须编写冗长的 URL、命令行示例等的文档时非常有用。
\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
}
我似乎无法为texttt{}
viasoul
或hl{}
具有此功能的命令启用突出显示。如何同时启用此换行功能和突出显示?
我已尝试通过以下方式实现此目的:
\usepackage{soul}
...
\scantokens{\hl{#1}\noexpand}%
然而,这会导致文本在句子中间被截断。
谢谢。
答案1
由于问题清楚地引用了该问题的答案:文本中的换行符,我会尝试一下。
\hl
无论出于何种原因,在修订版中使用会导致字符串中特殊/活动字符 ( , , )\texttt
的数量被截断。唯一的解决方案是用该数量的额外标记填充输入字符串,这样就可以吃掉所需的字符串,同时留下填充。/
[
.
\hl
虽然我没有然而找到一种在 内部即时执行此操作的方法\texttt
,我确实弄清楚了如果在输入 之前收到警报,该如何执行此操作\texttt
。因此,宏\HLtt{}
用于突出显示完整的\texttt
字符串(而不仅仅是其中的一部分)。
它的工作原理(在调用之前\textt
)是将参数传递给listofitems
计数特殊字符。然后,它会为每次出现特殊字符而将 加一。最后,它x
使用原始参数调用\tmp
\textt
加填充。
\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}