5

I want the content of \verbatim to be indented relative to the actual indent of \verbatim, not the exact number of spaces.

For example, I like to indent at sections to make the LaTeX source more readable:

\section{a}
  Regular paragraph.
  \begin{verbatim}
  Gets indented by 2 spaces. Bad!
  \end{verbatim}

The problem is that the verbatim will also get an unwanted 2 space indent in that case.

I could solve it via:

\section{a}
  Regular paragraph.
  \begin{verbatim}
Not indented in output, GOOD. But LaTeX source is ugly. BAD!
  \end{verbatim}

or:

\section{a}
  Regular paragraph.
\begin{verbatim}
Not indented in output, GOOD. but LaTeX source is ugly.
\end{verbatim}

but then I'd lose all my LaTeX source indentation.

Is there a way to only count spaces relative to the indent of the verbatim environment?

If this were possible, I would expect:

\section{a}
  Regular paragraph.
  \begin{verbatim}
Gets indented. Bad!
  \end{verbatim}

to either render without indentation, or with negative indentation, but it does not matter since I don't intend to ever use it.

7
  • related: tex.stackexchange.com/questions/148407/…
    – jub0bs
    Commented Mar 11, 2014 at 20:59
  • @Jubobs I think that question is mostly about automatically indenting all the verbatim outputs. Commented Mar 11, 2014 at 21:03
  • Yes, it's only vaguely related; definitely not a duplicate.
    – jub0bs
    Commented Mar 11, 2014 at 21:05
  • I'd simply not indent all contents; my scheme is <blank line><blank line>\section{Title}<blank line> that, together with syntax coloring makes section titles recognizable.
    – egreg
    Commented Mar 11, 2014 at 21:27
  • egreg is right. You quickly run past the 79th column, especially if you insist on indenting further at each lower-level sectioning command...
    – jub0bs
    Commented Mar 11, 2014 at 21:34

2 Answers 2

6

The verbatim environment is not sophisticated enough for what you're asking. What you can do instead is

  • use an lstlisting environment (from the listings package) in conjunction with the autogobble option (from the lstautogobble package),
  • set the basic style to typewriter font,
  • pass fullflexible to the columns key (more details in 2.10 in the listings manual),
  • set keepspaces to true, which tells the package not to drop spaces to fix column alignment and always converts tabulators to spaces (see subsection 4.13).

so as to mimick verbatim output but automatically remove the leading white space. For convenience, you can even define a custom environment with listings' \lstnewenvironment macro. See below.

Side note: if you really insist on using a verbatim environment, you probably shouldn't leave a newline character just before \end{verbatim}; otherwise, that newline char will be printed verbatim and you'll get an (undesirable?) linebreak. For instance, you should write

\begin{verbatim}
    Regular paragraph.\end{verbatim}

instead of

\begin{verbatim}
    Regular paragraph.
\end{verbatim}

enter image description here

\documentclass{article}

\usepackage{listings}
\usepackage{lstautogobble}

\lstnewenvironment{lstverbatim}[1][]{
  \lstset
  {
    autogobble,
    basicstyle=\ttfamily,
    columns=fullflexible,
    breaklines=true,
    keepspaces=true,
  }
}{}

\begin{document}
\section{a}
    \begin{verbatim}
        Regular paragraph\end{verbatim}
\section{b}
    \begin{lstverbatim}
        Regular paragraph
    \end{lstverbatim}
\end{document}
1

I use the ability to pass optional arguments to the macros of the verbatimbox package in order to condition the verbatim in various ways (typically font size and style). But in this case, I condition it with a negative \hspace equal to the number of \texttt letters of the desired source-indent, which is passed as a mandatory argument to the newly created indentedverb environment.

\documentclass{article}
\usepackage{verbatimbox}
\newenvironment{indentedverb}[1]{%
  \setbox0=\hbox{\texttt{x}}%
  \verbnobox[\hspace{-#1\wd0}]
}{\endverbnobox\vspace{-\baselineskip}}
\begin{document}
\section{My Section}
  text
  \begin{indentedverb}{2}
  \verbatim text
  This is a test
  \end{indentedverb}
  More text
    \begin{indentedverb}{4}
    \verbatim text
    with a four character source indent
    \end{indentedverb}
\end{document}

enter image description here

1
  • 1
    I feel this is not very satisfactory in comparison to Jubobs answer, as it requires users to count a lot of spaces, and changing indentation would be a messy job. Commented Mar 12, 2014 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .