6

I have been looking everywhere and I found the ways of doing this with images with \usepackage{float} for \includegraphics. But I also have texts from .txt files that I have as in \lstinputlisting that I would also like to force.

I don't really understand the documentation of how placement works.

5.1 Space and placement

float=[*] <subset of tbphi> or float   floatplacement
makes sense on individual displayed listings only and lets them float. The argument controls where LATEX is allowed to put the float: at the top or bottom of the current/next page, on a separate page, or here where the listing is. The optional star can be used to get a double-column float in a two-column document

floatplacement=<place specifiers>   tbp
is used as place specifier if float is used without value.

Any ideas how I force the code below with \lstinputlisting?

\lstinputlisting[float=h,frame=tb,
    caption=Iteration: 2. Variable importance for unpruned decision tree ,captionpos=b, belowcaptionskip=2cm ,label=zebra]{results/maybe_80_20.txt}
3
  • Probably you are using the H specifier for figures. Why don't you use it for listings? But please consider that you really disable all of the advantages and the concept of floats.
    – TeXnician
    May 10, 2017 at 4:23
  • 1
    @TeXnician: This is not a duplicate. E.g. \lstinputlisting[float=H,… will not work. See my note about this in my answer below. May 10, 2017 at 6:30
  • @Schweinebacke Thanks for the information. Voted for reopening too.
    – TeXnician
    May 10, 2017 at 7:32

1 Answer 1

10

Package listings supports captions for not floating listings. See the initial paragraph in section 5.7, "Captions" in the listings manual:

In despite of LaTeX standard behaviour, captions and floats are independent from each other here; you can use captions with non-floating listings.

So you can simple remove the float option from your example and use, e.g.:

\documentclass{article}
\usepackage{listings}
\usepackage{mwe}

\begin{document}
\blindtext

\lstinputlisting[frame=tb,caption=Source of this file,
  captionpos=b,belowcaptionskip=2cm,
  label=zebra]{\jobname.tex}

\blindtext
\end{document}

to get

enter image description here

You have to use option float only for those listings, that should float.

Note: listings also allows page breaks inside non-floating listings with captions! So you have to use either a \parbox or minipage around them, if page breaks should not be allowed:

\documentclass{article}
\usepackage{listings}
\usepackage{mwe}

\begin{document}
\blindtext[3]

\noindent\begin{minipage}{\linewidth}
\lstinputlisting[frame=tb,caption=Source of this file,
  captionpos=b,belowcaptionskip=2cm,
  label=zebra]{\jobname.tex}
\end{minipage}

\blindtext
\end{document}

results in:

good result

while

\documentclass{article}
\usepackage{listings}
\usepackage{mwe}

\begin{document}
\blindtext[3]

%\noindent\begin{minipage}{\linewidth}
\lstinputlisting[frame=tb,caption=Source of this file,
  captionpos=b,belowcaptionskip=2cm,
  label=zebra]{\jobname.tex}
%\end{minipage}

\blindtext
\end{document}

results in:

not so good restult

BTW: Option H of package float cannot be used with \lstinputlisting:

% CAVEAT: THIS WILL NOT WORK! DON'T DO IT!!!
\documentclass{article}
\usepackage{float}
\usepackage{listings}
\usepackage{mwe}

\begin{document}
\blindtext

\lstinputlisting[float=H,frame=tb,
  caption=Source of this file,
  captionpos=b,belowcaptionskip=2cm,
  label=zebra]{\jobname.tex}

\blindtext
\end{document}

would result

the listing is missing

As you can see, the listing is missing (without any error message). So: Don't do this!

You must log in to answer this question.

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