1

I have the following code:

{
    \lstset{language=C,
            basicstyle=\tiny,
            tabsize=3,
            frame=lines,
            breaklines=true,
            breakatwhitespace=true}

    \lstinputlisting[float,caption={Caption1},label=txt:Netlink,lastline=69]{file.c}
}

{
    \lstset{language=C,
            basicstyle=\tiny,
            tabsize=3,
            frame=lines,
            breaklines=true,
            breakatwhitespace=true}
\ContinuedFloat
    \lstinputlisting[float,caption={Caption 1 (cont.)},label=txt:Netlink,firstline=71]{code.c}
}

It produces something like:

        Listing 1.1: Caption 1
The code 

        Listing 1.2: Caption 1 (cont.)
The code 

I want it to be like if you use \Continuedfloat

        Listing 1.1: Caption 1
The code 

        Listing 1.1: Caption 1 (cont.)
The code 

And then on list of listings I would like it to be shown just once.

I have no idea on how to do it. Any help is more than welcome.

Edit 1: (for @GonzaloMedina)

This is the result including \lstlistoflistings output

Would it be possible to have only one entry there?

  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Feb 5 '14 at 13:04
1

I defined two commands \savecount and \contcap. After the first part of the code, invoke \savecont (this simply stores the current value of the lstlisting counter). For the continued parts, inside a group invoke \contcap (this produces the appropriate counter value and uses a special caption format to produce the desired "(Cont.)" text):

\documentclass{article}
\usepackage{listings,caption}

\DeclareCaptionFormat{cont}{#1~(cont.)#2#3 }

\newcounter{tmp}

\newcommand\savecount{\setcounter{tmp}{\value{lstlisting}}}
\newcommand\contcap{%
\setcounter{lstlisting}{\value{tmp}}
\addtocounter{lstlisting}{-1}
\captionsetup[lstlisting]{format=cont}}

 \lstset{language=C,
            basicstyle=\tiny,
            tabsize=3,
            frame=lines,
            breaklines=true,
            breakatwhitespace=true
}

\begin{document}
\lstlistoflistings

\lstinputlisting[float,caption={the first part of a listing},label=txta:Netlink,lastline=2]{code.c}
\savecount

\begingroup
\contcap
\lstinputlisting[float,nolol,caption={the second part of a listing},label=txtb:Netlink,firstline=3,lastline=5]{code.c}
\endgroup

\begingroup
\contcap
\lstinputlisting[float,nolol,caption={the third part of a listing},label=txtc:Netlink,firstline=6]{code.c}
\endgroup

\end{document}

To prevent multiple entries in an eventual List of listings, use the nolol key for the continuations.

  • Main problem is that your solution repeats the entry many times when \lstlistoflistings (see edit 1 in the question) – jlanza Feb 5 '14 at 15:35
  • @jlanza that's no problem, use the nolol key in the continuations. See my updated answer. – Gonzalo Medina Feb 5 '14 at 15:37
  • and the issue of only having 1 and not listing 1 in the list of listings? Do you know why is that? – jlanza Feb 5 '14 at 19:01
  • @jlanza that's the default behaviour. If you want to add the word "Listing" in the list, you'll need to add something like \makeatletter \def\l@lstlisting#1#2{\@dottedtocline{1}{0em}{1em}{\lstlistingname~#1}{#2}} \makeatother (perhaps adjusting the lengths according to your needs) to the preamble. – Gonzalo Medina Feb 5 '14 at 19:25

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.