`\lstinline` 内の長い名前の先頭で改行を防ぐ

`\lstinline` 内の長い名前の先頭で改行を防ぐ

この疑問は長いクラス名のような非常に長い lstinline フレーズをどのように処理しますか?

この質問は、フレーズ内の改行に関するものです\lstinline。大文字を自動的にハイフンで区切る回答を提供しましたが、コメントで、アンダースコアも自動的にハイフンで区切ると便利な場合があることに気付きました_

私はこれを実装することができました。しかし、1 つ問題が残っています。名前の先頭で自動的にハイフンが付けられるのを防ぎたいのです_(たとえば、Python では、慣例により内部名はアンダースコア (または 2 つ) で始まるため、これは一般的な状況です)。

\documentclass{article}
\usepackage[paperwidth=10cm,paperheight=5cm]{geometry}
\pagestyle{empty}
\usepackage{lstdoc}
\lstMakeShortInline[literate={\_}{\_}{1\discretionary{\_}{}{}}]|

\begin{document}
\textbf{Good line break:} in this paragraph the line break in the
python's variable |a_python_style_name| is good!

\textbf{Bad line break:} the internal variable |_internal_python_name|
is not hyphenated very nicely in this paragraph.
\end{document}

<code>\listinline</code> 内の良い改行と悪い改行

答え1

\discretionary最初に何か他のものを挿入して挿入を回避します。いくつかのオプションを次に示します。

ここに画像の説明を入力してください

\documentclass{article}
\usepackage[paperwidth=10cm,paperheight=8cm]{geometry}
\pagestyle{empty}
\usepackage{lstdoc}
\lstset{mathescape=true}
\lstMakeShortInline[literate={\_}{\_}{1\discretionary{\_}{}{}} {~}{\_}{1}]|
\newcommand{\preunderscore}{\_}

\begin{document}
\textbf{Good line break:} in this paragraph the line break in the
python's variable |a_python_style_name| is good!

\textbf{Bad line break:} the internal variable |_internal_python_name|\par
|_internal_python_name|

\textbf{Bad line break:} the internal variable |~internal_python_name|\par
|~internal_python_name|

\textbf{Bad line break:} the internal variable |$\preunderscore$internal_python_name|\par
|$\preunderscore$internal_python_name|
\end{document}

を使用すると、の置換mathescapeの代わりにマクロを挿入できます。または、先頭に別の文字を使用します。スペースに違いがあります。literate_literate

関連情報