2

enter image description here

I just started working with Latex so this may sound like a very nooby question. I put some code in a lstlisting and after that I want to add some blank line and then the "Define the bla bla" line.

Problem 1: That space before Define, I don't know why is that occurring... it doesn't happen in the rest of my document...

Problem 2: I try to insert a line break after \lstlisting using \ but it doesn't work...

This is my code for that area:

\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.

This is what I would like to achieve:

enter image description here

  • The best way to add a break of one line height is with \vspace{\baselineskip}. (\\ is not recommended because it doesn't end the paragraph.) To remove the paragraph indent, use \noindent. – ChrisS Oct 2 '14 at 6:18
  • Welcome to TeX.SX! – LaRiFaRi Oct 2 '14 at 7:05
3

You should not use that \\ too much. Typically, you should not use it at all in normal text. You have to decide, where a new paragraph starts (blank line in code) and where it shouldn't (no blank line or a %). A new paragraph will always get that indent (in front of "Define the size...") to make that new paragraph visible. If you do not want that in this single case, use \noindent.

The best way would be, to leave the empty lines away, as you do not want to get new paragraphs. This will keep your semantics of your code correct. If you want to get bigger spacing around your listing, it should be the same for every listing. And thus, it has to be defined in the preamble. Please see my 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}

enter image description here

If later on, you decide to change the spacing, you can just redefine aboveskip=10pt or any other measure or leave it away. This could happen if you change your listings style. E.g. to be surrounded by a box or alike. This gives you the maximal flexibility.


NB: I replaced all your blank lines by % as it seems to be one big paragraph. You should play around with these (delete one by one) in order to see what is happening. E.g. the spacing in front of your enumeration was too big (suggesting that this was not the start of a new paragraph).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.