防止在「\lstinline」中長名稱開頭換行

防止在「\lstinline」中長名稱開頭換行

這個問題是從如何處理很長的 lstinline 片語,例如長類名?

這個問題是關於短語內的換行\lstinline。我提供了一個自動在大寫字母上連字符的答案,然後,在評論中,我意識到有時在下劃線上自動連字符_也很有用。

我設法實現了這一點,但仍然存在一個小問題:我想防止_在名稱開頭自動使用- 連字符(例如,在Python 中,這是常見情況,按照慣例,內部名稱以下劃線(或兩個下劃線)開頭) ))。

\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字元作為開頭。間距有差異。

相關內容