处理过满内联列表的最佳方法

处理过满内联列表的最佳方法

我正在使用列表包。由于我使用的是 C# 语法,因此列表通常包含长单词(类型名称/变量名称)。由于列表中的单词不会在多行上用连字符连接和断开,因此每当这样的内联列表恰好从行末开始时,我最终都会得到过满的行。

当我使用sloppypar(按照建议)这里),整个段落看起来很糟糕,因为word 将被连字符和拆分成两行,并且空格可能会变得非常宽。当我不使用时sloppypar,个别行可能会超出页面的右边距并突出;也不美观。

我想要实现的是以下两个目标之一,但也许您有更好的建议:

  • 使用完全不同字体的连字符对内联列表中的单词进行连字符连接和拆分。这样,插入的连字符对读者来说就会很明显,并且不会干扰列表的语法。
  • 只让那些线内联列表涉及马虎,但不是整个段落。我愿意牺牲几行的整洁外观,以使段落的大部分看起来漂亮而整洁。

请参阅以下我的 MWE:
整洁与邋遢

\documentclass[a4paper]{article}
\usepackage{color,listings}

\setlength{\textwidth}{120mm}

\lstset{language=[Sharp]C,
    showspaces=false,
    showtabs=false,
    linewidth=\textwidth{},
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    basicstyle=\ttfamily
}

\begin{document}
\section{Tidy, But Overfull}
By writing this sentence, we will get a feeling for the textwidth of this document.
Then we will talk about some variable:
\lstinline$CancellationToken cancellationToken$.
Now that that's out of the way, we can continue writing about other nonsensical subjects which helps reveal the textwidth once again.

\section{Sloppy, But Not Overfull}
\begin{sloppypar}
By writing this sentence, we will get a feeling for the textwidth of this document.
Then we will talk about some variable:
\lstinline$CancellationToken cancellationToken$.
Now that that's out of the way, we can continue writing about other nonsensical subjects which helps reveal the textwidth once again.
\end{sloppypar}

\end{document}

答案1

您可以定义\-为列表内的可选换行符。

literate={\\\-}{}{0\discretionary{{\normalfont\LARGE -}}{}{}}

并按以下方式使用它:

\lstinline$CancellationToken cancellation\-Token$

您可以将其更改\normalfont\LARGE为任何您喜欢的内容,以强调连字符。

平均能量损失

\documentclass[a4paper]{article}
\usepackage{color,listings}

\setlength{\textwidth}{120mm}

\lstset{language=[Sharp]C,
    showspaces=false,
    showtabs=false,
    linewidth=\textwidth{},
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    basicstyle=\ttfamily,
    literate={\\\-}{}{0\discretionary{{\normalfont\LARGE -}}{}{}}
}

\begin{document}
\section{Tidy, But Overfull}
By writing this sentence, we will get a feeling for the textwidth of this document.
Then we will talk about some variable:
\lstinline$CancellationToken cancellation\-Token$.
Now that that's out of the way, we can continue writing about other nonsensical subjects which helps reveal the textwidth once again.
\end{document} 

输出

在此处输入图片描述

答案2

使用

\section{sloppy, with hyphenation}
{\emergencystretch=0.5em\hyphenpenalty=-1000
By writing this sentence, we will get a feeling for the textwidth of this document.
Then we will talk about some variable:
\lstinline$CancellationToken cancellationToken$.
Now that that's out of the way, we can continue writing about other
nonsensical subjects which helps reveal the textwidth once again.\par}

在此处输入图片描述

相关内容