4

This is my first post on this forum and I am turning on you for help with advanced problem. I am writing my thesis and I am using listings package for including code snippets. However, I don't use float environment with every snippet, because some of them needs more than 1 page. So I can't include them in figure environment. But I want code snippets to act as figures, so I set them to share counter and name with figures:

\renewcommand\lstlistingname{Slika}
\let\c@lstlisting\c@figure
\let\thelstlisting\thefigure
\let\ftype@lstlisting\ftype@figure

Now I have trouble adding them to list of figures just as they were figures. Numbering, captioning, referencing and everything works just as I want, but I can't add listings to list of figures. I tryed to google the answer, but it seems that no one before had this specific problem. I don't have enough knowledge with LaTeX to write any scripts or anything like that, so I ask you guys for help. Thank you in advance.

4
  • welcome to tex.sx. sorry i can't offer any technical expertise on this, only commiseration. Aug 22 '16 at 18:33
  • A list of figures or tables is actually a list of captions. See \captionof from the caption or captionof packages. Aug 23 '16 at 4:04
  • @JohnKormylo: Yes, but listings uses a caption option in a key value interface. I don't think it's very easy to listings's caption mechanism to work with \captionof
    – user31729
    Aug 23 '16 at 4:20
  • Crosspost
    – Johannes_B
    Aug 23 '16 at 4:58
3

Update See a shorter version without using a patch at the end the answer.

Some note before: A listing is a listing and not a figure, so merging listings and figures in the LoF is perhaps not the best way to apply those different concepts -- that's why there is a \lstlistoflistings command ;-)

listings uses \addcontentsline{lol}{lstlistings}{...} and this is hard-coded in \lst@MakeCaption. Either copy the code from listings.sty and change the settings or use a patch, that changes the relevant portions. In a normal setup, the 2nd \addcontentsline{lol}{lstlistings}{...} command is active.

Now, in order to prevent strange differences between a normal lof - line and a lol line, I suggest to use \let\l@lstlisting\l@figure as well!

\documentclass{article}

\usepackage{xpatch}
\usepackage{listings}


\makeatletter
\AtBeginDocument{%

\xpatchcmd{\lst@MakeCaption}{%
  \lst@ifnolol\else
  \ifx\lst@@caption\@empty
  \ifx\lst@caption\@empty
  \ifx\lst@intname\@empty \else \def\lst@temp{ }%
  \ifx\lst@intname\lst@temp \else
  \addcontentsline{lol}{lstlisting}\lst@name
  \fi\fi
  \fi
  \else
  \addcontentsline{lol}{lstlisting}%
  {\protect\numberline{\thelstlisting}\lst@@caption}%
  \fi
  \fi
}{%
  \lst@ifnolol\else
  \ifx\lst@@caption\@empty
  \ifx\lst@caption\@empty
  \ifx\lst@intname\@empty \else \def\lst@temp{ }%
  \ifx\lst@intname\lst@temp \else
  \addcontentsline{lof}{lstlisting}\lst@name
  \fi\fi
  \fi
  \else
  \addcontentsline{lof}{lstlisting}%
  {\protect\numberline{\thelstlisting}\lst@@caption}%
  \fi
  \fi
}{\typeout{Patch success!}}{\typeout{Patch failure}}


\renewcommand\lstlistingname{Slika}
\let\l@lstlisting\l@figure
\let\c@lstlisting\c@figure
\let\thelstlisting\thefigure
\let\ftype@lstlisting\ftype@figure
}
\makeatother


\begin{document}
\listoffigures

\begin{figure}
\caption{Foo figure}
\end{figure}

\begin{lstlisting}[language={C},caption={Hello World},label={foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}


\end{lstlisting}
\end{document}

A shorter version without patching

\documentclass{article}

\usepackage{listings}

\usepackage{hyperref}
\makeatletter
% Grab the old \addcontentsline, which has been already being redefined by hyperref (eventually)
\let\latex@@addcontentsline\addcontentsline 

\AtBeginDocument{%
    \renewcommand{\addcontentsline}[3]{%
  \def\@@zzz{#1}\def\@@zxx{lol}
  \latex@@addcontentsline{%
    \ifx\@@zzz\@@zxx lof\else #1\fi
  }{#2}{#3}%
}

\renewcommand\lstlistingname{Slika}
\let\l@lstlisting\l@figure
\let\c@lstlisting\c@figure
\let\thelstlisting\thefigure
\let\ftype@lstlisting\ftype@figure
}
\makeatother


\begin{document}
\listoffigures
\clearpage
\begin{figure}
\caption{Foo figure}
\end{figure}

\clearpage
\begin{lstlisting}[language={C},caption={Hello World},label={foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}


\end{lstlisting}
\end{document}

enter image description here

7
  • Would you mind answering the linked crosspost above as well?
    – Johannes_B
    Aug 23 '16 at 4:59
  • @Johannes_B: Done so ...
    – user31729
    Aug 23 '16 at 5:08
  • 1
    @ChristianHupfer: You can shorten your redefinition using \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{\ifnum\pdfstrcmp{#1}{lol}=0 lof\else#1\fi}{#2}{#3}}.
    – Werner
    Aug 23 '16 at 5:32
  • @Werner: That's shorter indeed, but won't compile if the user uses latex 'only' (ok, that's rather unlikely nowadays), I think.
    – user31729
    Aug 23 '16 at 10:38
  • @ChristianHupfer: \pdfstrcmp is not just for pdflatex. Try it with latex only...
    – Werner
    Aug 23 '16 at 15:30

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.