# 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}
\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}