Tuesday, 20 August 2013

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

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:

No comments:

Post a Comment