1

I am using the listings package inside my document, and to use a footnote inside the caption of a listing I had to separate the \caption from the \lstinputlisting.

If I create the document now, the listing with the separate caption seems to follow a different numbering than the other ones - while the surrounding listings are "Listing 5.6" and "Listing 5.7" the one with the separate caption is called "Listing 1".

Here's the code I am working with (I dumbed it down a bit, still the exact same structure as in the document though):

% This listing is working just fine (called Listing 5.6 in the document).
\begin{lstfloat}
    \lstinputlisting[label=code:abort,caption={Workin caption here},captionpos=b]{Inhalt/Code/abort.js}
\end{lstfloat}

Here is some text to talk about the listing...

% This listing is suddenly called Listing 1
\begin{lstfloat}
    \lstinputlisting[label=code:shell]{Inhalt/Code/shell.sh}
    \caption[Manipulation und Abfrage der Daten mithilfe von cURL]{Manipulation und Abfrage der Daten mithilfe von cURL\protect\footnotemark}
\end{lstfloat}

\footnotetext{\url{https://curl.haxx.se/}}

% This listing is working file as well (called Listing 5.7 in the document)
\begin{lstfloat}
    \lstinputlisting[label=code:connect,caption={Another working caption},captionpos=b]{Inhalt/Code/connect.js}
\end{lstfloat}

I am no LaTeX expert and I have no idea what is going wrong.

  • Floating the footnote text was the only solution I found online to handle the footnote inside the caption in the first place - and it does end up on the previous page. Apart from the numbering issue, do you have an idea on how to fix that?

    EDIT: I will try out putting the label below the caption and come back to you

    EDIT: The issue persists. Putting the label below the caption is not the correct solution.

    – Thomas Pötzsch Sep 12 '18 at 09:47
  • What do you mean by that? Just ommitting the footnote inside the caption? That does not seem like a constructive solution, tbh. – Thomas Pötzsch Sep 12 '18 at 11:23
  • Yes. And I do not want to discuss this. I am searching for a solution of the problem, not for advice on how to write a document. – Thomas Pötzsch Sep 12 '18 at 11:37

1 Answers1

1

You should read the documentation of listings.

I guess that lstfloat has been taken from https://tex.stackexchange.com/a/354023/4427

\begin{filecontents*}{\jobname.sh}
echo "Hello World"
\end{filecontents*}

\documentclass{article}

\usepackage{listings}
\usepackage{url}
\usepackage{float}

\newfloat{lstfloat}{htbp}{lop}
\floatname{lstfloat}{Listing}
\newcommand{\mylistoflistings}{\listof{lstfloat}{List of Listings}}
\AtBeginDocument{\counterwithin{lstlisting}{section}}

\begin{document}

\section{Test}

\begin{lstfloat}[!hp]
    \lstinputlisting[label=code:abort,caption={Workin caption here},captionpos=b]{\jobname.sh}
\end{lstfloat}

Here is some text to talk about the listing...

\begin{lstfloat}[!hp]
    \lstinputlisting[
      label=code:shell,
      captionpos=b,
      caption={%
       [Manipulation und Abfrage der Daten mithilfe von cURL]%
       {Manipulation und Abfrage der Daten mithilfe von cURL\protect\footnotemark}},
    ]{\jobname.sh}
\end{lstfloat}

\footnotetext{\url{https://curl.haxx.se/}}

Here is some text to talk about the listing...

\begin{lstfloat}[!hp]
    \lstinputlisting[label=code:connect,caption={Another working caption},captionpos=b]{\jobname.sh}
\end{lstfloat}

\end{document}

enter image description here

egreg
  • 1,121,712
  • You already proposed this solution in the comments you provided above and then deleted after I told you that this is not the solution. I do not know how to make it more clear besides telling you a third time: This does not work. – Thomas Pötzsch Sep 12 '18 at 13:35
  • @ThomasPötzsch I assumed you defined lstfloat in a correct way; apparently you don't. Anyway, there is no need to specify separately the \caption. The current version does work, but I had to guess something about lstfloat. – egreg Sep 12 '18 at 13:57