作为一种解决方法,目前我正在使用框架式 Verbatim,但我希望
- 这组列表在列表列表中显示为单个列表
- 代码将
listings
根据我的lstset
设置由包呈现 - 行号显示在代码行的开头,但仅此而已,在每个单独的文件中从 1 重新开始
这可能吗?
我当前的代码:
\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}
其结果如下:
答案1
这Stack Overflow 帖子描述了一种基于该subfig
包的解决方案,但后者与通用hyperref
包存在兼容性问题(参见Stephan 的帖子),subcaption
建议使用该包来代替subfig
。
以下是我使用 尝试的解决方案subcaption
。它不是最佳的——缺乏自动化,列表和标题之间的垂直间距有点偏离——但它应该能让你达到一半的目标。
\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}