views:

1512

answers:

4

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

+1  A: 

Have you looked at the the float package. Just guessing.

dmckee
Just looked now. I don't think its what I'm looking for, but there's a chance it will help. Thanks.
Paul Biggar
OK, I can see how the float package might help. If I define my own float (rather than using "figure"}, and make it use the .lol extension (so that the captions are put in "list of listings", it might work. Or I could wrap all listings in a "myfloat" float, instead of using the "float" command, then use the same environment to put subfloats in. I can try that.
Paul Biggar
Ouch. That is more complicated than I'd hoped.
dmckee
Actually, not so bad. That's what I did in the end.
Paul Biggar
A: 

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.

Mica
A: 

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}
}
}
Paul Biggar
+1  A: 

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}
Paul Biggar