# Shift Verbatim environment to the left using \hspace

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}

-

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}


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}

-

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}

-

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

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

-