Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 '11 at 13:56
    
@lockstep: OK. Thanks for the correction :) –  yassin Feb 25 '11 at 13:59
1  
Could you give us a minimal example so that we know what your code looks like? –  meep.meep Feb 25 '11 at 14:02
    
@meep.meep: Done –  yassin Feb 25 '11 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. –  Mikael Öhman Feb 25 '11 at 17:43
add comment

2 Answers

up vote 3 down vote accepted

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

share|improve this answer
2  
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 '11 at 14:27
add comment

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

share|improve this answer
    
Thanks for your answer –  yassin Feb 25 '11 at 14:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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