Question

I have a macro that do side-by-side figures, below. It uses subfloats, and so it goes in a figure.

\newcommand{\listbylist}[6][showlines=true]{
   \begin{figure}
      \subfloat[ ]{
        \lstinputlisting[showlines=true,#1]{#2}                                                  
     \label{#4:A}
  }
  \hfill{}
  \subfloat[ ]{                                                                               
     % by setting the frame to leftline, we avoid a box into oblivion                         
     % turn off numbers                                                                       
     \lstinputlisting[showlines=true,frame=leftline,#1,numbers=none]{#3}                      
     \label{#4:B}                                                                             
  }                                                                                           
  \hfill{}
   \caption[#5]{#6}
       \label{#4}                                                                                         
   \end{figure}
}

Unfortunately, this uses the Figure counters, not the Listings ones. It also shows up in the wrong table of contents, and uses the word "Figure" instead of "Listings" in the caption, references to it, etc. Is there a way to rectify this?

I would prefer a simple way, like adding the word "Listing" somewhere...

Was it helpful?

Solution

Instead of using the built-in float of lstlistings, wrap them in a custom float:

\begin{mylisting}
\begin{lstlisting}
int x = 1;
\end{lstlisting}
\end{mylisting}

Then use the same float (mylisting) for the subfloat usage:

\newcommand{\listbylist}[6][showlines=true]{
  \begin{mylisting}
    \subfloat[ ]{
      ...
    }
    \hfill{}
    \subfloat[ ]{
      ...
    }
    \hfill{}
    \caption[#5]{#6}
    \label{#4}
  \end{mylisting}
}

This needs to all be set up in the preamble:

\newfloat{mylisting}{tbphH}{lopbl}[chapter]
\floatname{mylisting}{Listing}
\newsubfloat{mylisting}
\newcommand{\listofmylistings}{\listof{mylisting}{List of Listings}}
% if you use the hyperref package
\providecommand*\theHmylisting{\themylisting}

OTHER TIPS

You might want to have a look at the subfloat documentation. I'm sure there is a macro call that makes subfloat count in the "figure" environment. You could try and redefine the "figure" environment counter to "listings" -- if that makes any sense at all.

OK, this is the wrong answer, but I did almost get there like this. It only failed to add captions to the right \listof.

Nearly there. It could probably be done better, but this almost works. All that's left it is to make the caption appear in the .lol file, not the .loc file. I'll ask a question about that, then fix this answer.

Basically, this just backs up the "figure" counter, and copies over the "listings" counter. After the figure, it puts them back.

% Need a counter to save the value to
\newcounter{pbsavefigurecounter}

\newcommand{\listbylist}[6][showlines=true]{
{% scope

   % Change ``Figure'' to ``Listing''
   \renewcommand{\figurename}{Listing}

   % save the figure counter
   \setcounter{pbsavefigurecounter}{\value{figure}}

   % copy the listings counter to the figure counter
   \setcounter{figure}{\value{lstlisting}}


   \begin{figure}
  \subfloat[ ]{
     \lstinputlisting[nolol,showlines=true,#1]{#2}
     \label{#4:A}
  }
  \hfill{}
  \subfloat[ ]{
     % by setting the frame to leftline, we avoid a box into oblivion
     % turn off numbers
     \lstinputlisting[nolol,showlines=true,frame=leftline,#1,numbers=none]{#3}
     \label{#4:B}
  }
  \hfill{}

%  \float@caption{lol}[#5]{#6}
   \label{#4}
   \end{figure}

   % Update the listings counter
   \setcounter{lstlisting}{\value{figure}}

   % Restore the figure counter
   \setcounter{figure}{\value{pbsavefigurecounter}}

   % Change ``Listing'' back to ``Figure''
   \renewcommand{\figurename}{Figure}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top