3

As a workaround currently I am using framed Verbatim, but I would like that

  • this set of listings appear as a single listing in the list of listings
  • the codes be rendered by the listings package according to my lstset settings
  • line numbers are shown at the beginning of code lines, but only there, restarting from 1 at each separate file

Is this somehow possible?

My current code:

\begin{figure}[h]
\centering\begin{tabular}{l}
\texttt{config.h} \\
\begin{minipage}{0.78\textwidth}
\begin{Verbatim}[frame=single]
// Choose one of the supported encoding options:
//#define STRING_ENCODING_ASCII
#define STRING_ENCODING_UCS32
//#define STRING_ENCODING_UTF8

#include "rules.h"
\end{Verbatim}
\end{minipage} \\ \vspace{0.01cm} \\
\texttt{rules.h} \\
\begin{minipage}{0.73\textwidth}
\begin{Verbatim}[frame=single]
#ifdef STRING_ENCODING_UCS32
   #define ENCODING_HEADER "encoding/ucs32.h"
#endif
\end{Verbatim}
\end{minipage} \\ \vspace{0.01cm} \\
\texttt{text.cpp} \\
\begin{minipage}{0.4\textwidth}
\begin{Verbatim}[frame=single]
#include "config.h"
#include ENCODING_HEADER
\end{Verbatim}
\end{minipage} \\
\end{tabular}
\caption{\label{fig:include_macro}The \code{\#include \emph{macropath}} pattern.}
\end{figure}

And its result: enter image description here

Moriambar
  • 11,466
  • related: http://tex.stackexchange.com/questions/63928/combining-subfloat-and-newfloat-packages-for-sublistings-environment – jub0bs Oct 20 '13 at 00:28
  • Do you need pagebreaks also or you are all right with the "floating" because of the figure environment? – masu Nov 04 '13 at 22:09
  • I am ok without pagebreaks at the moment. I cannot say I will never need them, but it is only a nice to have extra for me. – jmihalicza Nov 06 '13 at 07:46

1 Answers1

1

This Stack Overflow post describes a solution based on the subfig package, but the latter has compatibility problems with the ubiquitous hyperref package (see Stephan's post) and the subcaption package is recommended instead of subfig.

Below is my attempt at a solution using subcaption. It's not optimal---lacks automation and the vertical spacing between listing and caption is a bit off---but it should get you halfway there.

enter image description here

\documentclass{article}

% -----------------------------------------------
\usepackage{filecontents}
% I generate the source files here for completeness of the MWE.
% In practice, those three files should sit in some folder accessible to pdflatex.
\begin{filecontents*}{config.h}
// Choose one of the supported encoding options:
//#define STRING_ENCODING_ASCII
#define STRING_ENCODING_UCS32
//#define STRING_ENCODING_UTF8

#include "rules.h"
\end{filecontents*}


\begin{filecontents*}{rules.h}
#ifdef STRING_ENCODING_UCS32
   #define ENCODING_HEADER "encoding/ucs32.h"
#endif
\end{filecontents*}


\begin{filecontents*}{text.cpp}
#include "config.h"
#include ENCODING_HEADER
\end{filecontents*}
% -----------------------------------------------

\usepackage{kantlipsum} % for filler text

\usepackage{float}          % for defining a new float env. (multilisting)
    \newfloat{multilisting}{tbphH}{lopbl}   
    \floatname{multilisting}{Listing}

\usepackage{subcaption} % for defining a custom subfloat env. (submultilisting)
    \DeclareCaptionSubType{multilisting}
    \captionsetup[submultilisting]
    {
        font            = {tt,normalsize},
        justification   = raggedright,
        singlelinecheck = off,
    }

\usepackage{listings}   
    \lstset
    {
        language    = C++,
        numbers     = left,
        frame       = single,
        captionpos  = b,
    }

\usepackage{hyperref}

% new macro for a bit more automation
\newcommand\sublstinputlisting[2][1]
% 1: multiplying factor of \textwidth;
% 2: path to source file
{
    \begin{submultilisting}[b]{#1\textwidth}
        \lstset
        {
            title           = \lstname,
            captionpos  = t,        
        }
        \lstinputlisting{#2}%
    \end{submultilisting}%
}

% macro code undefined in your incomplete example, so I just coded my own
\newcommand\code[1]{\texttt{#1}}


\begin{document}

\lstlistoflistings

\vfill

\kant[1]
\begin{multilisting}
    \sublstinputlisting[0.85]{config.h}
    \par
    \sublstinputlisting[0.8]{rules.h}
    \par
    \sublstinputlisting[0.45]{text.cpp}
    \captionsetup{type=lstlisting}%
    \caption{The \code{\#include \emph{macropath}} pattern.}
    \label{my_cplusplus_listing}
\end{multilisting}

\begin{lstlisting}[caption = test listing]
test
test
\end{lstlisting}

\end{document}
jub0bs
  • 58,916