6

I am using LaTeX and I have a verbatim environment inside an enumerate one. However, there is much whitespace between the verbatim text and the following list item.

What I have now is something like this: Incorrect

I would like to get something like this: Correct

Does anyone know how to achieve it in an elegant way (i.e. without manually using \vspace{-4ex})?

[EDIT] The code I am using is like this:

\begin{enumerate}
    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
        \begin{verbatim}
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Vestibulum dictum quam at sapien pellentesque porta.
        \end{verbatim}

    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
\end{enumerate}
4
  • Welcome to tex.sx! I took the liberty to remove the {latex} tag from your question because most of the questions at our site are about LaTeX.
    – lockstep
    Feb 25, 2011 at 13:56
  • 1
    Could you give us a minimal example so that we know what your code looks like?
    – meep.meep
    Feb 25, 2011 at 14:02
  • @meep.meep: Done
    – yassin
    Feb 25, 2011 at 14:06
  • 1
    Many newbies uses verbatim unnecessarily, and it's typically very rarely the best choice. If you are just looking for the font style, then you should use \texttt{...}. If its describing code, then using listings is preferable. Feb 25, 2011 at 17:43

2 Answers 2

6

The problem seem to be that the verbatim environment is indented. Note that the spaces at the beginning are also taken verbatim. Do not indent it and use e.g. the quote environment for an extra indention in the resulting document if this is wanted.

\documentclass{article}
\begin{document}
\begin{enumerate}
    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
        \begin{quote}
\begin{verbatim}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum dictum quam at sapien pellentesque porta.
\end{verbatim}
        \end{quote}

    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
\end{enumerate}
\end{document}

Result:

Result

1
  • 3
    Thanks. That works. The problem seems that when there is whitespace before \end{verbatim}, LaTeX supposes it is a blank line. The use of the quote environment is awesome. (I am really sorry, but I can't mark this answer as useful since I don't have enough reputation).
    – yassin
    Feb 25, 2011 at 14:27
2

You use too many spaces every line :)

The vertical space around verbatim environment can be reduced like lists. Note that a verbatim environment itself is a list. Say you can use something like:

\begingroup
\topsep=0pt
\itemsep=0pt
\begin{verbatim}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum dictum quam at sapien pellentesque porta.
\end{verbatim}

However, the space before and after verbatim comes from both verbatim and enumerate, you may want to reduce both of them. This can be done by enumitem package, for both together. And you can use fancyvrb package to add the left margin. Here is full code:

\documentclass{article}
\usepackage{enumitem}
\usepackage{fancyvrb}
\begin{document}
\begin{enumerate}[noitemsep]
    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
\begin{Verbatim}[xleftmargin=2em]
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum dictum quam at sapien pellentesque porta.
\end{Verbatim}
    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
        dictum quam at sapien pellentesque porta. Morbi arcu nibh, consequat
        non consequat a, congue sed libero.
\end{enumerate}

\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .