5

The question

Is it possible to configure the listings package to show the content in the lstlisting environment even if the global option draft is set?

Context

When working on a larger document, the draft option is useful to avoid compiling things like the table of contents, the glossary maybe even placeholder images for larger graphics every single time and hence allow for faster iteration on the document.

However, it seems that this option it also prompts the listings package to avoid showing the "content" of listings - while the captions are typeset correctly (try MWE with and without draft option further down, which includes all other packages my actual document uses).

I have checked the package documentation but it seems to me this is not a package-specific thing, but rather my ignorance of \LaTeX conventions getting in my way.

Minimal working example

%\documentclass[]{book}
\documentclass[draft]{book}

\usepackage[numbers,sort&compress]{natbib}
\usepackage[protrusion=true,expansion,babel=true]{microtype}
\usepackage{mathtools} % This includes "amsmath" and the dcases environment among other things
\usepackage{dsfont}
\usepackage{tikz}
\usepackage{listings}
\usepackage{hyperref}
%\usepackage[toc]{glossaries}
%% ---------------- end of usepackages -------------
\begin{document}[caption=This will always be visible]
    \lstset{language=Java}
    // This is only visible when compiling without the draft option
    \begin{lstlisting}

        public class MyTest{
            int foo;
        }
    \end{lstlisting}

\end{document}
10

The listings package provides an option called final that you can use for that. See section 2.2 of the listings doc:

final

Overwrites [sic] a global draft option.

Simply pass the final option to listings when you load the package:

\documentclass[draft]{book}

\usepackage[numbers,sort&compress]{natbib}
\usepackage[protrusion=true,expansion,babel=true]{microtype}
\usepackage{mathtools} % This includes "amsmath" and the dcases environment among other things
\usepackage{dsfont}
\usepackage{tikz}
\usepackage[final]{listings}% <=====
\usepackage{hyperref}
%\usepackage[toc]{glossaries}
%% ---------------- end of usepackages -------------
\begin{document}[caption=This will always be visible]
    \lstset{language=Java}

    \begin{lstlisting}
        public class MyTest{
            int foo;
        }
    \end{lstlisting}

\end{document}
2

I tried Arash's answer but got a ! LaTeX Error: Option clash for package listings when I called \lstdefinestyle. Instead, I added the print option to my style:

\lstdefinestyle{py}{
  language=Python,
  print   <============
}

That causes any listing with style=py to be included, even when draft is set. The docs, sec. 5.2, say that print "can be used to typeset particular listings despite using the draft option."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.