9

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?

3
  • 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. Jul 1, 2016 at 7:46
  • Ah, you're right, they are tabs (at least partially). Maybe I could set a tab length defined in spaces?
    – Charles
    Jul 1, 2016 at 7: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. Jul 1, 2016 at 7:53

3 Answers 3

9

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

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}
5
  • try that with tabs not spaces:-) Jul 1, 2016 at 7:52
  • 1
    @DavidCarlisle - The SE site software seems to replace tabs with spaces automatically. :-) Maybe that's why I can't reproduce the OP's issue...
    – Mico
    Jul 1, 2016 at 7:53
  • Correct! I didn't notice that the spaces were actually tabs when copying from the terminal. I am actually generating the LaTeX with org-mode in emacs and I think I still haven't sorted out the generation correctly.
    – Charles
    Jul 1, 2016 at 7:54
  • 4
    @Charles yo emacs! the editor of choice, just use M-x untabify on your source file! Jul 1, 2016 at 7:55
  • Thanks, that does work. I also posted a solution in LaTeX.
    – Charles
    Jul 1, 2016 at 8:11
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.