3

How can I force \lstinputlisting[...]{filename} to obey all parameters set by \lstset? (Note the float option given to the TeX example, but not to the LaTeX example.)

\begin{filecontents*}{helloworld.tex.example}
Hello, World!
\bye
\end{filecontents*}

\begin{filecontents*}{helloworld.ltx.example}
\documentclass{article}
\usepackage[margin=1in]{geometry}

\begin{document}
  Hello, World!
\end{document}
\end{filecontents*}

%%%%%%%%%%%%%%%%%%%%%%%

\documentclass{article}

\usepackage{listings}
\lstset{float,frame=lines}

\usepackage{mwe}
\begin{document}
\lipsum[1]
\lstinputlisting[language = TeX, float, caption=A \TeX\ example]
                {helloworld.tex.example}
\lipsum[2]
\lstinputlisting[language = {[LaTeX]TeX}, caption=A \LaTeX\ example]
                {helloworld.ltx.example}
\lipsum[3]
\end{document}

output

5

The documentation says about float:

makes sense on individual displayed listings only and lets them float.

Thus it might be a feature, that option float cannot be set globally.

Nevertheless, the following workaround seems to work:

\usepackage{listings}
\lstset{float}
\makeatletter
\let\lst@floatdefault\lst@float
\makeatother
  • 1
    So basically, this is by design? How strange. Thanks for the workaround! – Sean Allred Aug 17 '13 at 20:21
  • I don't understand that either. It would make perfect sense for me to have all my listings floating. – rbs Aug 30 '18 at 16:52
  • @rbs That's a question for the package author. – Heiko Oberdiek Aug 30 '18 at 16:56

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.