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:


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

    -- indented comment
    local this = "test"

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

Here is the result (the blue background is there to visualize the leading space): Result with 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
  • 1
    Yeah, this is about code documentation, so I definitely need some features of listings.
    – pschulz
    Aug 11, 2020 at 10:50

1 Answer 1


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.

\lstset{backgroundcolor = \color{blue}}
\lstinputlisting[firstline=2, lastline = 3,gobble=4]{xxx.lua} % It doesn't work.

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.
    – pschulz
    Aug 11, 2020 at 11:26
  • tlmgr have user mode. You can install to TEXMFHOME dir with user mode.
    – Tsubasa
    Aug 11, 2020 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .