texttt 中的換行符

texttt 中的換行符

我希望乳膠能夠打破內部\texttt{}不包含任何空格的長文本。目前,文字剛剛脫離頁面。

Latex 文件是從 reStructureText 文件產生的(我必須使用 reStructuredText)。因此我不想使用這些解決方案:

  • 替換\texttt{}\path{}or \url{}(除非有人能告訴我如何讓 Docutils 做到這一點)
  • 插入\allowbreak{}/ 軟連字元(可行,但會破壞 reStructuredText 文件的可讀性)

Latex 文件中是否有一些全域配置會導致 texttt 字串中斷?還有其他一些好方法來獲取顯示所有文字的可列印文件嗎?

這是我的問題的一個最小示例:

\documentclass[a4paper]{article}
\begin{document}
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.
\end{document}

當我嘗試建立 pdf(使用“pdflatex a.tex”)時,只有“TranformerEx”對“TranformerException”可見。

答案1

竊取egreg的答案如何在不使用 url 套件的情況下模擬 \url 連字符?,其中他重新定義了\url,我從相同的重新定義開始,\texttt而不是重新定義\url。在egreg 的原始版本中,它允許在./字元處換行。

但是可以加入其他斷點符號,例如下面我也打了[一個斷點:

\documentclass[a4paper]{article}
\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{lipsum}
\begin{document}
\lipsum[4]
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
\texttt{\$GLOBALS['TCA']['tt\_address']['feInterface']['fe\_admin\_fieldList']}
\end{document}

在此輸入影像描述

如果您需要在部分或其他標題或寫入 .aux 檔案並從 .aux 檔案中讀回的其他位置使用此解決方案,則需要替換\renewcommand\DeclareRobustCommand- 請參閱此答案以獲取更多資訊:將換行 \texttt{} 與部分和目錄結合 - 不正確的字母常數


補充

Lemdan 在評論中詢問(Joey 很久以前就問過)如何讓下劃線成為_斷點?因為底線通常是 catcode 8,所以必須吸收參數來\texttt使用設定為 12 的 catcode _\texttt這也將消除在 中轉義下劃線的需要\texttt

\documentclass[a4paper]{article}

\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{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active\catcode`_=\active
  \scantokens{#1\noexpand}%
  \endgroup
}
\catcode`_=8 % 

\usepackage{lipsum}
\begin{document}
\lipsum[4]
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
\texttt{\$GLOBALS_'TCA']['tt_address']['feInterface']['fe_admin_fieldList']}
\end{document}

在此輸入影像描述

答案2

根據史蒂文的回答,在我的文件中,我\texttt在圖中使用來顯示路徑和網址,但也在\texttt文字中使用來突出顯示小段程式碼。

雖然史蒂文提供的程式碼適用於我的圖形,但在\texttt為不使用符號的普通文字編寫命令時,它仍然會溢出邊距。如下圖所示,\texttt對​​於數字來說效果很好,但對於普通文字則效果不佳。

在此輸入影像描述

為了解決這個問題,我借用了程式碼\justify 來自 tex.se 的帖子並將其添加到現有程式碼中。

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}

\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
  \justify\scantokens{#1\noexpand}%
  \endgroup
}

這會產生更好的輸出,並且適用於我在不同地方使用打字機的情況。

在此輸入影像描述

答案3

我不確定您願意從序言中做多少事情,但這裡有一個建議:

\documentclass{article}

\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}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

\end{document}

從你的 MWE 來看,我假設你希望你的程式碼在.發生 a 的地方是可破壞的。假設您可能遇到較長文字的所有情況都涉及此類分隔名稱,並且您在文件中.不需要用於任何其他目的,那麼這可能是您的解決方案。\texttt

如果您可能擔心另一個套件可能希望.處於活動狀態但定義不同,您可以採取以下方法:

\documentclass{article}

\begingroup
  \catcode`\.=\active
  \gdef\redefineperiod{\def.{\normalperiod\allowbreak}}%%
\endgroup

\newcommand\aepath[1]{%%
  \bgroup 
    \let\normalperiod=.%%
    \catcode`\.=\active
    \redefineperiod
    \everyeof{\noexpand}%%
    \endlinechar=-1%%
    \ttfamily\scantokens{#1}%%
  \egroup}

\let\oldtexttt\texttt
\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.  Followed
by more text which is just to fill to the ned of the line.

\end{document}

天真地(即:這是我最初認為我可以做的),你可以嘗試

\documentclass{article}

\newcommand\aepath[1]{%%
  \bgroup 
    \let\normalperiod=.%%
    \catcode`\.=\active
    \def.{\normalperiod\allowbreak}%%
    \everyeof{\noexpand}%%
    \endlinechar=-1%%
    \ttfamily\scantokens{#1}%%
  \egroup}

\let\oldtexttt\texttt
\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.  Followed
by more text which is just to fill to the ned of the line.

\end{document}

但這將會失敗,因為接下來的時間\def已經被標記化並且不活躍。因此,LaTeX 將拋出有關缺少控制序列的錯誤。

更新

如果您不是特別關心中斷發生的位置,那麼您可以使用類似以下的內容:

\documentclass{article}

\makeatletter
\newcommand\aepath[1]{%%
  \bgroup
    \ttfamily
    \ae@path#1\relax\@nil
  \egroup}
\def\ae@path#1#2\@nil{%%
  \def\ae@continue{}%%
  \detokenize{#1}\unskip\penalty\z@  
  \ifx\relax#2%%
  \else 
    \def\ae@continue{\ae@path#2\@nil}%%
  \fi
  \ae@continue}
\makeatother

\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

\end{document}

雖然這對我來說似乎有點次優。我認為最好決定斷點應該在哪裡,並按照示例.將這些字符(例如/-等)變成命令上下文中的活動字符,並偷偷添加懲罰以允許在它們之後中斷。

答案4

這是一個遲到的回复,但我發布了一個帶有答案的問題這裡,這又是一字不差的:

使用 \seqsplit。

\documentclass[9pt,oneside]{amsart}

\usepackage{seqsplit}

\begin{document}

Filler text. Filler text. Filler text. Filler text. \seqsplit{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}

\end{document}

使用 seqsplit 分割長文本。

相關內容