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 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.

share|improve this question
    
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 4 '13 at 20:32

4 Answers 4

up vote 10 down vote accepted

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}

enter image description here

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

share|improve this answer

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).

enter image description here

\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}
share|improve this answer

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}

output

share|improve this answer

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}

enter image description here

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.