views:

51

answers:

2

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

section{section}

    bla bla bla bla bla

    subsection{a}

        bla bla bla bla bla

    subsection{b}

        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:

    subsection{a}

        bla bla bla bla bla
    \begin{lstlisting}
    public static void main(String [] args){
        bla bla
    }
    \end{lstlisting}

Produces:

*a*

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

I would like it to produce:

*a*

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).

    \usepackage{listings}
    \usepackage{color}

    \definecolor{dkgreen}{rgb}{0,0.6,0}
    \definecolor{gray}{rgb}{0.5,0.5,0.5}
    \definecolor{mauve}{rgb}{0.58,0,0.82}

    \lstset{frame=tb,
      language=Java,
      aboveskip=3mm,
      belowskip=3mm,
      showstringspaces=false,
      columns=flexible,
      basicstyle={\small\ttfamily},
      numbers=none,
      numberstyle=\tiny\color{gray},
      keywordstyle=\color{blue},
      commentstyle=\color{dkgreen},
      stringstyle=\color{mauve},
      breakatwhitespace=true
      tabsize=3
    }
+3  A: 

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

Use gobble-setting to remove characters from start.

Example

Remove two tabs (= 8 spaces) from start:

\begin{lstlisting}[gobble=8]
Cloudanger
Will this gobble 3 tabs and the next 5 characters, or will it guarantee to gobble 8 columns?
dreamlax
@dreamlax: At least in my LaTeX tabs convert to 8 spaces. So it will gobble 2 tabs.
Cloudanger
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
@sixty: LaTeXCalc http://latexcalc.sourceforge.net/ can do computations.
Cloudanger
A: 

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/

Herbert Sitz
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