列表后的换行符不起作用

列表后的换行符不起作用

在此处输入图片描述

我刚开始使用 Latex,所以这听起来可能是一个非常新手的问题。我在列表中放了一些代码,然后我想添加一些空白行,然后添加“定义 bla bla”行。

问题 1:Define 之前的那个空格,我不知道为什么会出现这种情况......在我的文档的其余部分中没有发生这种情况......

问题 2:我尝试使用 \ 在 \lstlisting 后插入换行符,但是不起作用......

这是我对该地区的代码:

\subsection*{Part C}

Read over and experiment with the following code:\\

\begin{lstlisting}
def is_b_list(x):
    """(object) -> bool
    Return whether x is a binary list.
    >>> is_b_list("b_list")
    False
    >>> is_b_list(0)
    True
    >>> is_b_list([0, 0])
    True
    >>> is_b_list([[0]])
    False
    """
    return (x == 0 or
           (isinstance(x, list) and len(x) == 2
            and all([is_b_list(y) for y in x])))
\end{lstlisting}

\\Define the size of a binary list as the number of left brackets in its Python representation, i.e the total number of list objects in the binary list. So 0 has size 0 and [0, 0] has size 1.

\begin{enumerate}
\item Experiment until you find a formula (probably recursive) that computes the number of different binary lists of size s. Notice that if you call the formula C(s) then C(0) computes 1 and (C1) also computes 1.

这就是我想要实现的目标:

在此处输入图片描述

答案1

您不应\\过多地使用它。通常,您不应该在普通文本中使用它。您必须决定新段落从哪里开始(代码中的空白行)以及它不应该从哪里开始(没有空白行或%)。新段落将始终获得该缩进(在“定义大小...”前面)以使该新段落可见。如果您在这种情况下不想这样做,请使用\noindent

最好的方法是,不要使用空行,因为您不想得到新的段落。这将使您的代码语义保持正确。如果您想在列表周围留出更大的间距,则每个列表的间距都应该相同。因此,必须在序言中定义它。请参阅我的 MWE:

% arara: pdflatex

\documentclass{article}
\usepackage{listings}
\lstset{aboveskip=\baselineskip,belowskip=\baselineskip,basicstyle=\ttfamily}

\begin{document}
\subsection*{Part C}
%
Read over and experiment with the following code:
%
\begin{lstlisting}
def is_b_list(x):
"""(object) -> bool
Return whether x is a binary list.
>>> is_b_list("b_list")
False
>>> is_b_list(0)
True
>>> is_b_list([0, 0])
True
>>> is_b_list([[0]])
False
"""
return (x == 0 or
(isinstance(x, list) and len(x) == 2
and all([is_b_list(y) for y in x])))
\end{lstlisting}
%
Define the size of a binary list as the number of left brackets in its Python representation, i.e.\ the total number of list objects in the binary list. So $0$ has size $0$ and $[0, 0]$ has size $1$.
%
\begin{enumerate}
    \item Experiment until you find a formula (probably recursive) that computes the number of different binary lists of size $s$. Notice that if you call the formula $C(s$) then $C(0)$ computes $1$ and $(C1)$ also computes $1$.
\end{enumerate}
\end{document}

在此处输入图片描述

如果稍后您决定更改间距,您可以重新定义aboveskip=10pt或任何其他度量或将其保留。如果您更改列表样式,则可能会发生这种情况。例如,被一个框或类似的东西包围。这为您提供了最大的灵活性。


注意:我替换了所有空白行,因为%它似乎是一个大段落。您应该尝试一下这些(逐个删除)以查看发生了什么。例如,枚举前面的间距太大(表明这不是新段落的开始)。

相关内容