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 pasted some code into a document in a Verbatim environment from fancyvrb. Because it is within two nested enumerate environments, the code is indented quite far over. As a result, one long line of code extends off the page. The quickest fix that I could think of was to place a \hspace command with a negative argument just before the Verbatim environment in order to shift the whole Verbatim environment over to the left a few cm. However, this had no effect. Can someone suggest a solution please? See MWE below.

\documentclass{article}
 \usepackage{fancyvrb}
 \begin{document}
 \begin{enumerate}
  \item First item
  \begin{enumerate}
   \item First sub-item

   \hspace{-5cm}\begin{Verbatim}[fontsize=\tiny]
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
   \end{Verbatim}
  \end{enumerate}
 \end{enumerate}
\end{document} 
share|improve this question

3 Answers 3

Here's a solution with the verbatimbox package. Note that this first solution will not break the verbatim across page boundaries.

\documentclass{article}
 \usepackage{verbatimbox}
 \begin{document}
 \begin{enumerate}
  \item First item
  \begin{enumerate}
   \item First sub-item

   \hspace{-5cm}\begin{verbbox}[\tiny]
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
   \end{verbbox}
   \theverbbox
  \end{enumerate}
 \end{enumerate}
\end{document} 

enter image description here

For a solution that can break the verbatim across page boundaries, with the \hspace:

\documentclass{article}
 \usepackage{verbatimbox}
\textheight 1.5in
 \begin{document}
 \begin{enumerate}
  \item First item
  \begin{enumerate}
   \item First sub-item

   \begin{verbnobox}[\tiny\hspace{-4cm}]
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
   \end{verbnobox}
  \end{enumerate}
 \end{enumerate}
\end{document} 
share|improve this answer

Verbatim is a list and uses as leftmargin \@totalleftmargin. So you can change this locally:

\documentclass{article}
 \usepackage{fancyvrb}
 \begin{document}
 \begin{enumerate}
  \item First item
  \begin{enumerate}
   \item First sub-item

\begingroup
\makeatletter
\@totalleftmargin=-1cm
   \begin{Verbatim}[fontsize=\tiny]
    This line of code is nice and short and fits nicely on the page.
    This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
   \end{Verbatim}
\endgroup

  \end{enumerate}
 \end{enumerate}
\end{document} 
share|improve this answer

fancyvrb provides xleftmargin and xrightmargin as key-value settings to adjust the left/right margins of the verbatim text:

enter image description here

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\begin{enumerate}
  \item First item
  \begin{enumerate}
    \item First sub-item

    \begin{Verbatim}[fontsize=\tiny,xleftmargin=-5cm]
      This line of code is nice and short and fits nicely on the page.
      This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    \end{Verbatim}
  \end{enumerate}
\end{enumerate}
\end{document}

Note though that spaces associated with your code indentation is necessarily included when using Verbatim. As such, one would typically use no indentation inside those environments, however awkward it may seem:

  \begin{enumerate}
    \item First sub-item

    \begin{Verbatim}[fontsize=\tiny,xleftmargin=-5cm]
This line of code is nice and short and fits nicely on the page.
This line of code is very long and does not fit onto the page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!The End
    \end{Verbatim}
  \end{enumerate}
share|improve this answer

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.