删除 \lstinline 末尾换行符后的空格

删除 \lstinline 末尾换行符后的空格

我使用 listings 包来表示逐字字体,并将 breaklines 设置为 true,以便自动生成换行符。我将 \emergencystretch 设置为异常大的值,因为这似乎是生成(相对)好看的文本的最佳方式,而无需手动编辑所有地方的格式。

但是,如果在 \lstinline(下例中使用的 = 的简写形式)后立即换行,则会在下一行的开头放置一个空格。如何自动删除这个空格?

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,basicstyle=\ttfamily}
\lstMakeShortInline=
\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

产生:(检查“quick”和“will”之间的空格——第一行末尾,第二段。

在此处输入图片描述

我最初的想法是指定 \lstset{...postbreak=\ignorespaces},但是这不起作用,因为换行符发生在 \lstinline 之后。

也许可以使用 \ifthenelse 语句,但这需要我有一个开关,让我知道在 \lstinline 命令之后是否产生了换行符(可能会很麻烦,因为这只能在排版后确定)。

如有任何想法我将不胜感激。

答案1

如果可拆分内联列表的结尾位于行尾,则 listings 会出现问题。另请参阅listings 包引入了不必要的换行符

你可以尝试breakatwhitespace:

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,breakatwhitespace,
        basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
%\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

或加载微类型

\documentclass[10pt,a5paper]{book}
\usepackage{listings,microtype}
\lstset{breaklines=true,
        basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
%\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

但在我看来,没有一个可以处理所有情况,所以你必须目视检查一切是否正常。

答案2

作为一种解决方法,如果你有一个文档,你只需要出去,那么你可以做

在此处输入图片描述

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
\setlength{\emergencystretch}{35pt}

%\showoutput
\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick=\linebreak[2] will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

但我现在遗漏了一些东西,我不明白为什么空格没有被删除,例如如果你使用\linebreak[0]我可能会稍后添加到这个答案中。这与简短处理无关=,你可以从\lstinline|How quick|

啊,列表在文本末尾添加了一个断点(至少解释了行为),如果您知道您不希望内联列表中断,您可以使用它\leavevmode\hbox{=How quick#}而不是使用\linebreak上述方法,但如果您确实想允许它中断但不是在最后,似乎需要对列表内部进行一些重新排列。

答案3

我建议使用minted(需要使用选项运行 LaTeX-shell-escape并安装pygmentize):

\documentclass[10pt,a5paper]{book}
\usepackage{minted}
\usemintedstyle{bw}

\begin{document}

Blah blah blah, standard text to highlight that usually
the line breaks are normal and no space follows them.
The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first
question. \mintinline{tex}=How quick= will it be answered?
Only time will tell. Seriously though,
it would be nice to get an answer within
about 3 and a half minutes.

\end{document}

在此处输入图片描述

相关内容