![texttt 中的换行符,与数学模式下的 textttt 配合使用](https://linux22.com/image/369434/texttt%20%E4%B8%AD%E7%9A%84%E6%8D%A2%E8%A1%8C%E7%AC%A6%EF%BC%8C%E4%B8%8E%E6%95%B0%E5%AD%A6%E6%A8%A1%E5%BC%8F%E4%B8%8B%E7%9A%84%20textttt%20%E9%85%8D%E5%90%88%E4%BD%BF%E7%94%A8.png)
我希望有自动换行符\texttt
(特别是对于我的情况,在文本模式下),同时\texttt
在数学模式下不会导致构建错误。添加连字符或使用\path
或\url
并不美观,因为最好显示文本 0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba,没有任何连字符或其他会中断它的字符,也没有 URL 边框,此外,能够双击文本进行复制和粘贴也很好。
梅威瑟:
\documentclass[9pt,oneside]{amsart}
\begingroup
\catcode`\.=\active
\gdef.{\normalperiod\allowbreak}%
\endgroup
\newcommand\aepath[1]{%%
\bgroup
\let\normalperiod=.%%
\catcode`\.=\active
\everyeof{\noexpand}%%
\endlinechar=-1%%
\ttfamily\scantokens{#1}%%
\egroup}
\let\oldtexttt\texttt
\let\texttt\aepath
\begin{document}
filler text $\texttt{KEC}$
This is more filler text. This is more filler text. This is more filler text, \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\end{document}
我尝试过以下解决方案文本中的换行符。如果我尝试其中任何一个答案,我都会得到以下错误。上面的 MWE 取自https://tex.stackexchange.com/a/219454/143781,我得到:
! LaTeX Error: Command \ttfamily invalid in math mode.
...
l.23 filler text $\texttt{KEC}
$^^M
! ==> Fatal error occurred, no output PDF file produced!
答案1
我不确定你为什么要重载\texttt
,但以下是有效的。
\documentclass[9pt,oneside]{amsart}
\usepackage{letltxmacro}
\begingroup
\catcode`\.=\active
\gdef.{\normalperiod\allowbreak}%
\endgroup
\DeclareRobustCommand\aepath[1]{%%
\bgroup
\let\normalperiod=.%%
\catcode`\.=\active
\everyeof{\noexpand}%%
\endlinechar=-1%%
\oldtexttt{\scantokens{#1}}%%
\egroup
}
\LetLtxMacro\oldtexttt\texttt
\let\texttt\aepath
\begin{document}
filler text $\texttt{KEC}$
This is more filler text, \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\end{document}
以下是如何自动拆分长序列的方法。我还添加了相同的方法\seqsplit
。使用新\aepath
宏,您可以决定在多少个字符后可以中断,而 的实现\seqsplit
不允许这样做。
\documentclass[9pt,oneside]{amsart}
\usepackage{xparse}
\usepackage{seqsplit}
\ExplSyntaxOn
\NewDocumentCommand\aepath{O{4}m}
{
\texttt{ \ray_aepath:nn { #1 } { #2 } }
}
\cs_new:Nn \ray_aepath:nn
{
\tl_range:nnn { #2 } { 1 } { #1 }
\tl_map_function:fN
{ \tl_range:nnn { #2 } { #1+1 } { \tl_count:n { #2 } } }
\ray_aepath_addbreak:n
}
\cs_generate_variant:Nn \tl_map_function:nN { f }
\cs_new:Nn \ray_aepath_addbreak:n
{
\hspace{0pt plus 0.2pt} #1
}
\ExplSyntaxOff
\begin{document}
This is more filler text,
\aepath{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\begin{minipage}[t]{4.5cm}
This is more filler text,
\aepath{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\end{minipage}\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\aepath{0xcf416c5},
\end{minipage}\qquad\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\aepath[1]{0xcf416c5},
\end{minipage}
\bigskip\bigskip
\begin{minipage}[t]{4.5cm}
This is more filler text,
\texttt{\seqsplit{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}},
\end{minipage}\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\end{minipage}\qquad\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\end{minipage}
\end{document}
答案2
您可以\mathtt
在数学模式中使用:
\documentclass[9pt,oneside]{amsart}
\begingroup
\catcode`\.=\active
\gdef.{\normalperiod\allowbreak}%
\endgroup
\newcommand\aepath[1]{%%
\bgroup
\let\normalperiod=.%%
\catcode`\.=\active
\everyeof{\noexpand}%%
\endlinechar=-1%%
\ttfamily\scantokens{#1}%%
\egroup}
\let\oldtexttt\texttt
\let\texttt\aepath
\begin{document}
filler text $\mathtt{KEC}$
This is more filler text, \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\end{document}