I have blocks surrounded by \begin{verbatim} and \end{verbatim}. I'd like to indent these about half an inch. I'm using pandoc to write pdfs via XeLaTeX and the default.latex pandoc template, so I'd like not to create a new environment, but to revise the behavior of verbatim in default.latex so that it is indented a little.
- 752
-
My first guess would be to put every verbatim into a minipage, but that is hard to decide without a minimal working example. – Johannes_B Dec 04 '13 at 20:32
4 Answers
The package fancyvrb has the enhanced Verbatim environment, and the key xleftmargin:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\thispagestyle{empty}
\noindent A non-verbatim line
\begin{Verbatim}[xleftmargin=.5in]
Some verbatim
lines
\end{Verbatim}
\noindent Another line
\end{document}

Moreover, you can redefine the standard verbatim like this:
\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=.5in}
\begin{document}
\thispagestyle{empty}
\noindent A non-verbatim line
\begin{verbatim}
Some verbatim
lines
\end{verbatim}
\noindent Another line
\end{document}
This gives the same result as above
- 38,129
The verbatim package has an internal interface that allows one to do this. One has to redefine \verbatim@processline, adding an \hspace command at its start:
\documentclass{article}
\usepackage{verbatim}
\newlength\myverbindent
\setlength\myverbindent{1in} % change this to change indentation
\makeatletter
\def\verbatim@processline{%
\hspace{\myverbindent}\the\verbatim@line\par}
\makeatother
\begin{document}
\noindent Unindented text followed by 1 inch indented verbatim:
\begin{verbatim}
verbatim \stuff
\and more stuff
\end{verbatim}
\setlength\myverbindent{.5in}
\noindent Unindented text followed by .5 inch indented verbatim:
\begin{verbatim}
verbatim \stuff
\and more stuff
\end{verbatim}
\end{document}

- 6,899
verbatim like most LaTeX display environments is a list environment, and one of the main reasons for that is so that they do the right thing when nested in other display environment such as quote (or a similar custom environment with different indentation).
\documentclass{article}
\begin{document}
X\dotfill X\\
X\dotfill X
\begin{verbatim}
%
$$
@@@
####
\end{verbatim}
X\dotfill X
\begin{quote}
\begin{verbatim}
%
$$
@@@
####
\end{verbatim}
\end{quote}
X\dotfill X
\end{document}
- 757,742
As long as your verbatim block doesn't span a page break, you can use the verbatimbox package. The verbbox environment puts the verbatim into a box that can be recalled with \theverbbox. The [t] option makes sure the box is strutted, to give the proper vertical spacing from the previous line.
\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\noindent Unindented text
\begin{verbbox}
This is
\verbatim text
\end{verbbox}
\theverbbox[t]
\noindent Unindented text
\end{document}

- 237,551