When including listings with \lstinputlisting, spaces (or tabs) used for indentation of the code are kept, which is of course a good thing. But I want to document code line-by-line, so sometimes the context of the indentation gets lost, making it unnecessary. For long lines, this space could be used better. Is there an option to gobble spaces for input listings (the manual states that this feature is ignored for \lstinputlisting).

Here is a MWE:

\documentclass{scrartcl}

\usepackage{listings}
\usepackage{xcolor}
\lstset{backgroundcolor = \color{blue!10!white}} % show leading space

\begin{filecontents*}{code.lua}
function()
-- indented comment
local this = "test"
end
\end{filecontents*}

\begin{document}
\lstinputlisting[firstline=2, lastline = 3]{code.lua}
\end{document}


Here is the result (the blue background is there to visualize the leading space): I would like to remove the space on the left.

• Do you require syntax highlighting features of listings or are you looking for a straight verbatim result? Aug 11, 2020 at 10:38
• Yeah, this is about code documentation, so I definitely need some features of listings. Aug 11, 2020 at 10:50

See How to gobble characters section in listings document. You can use gobble=4 option to remove leading 4 characters but it doesn't work with \lstinputlisting.

Consider using minted package which can process more languages. You can write like this.

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstset{backgroundcolor = \color{blue}}
\usepackage{minted}
\begin{document}
\lstinputlisting[firstline=2, lastline = 3,gobble=4]{xxx.lua} % It doesn't work.
\begin{lstlisting}[gobble=4]
aaa
\end{lstlisting}
\inputminted[firstline=2,lastline=3,autogobble]{lua}{xxx.lua}
\end{document}


Note: minted package requires -shell-escape option and Python installed.

• Thanks, then I will shift to minted. Do you know if there is a way to install styles locally? I can only find stuff about putting it in the distribution directory in the documentation. Aug 11, 2020 at 11:26
• tlmgr have user mode. You can install to TEXMFHOME dir with user mode. Aug 11, 2020 at 11:57