有人知道如何让内联listings
换行吗?请看我下面附上的截图。第二行中的代码示例超出了文本边距。
另外,您知道为什么\lstinline|{ print "" }|
第三行文本的内联列表会删除引号后的空格吗?它显示的内容与{ print ""}
书中所述相同,尽管我的列表是{ print "" }
。
这是您在屏幕截图中看到的文档的完整 LaTeX,
\documentclass[11pt]{memoir}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage[T1]{fontenc}
\begin{document}
\newcommand{\bb}[1]{\textbf{#1}}
\newcommand{\code}[1]{\emph{#1}}
\newcommand{\n}{\lstinline|\n|}
\definecolor{codebg}{HTML}{EEEEEE}
\definecolor{codeframe}{HTML}{CCCCCC}
\lstset{language=Awk}
\lstset{backgroundcolor=\color{codebg}}
\lstset{frame=single}
\lstset{framesep=10pt}
\lstset{rulecolor=\color{codeframe}}
\lstset{upquote=true}
\lstset{basicstyle=\ttfamily}
\lstset{emph={awk}, emphstyle=\textbf}
\chapter{Line Spacing}
\label{linespacing} % So I can \ref{filespacing} later.
\section{Double-space a file}
\label{doublespacefile}
\begin{lstlisting}
awk '1; { print "" }'
\end{lstlisting}
So how does this one-liner work? A one-liner is an Awk program and every Awk program consists of a sequence of pattern-action statements \lstinline|pattern { action statements }|. In this case there are two statements \lstinline|1| and \lstinline|{ print "" }|. In a pattern-action statement either the pattern or the action may be missing. If the pattern is missing, the action is applied to every single line of input. A missing action is equivalent to \lstinline|{ print }|. Thus, this one-liner translates to:
\begin{lstlisting}
awk '1 { print }; { print "" }'
\end{lstlisting}
An action is applied only if the pattern matches, i.e., pattern is true. Since \code1 is always true, this one-liner translates further into two print statements:
\begin{lstlisting}
awk '{ print }; { print "" }'
\end{lstlisting}
Every print statement in Awk is silently followed by the \code{ORS} - Output Record Separator variable, which is a newline by default. The first print statement with no arguments is equivalent to \lstinline{print \$0}, where \code{\$0} is the variable holding the entire line (including the newline at the end). The second print statement seemingly prints nothing, but knowing that each print statement is followed by \code{ORS}, it actually prints a newline. So there we have it, each line gets double-spaced.
We can also drop the semicolon and write it as:
\end{document}
答案1
您可以使用选项启用换行breaklines
,但我怀疑您是否会喜欢这个结果(例如,括号后会有换行符),因为listings
不知道您觉得什么“好看”。我可能会将代码拆分为两个或更多\lstinline
命令。
我认为缺少空格是一个错误。解析字符串分隔符会丢失空格(但仅限于固定宽度字体)。您可以在“后使用两个空格,或者使用以下方法删除引号作为字符串分隔符 \lstset{deletestring=[b]{"}}