TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I write latex I usually indent my sections like this (it allows easier folding in vim, is like reading code, etc).


    bla bla bla bla bla


        bla bla bla bla bla


        bla bla bla bla bla

The problem is when I insert code (see below) into my document if I indent the all of the code 3 tabs it will be be indented 3 tabs in the document. I want to preserve the relative indenting but I don't want that extra three tabs of indentation.

For example this latex:


        bla bla bla bla bla
    public static void main(String [] args){
        bla bla



bla bla bla bla bla
            /* code is indented 3 tabs */
        public static void main(String [] args){
            bla bla

I would like it to produce:


bla bla bla bla bla
public static void main(String [] args){
    bla bla

This is the code that is responsible for the code formatting (thanks Cloudanger).



share|improve this question

migrated from stackoverflow.com Mar 14 '12 at 15:04

This question came from our site for professional and enthusiast programmers.

You may want to look here for automatic tab skipping. – Stefan Mar 14 '12 at 22:58
up vote 5 down vote accepted

Not a very good solution (automatical would be ideal), but this works:

Use gobble-setting to remove characters from start.


Remove two tabs (= 8 spaces) from start:

share|improve this answer
Will this gobble 3 tabs and the next 5 characters, or will it guarantee to gobble 8 columns? – dreamlax Aug 4 '10 at 21:56
@dreamlax: At least in my LaTeX tabs convert to 8 spaces. So it will gobble 2 tabs. – Cloudanger Aug 4 '10 at 22:01
Wow! That is awesome! Thanks so much. Is there anyway that I can do something like \begin{lstlisting}[gobble=a*b] where a could be the number of spaces/tab (which I assume could change, probably it is set to an env variable) and b is the number of tabs to enter. This would a little bit cleaner.... If I don't get an "automatic" answer in the next few days I will marks yours as accepted. – sixtyfootersdude Aug 5 '10 at 17:49
@sixty: LaTeXCalc latexcalc.sourceforge.net can do computations. – Cloudanger Aug 5 '10 at 18:51

What do you mean by "it allows easier folding in Vim"? Are you writing LaTeX and using indents to get your folding in Vim? That seems like less than optimal approach.

A better approach would be to use the Vim LaTeX-Suite plugin, which gives you automatic folding of sections regardless of how they're indented. Plus it has a lot of other benefits. http://vim-latex.sourceforge.net/

share|improve this answer
Actually yeah I am using that (so it takes care of folding) but when I edit it on another computer (without latex-suite) it is nice to still have folding. Reading it is also easier because it is more codey. – sixtyfootersdude Aug 5 '10 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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