开发者

Latex's verbatim: how to indent every instance?

开发者 https://www.devze.com 2022-12-18 17:11 出处:网络
I have a latex document with a bunch of verbatim text. I would like to indent every single instance开发者_如何学编程 of these.

I have a latex document with a bunch of verbatim text. I would like to indent every single instance开发者_如何学编程 of these.

For example:

This is regular text.
\begin{verbatim}
This is verbatim text.
\end{verbatim}

I want "This is verbatim text" to be indented a centimeter or two. How do I do this?


You could wrap all your verbatim environments in quote environments:

\begin{quote}
\begin{verbatim}
This is indented verbatim text.
Works for multiple lines, too.
\end{verbatim}
\end{quote}


This extends ezod's answer above.

\documentclass{article}
\usepackage{fancyvrb}
\newenvironment{qv}
{\quote\Verbatim}
{\endVerbatim\endquote}
\begin{document}
\begin{qv}
This text
is indented.
\end{qv}
\end{document}

EDIT: Another way is to simply add the xleftmargin option to RecustomVerbatimEnvironment before \begin{document}:

\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{xleftmargin=5mm}


Any indent

\catcode`\@=11
\let \saveverbatime \@xverbatim
\def \@xverbatim {\leftskip = 1cm\relax\saveverbatime}
\catcode`\@=12


The answer based on redefining \@xverbatim is the right approach. Unfortunately, this only works for the standard verbatim environment, and lots of people these days use the verbatim style which is much better. (If you don't, start doing it!)

For that one, here is the winning trick:

\makeatletter \def\verbatim@startline{\verbatim@line{\leavevmode\kern20pt\relax}} \makeatother

Put that in your preamble, and every verbatim environment, as well every \verbatiminput file will nicely be indented.

Sheesh people, this took me 30 seconds of reading the source....

Victor.


You could define a new command that indents how you like.

Something like this:

\newcommand{\myverb}[1]{ \indent{ \begin{verbatim} #1 \end{verbatim} } }

This should let you do:

\myverb{ This is verbatim text. }


You might be interested in this because you are formatting a lot of program code. If so, consider the listings package.

0

精彩评论

暂无评论...
验证码 换一张
取 消