
\texttt{}
LaTeX で、空白を含まない長いテキストを改行したいのですが、現状ではテキストがページからはみ出てしまいます。
LaTeX ドキュメントは reStructureText ドキュメントから生成されます (reStructuredText を使用する必要があります)。したがって、次のソリューションは使用しないことを推奨します。
- または
\texttt{}
に置き換える(Docutilsでこれを行う方法を誰かが教えてくれない限り)\path{}
\url{}
- / ソフトハイフンの挿入
\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」を使用)、「TranformerException」の「TranformerEx」のみが表示されます。
答え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}
\renewcommand
このソリューションをセクションやその他の見出し、または .aux ファイルに書き込まれ、そこから読み戻されるその他の場所で使用する必要があれば、次のものを置き換える必要があります\DeclareRobustCommand
。詳細については、この回答を参照してください。折り返し \texttt{} とセクションおよび目次の組み合わせ - 不適切なアルファベット定数
補足
Lemdan はコメントで、アンダースコアをブレークポイントにするにはどうすればよいかと尋ねています (Joey もかなり前に尋ねていました)。アンダースコアは通常、catcode 8 であるため、代わりに の catcode を12 に設定する_
という議論を受け入れなければなりません。もちろん、これにより 内の math 内での添え字の使用ができなくなりますが、それほど難しい問題ではないはずです。これにより、 内のアンダースコアをエスケープする必要もなくなります。\texttt
_
\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
Steven の回答に従って、私のドキュメントでは、パスと URL を表示するために図を使用していましたが、テキストでは小さなコード部分を強調表示するために\texttt
も使用していました。\texttt
Steven が提供したコードは私の図では機能しますが、\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 から、 が発生した時点でコードをブレーク可能にしたいと考えていると推測しました.
。長めのテキストに遭遇する可能性のあるすべての状況でこのような分離された名前が使用され.
、ドキュメントの他の目的には必要ないと仮定すると\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}