listings: Set tabsize while using spaces for indentation in the loaded file does not work

I am writing a report where I want to include some source code. To do so I use the listings package to load the content of the source files directly.

Because of coding standards I am using 4 spaces for indentation. Using 4 spaces in a document is a bit much, so I used the option tabsize=2 in the preamble. After a bit of testing I figured out that this setting only affect the size of 'real' tabs.

Is there a way to easily change the the indentation size to 2 spaces while leaving the code untouched? The only solution that I can think of is to make a copy of all source files replacing 4 spaces with either 2 spaces or a single 'real' tab.

Here is a (M)WE:

\documentclass[10pt,a4paper,oneside]{article}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[T1]{fontenc} %use different encoding (copy from pdf is now possible}
\usepackage{fullpage} %small margins
\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\usepackage{listings} %sourcecode
\lstset{
numbers=left,
breaklines=true,
backgroundcolor=\color{light-gray},
tabsize=2,
basicstyle=\ttfamily,
}

\begin{document}
\section{With 4 leading spaces}
\lstinputlisting[tabsize=2]{code1.mcf}

\section{With real' tabs}
\lstinputlisting[tabsize=2]{code2.mcf}
\end{document}


Where the content of the file code1.mcf is (4 spaces indented):

[
true*.
foo(false).
!(foo(true))*.
bar
]false


The content of code2.mcf uses real 'tabs' in stead of the 4 spaces. The output that is produced by pdflatex is shown below:

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  karlkoeller Aug 20 '13 at 22:15

The solution is to add the option literate={\ \ }{{\ }}1 in your \lstset.

This way you are declaring to substitute each occurrence of two spaces with one space, and you don't have the need to modify your files.

MWE:

\documentclass[10pt,a4paper,oneside]{article}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[T1]{fontenc} %use different encoding (copy from pdf is now possible}
\usepackage{fullpage} %small margins
\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\usepackage{listings} %sourcecode
\lstset{
numbers=left,
breaklines=true,
backgroundcolor=\color{light-gray},
tabsize=2,
basicstyle=\ttfamily,
literate={\ \ }{{\ }}1
}

\begin{document}
\section{With 4 leading spaces}
\lstinputlisting{code1.mcf}

\section{With real' tabs}
\lstinputlisting{code2.mcf}
\end{document}


Output:

-
Sorry for the late reply. Thanks for your reply! This indeed solves my problem, but if you are using spaces for alignment this might not work. The first code block on this website shows exactly what I mean: http://www.emacswiki.org/SmartTabs I'll accept your answer since it works fine in my case. –  Jeroen Aug 21 '13 at 12:40
@Jeroen Unfortunately I don't know a method to distinguish whether such a group has to be considered an alignement or an indentation... It's definitely better to deal with these things through the "search and replace" feature of your editor, I think. –  karlkoeller Aug 22 '13 at 5:24
@ karlkoeller: I guess that the best option in that case would be to write a small script that takes the content of the sourcefiles and replaces only leading tabs and generetes altered sourcefiles that can be included by latex. Although this might not work in all cases. Nevertheless my problem is solved! Thanks for the help –  Jeroen Aug 22 '13 at 8:03