12

I have code. I used \begin{verbatim} \end{verbatim}. My code contains indentation to organize the code. But the verbatim does not add spaces. When I try to add vertical spaces using \quad, the command \quad appears as it is. How can I indent my code inside verbatim?

user6875880
  • 2,183

5 Answers5

12

You can use fancyvrb (this is the CTAN repository); its Verbatim environment respects tabs.

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}

\begin{Verbatim}
1234567890
	12345678901234567890
		1234567890
	12345678901234567890
1234567890
\end{Verbatim}

\begin{Verbatim}[tabsize=4]
1234567890
	12345678901234567890
		1234567890
	12345678901234567890
1234567890
\end{Verbatim}

\end{document}

The default value of tabsize is 8; you can set it once for all with \fvset{tabsize=4} (or whatever value you want) in the document preamble.

Technical note Pasting on the site changes tabs to spaces. The second and fourth lines have one tab, the third line has two.

enter image description here

egreg
  • 1,121,712
  • It works nicely for me. It doesn't like in-line comments though. For example, \begin{Verbatim}[tabsize=4] % comment throws an error that disappears when the comment is on a new line. – XavierStuvw Sep 21 '17 at 15:29
  • 1
    @XavierStuvw It's not possible; anything after \begin{Verbatim} is typeset verbatim, including comments; for implementation reasons, tokens after the options are discarded (with error) – egreg Sep 21 '17 at 15:32
  • 1
    Make sure the V is capital by the way folks. – IntegrateThis Sep 08 '19 at 00:29
4

The verbatim environment ignores tabs but not white space characters. If your code is just a short snippet and you want to avoid excess packages you could just use the space bar. It's perhaps less elegant but since it will be displayed in a monospace typeface it's not all that bad in my opinion.

2

Here are two ways with the verbatimbox package. The first way will pagebreak, but adds space above/below the environment. The second way does not page break, but is flexibly manipulated in the manner of a LaTeX box (i.e., one can apply \centering, etc. to the box or, as here, frame it)

\documentclass{article}
\usepackage{verbatimbox,lipsum}
\begin{document}
\lipsum[1]

\begin{verbnobox}[\small\slshape\hspace{1in}]
Lorem ipsum
do\or $it amet%,
consectetuer
\end{verbnobox}

\lipsum[4]
\begin{verbbox}[\small\slshape]
Lorem ipsum
do\or $it amet%,
consectetuer
\end{verbbox}
\noindent\hspace{1in}\fbox{\theverbbox}

\lipsum[4]
\end{document}

enter image description here

0

The package moreverb also solves that issue and provides lines numbering.

chris
  • 144
0

In my case verbatim started to respect indentation only after I switched all tab symbols to equivalent amounts of whitespaces. Initial code and output in latex(note that tabs are designated as \t here):

\begin{verbatim}
\t  for i in range(len(W)): 
\t  \t  print(f'W_{i+1}(s) = {W[i]}')
\end{verbatim}

enter image description here:

After said changes:

\begin{verbatim}
    for i in range(len(W)): 
        print(f'W_{i+1}(s) = {W[i]}')
\end{verbatim}

enter image description here