11

The following code (as a small example):

\begin{verbatim}                                                                
         #####                                                              
       #########                                                            
     #############                                                       
\end{verbatim}

gets rendered like this:

    #####
  #########
#############

What's happening is that the verbatim block is scanned and the leading whitespace block is removed, or normalized somehow. How can I stop this from occurring?

Charles
  • 133
  • 2
    I would guess that the leading space are tabs not spaces, easiest is to fix your source to use spaces, or if not the verbatim and other packages have some support for tabs. – David Carlisle Jul 01 '16 at 07:46
  • Ah, you're right, they are tabs (at least partially). Maybe I could set a tab length defined in spaces? – Charles Jul 01 '16 at 07:48
  • @Charles as I say, you can in the verbatim package extended version of verbatim, not in the one built in to latex, or most editors should easily allow you to replace tabs by the right number of spaces, so the standard verbatim works. – David Carlisle Jul 01 '16 at 07:53

3 Answers3

10

You can use fancyvrb that respects TAB characters by default and allows to set the number of spaces they mean at runtime.

\documentclass{article}
\usepackage{showframe} % just for the example
\usepackage{fancyvrb}

\begin{document}

First attempt
\begin{verbatim}
        #####
      #########
    #############
\end{verbatim}

Second attempt:
\begin{Verbatim}
        #####
      #########
    #############
\end{Verbatim}

Third attempt:
\begin{Verbatim}[tabsize=4]
        #####
      #########
    #############
\end{Verbatim}

\end{document}

Note: each line in the three verbatim environments starts with a TAB, in my test file (TABs are normalized to four spaces by the site).

enter image description here

egreg
  • 1,121,712
2

I'm afraid your assertion that the whitespace to the left of the hash symbols is "removed, or normalized somehow" in a verbatim environment is incorrect, as the following example shows. (The framelines in the screenshot serve to denote the edges of the text block.)

enter image description here

\documentclass{article}
\usepackage{showframe}
\begin{document}
\noindent
\begin{verbatim}                                                                
         #####                                                              
       #########                                                            
     #############                                                       
#######################
\end{verbatim}
\end{document}
Mico
  • 506,678
2

Turns out the moreverb package is just for this kind of situation:

\usepackage{moreverb}
...

\begin{verbatimtab}
...
\end{verbatimtab}

From the documentation:

\begin{verbatimtab}[⟨tab width⟩] reproduces its body verbatim, 
with the tabs expanded to the given width (the default value is 8).
Charles
  • 133