GeneralHow to join lof and lol?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bmandl
Posts: 2
Joined: Mon Aug 22, 2016 8:16 pm

How to join lof and lol?

Post by bmandl »

Hello latex gurus. 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:

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to join lof and lol?

Post by Johannes_B »

Welcome,

a minimal working example to show how you are producing your listings, captions and list of figures is needed in order to reproduce your specific scenario.

By the way, you should treat listings as listings, not figures. After all, they are not figures :-)

EDIT: Crosspost
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
BlackForestrian

How to join lof and lol?

Post by BlackForestrian »

I agree with Johannes that listings should not be included in the LoF at all, but here are two ways to do so:


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!

Another way is to redefine \addcontentsline and check whether the first argument is a `lol` (then use `lof` instead) or fall back to the normal behaviour of \addcontentsline.

Code: Select all

\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}
2nd version:

Code: Select all

\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\@temp@a{#1}
  \def\@temp@b{lol}
  \ifx\@temp@a\@temp@b
  \latex@@addcontentsline{lof}{#2}{#3}%
  \else
  \latex@@addcontentsline{#1}{#2}{#3}%
  \fi
}

\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}
Attachments
listoffiguresandlistings.png
listoffiguresandlistings.png (9.72 KiB) Viewed 6910 times
Last edited by cgnieder on Wed Sep 14, 2016 8:13 pm, edited 1 time in total.
bmandl
Posts: 2
Joined: Mon Aug 22, 2016 8:16 pm

Re: How to join lof and lol?

Post by bmandl »

Thank you all for replies. Unfortunately, above code doesn't do nothing in my case, so I decided to make separate list of listings. I am curious anyways :-)
BlackForestrian

How to join lof and lol?

Post by BlackForestrian »

bmandl wrote:Thank you all for replies. Unfortunately, above code doesn't do nothing in my case, so I decided to make separate list of listings. I am curious anyways :-)
What does this mean? Both versions of the code works for standard setups. Since you have not provided really much, you seem to do strange things instead.
Post Reply