使用 \hspace 将 Verbatim 环境向左移动

使用 \hspace 将 Verbatim 环境向左移动

Verbatim我将一些代码粘贴到环境中的文档中fancyvrb。由于它位于两个嵌套enumerate环境中,因此代码缩进得相当远。结果,一行长代码超出了页面范围。我能想到的最快的解决方法是在环境\hspace之前放置一个带有负参数的命令Verbatim,以便将整个Verbatim环境向左移动几厘米。然而,这没有效果。有人能提出解决方案吗?请参阅下面的 MWE。

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

答案1

Verbatim 是一个列表,并使用 leftmargin \@totalleftmargin。因此,您可以在本地进行更改:

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

答案2

这是软件包的解决方案verbatimbox。请注意,第一个解决方案不会跨页破坏逐字记录。

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

在此处输入图片描述

对于可以打破跨页面边界的逐字记录的解决方案,使用\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} 

答案3

fancyvrb提供xleftmarginxrightmargin作为键值设置来调整逐字文本的左/右边距:

在此处输入图片描述

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

但请注意,使用 时必须包含与代码缩进相关的空格Verbatim。因此,人们通常在这些环境中不使用缩进,无论它看起来很尴尬:

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

相关内容