4

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

1 Answer 1

6

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
3
  • 2
    So basically, this is by design? How strange. Thanks for the workaround! Aug 17, 2013 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, 2018 at 16:52
  • @rbs That's a question for the package author. Aug 30, 2018 at 16:56

You must log in to answer this question.

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