在一个框中列出多个列表,并带有子标题和使用列表计数器

在一个框中列出多个列表,并带有子标题和使用列表计数器

我对(图形)图形和列表使用单独的计数器。我使用captionsubcaption包。我使用构建子图subfigure

\begin{figure}
\begin{subfigure}{0.34\textwidth}
...
\caption{sub-1}\label{fig:sub1}
\end{subfigure}
\begin{subfigure}{0.34\textwidth}
...
\caption{sub-2}\label{fig:sub2}
\end{subfigure}
\end{figure}

现在我需要同样的结构来列出列表。比如

\newsavebox{\verbsavebox}

\begin{figure}
\begin{lrbox}{\verbsavebox}
\begin{minipage}{.34\textwidth}
\begin{Verbatim}
...
\end{Verbatim}
\end{minipage}
\end{lrbox}%
\begin{subfigure}{0.34\textwidth}
\usebox{\verbsavebox}
\caption{sub-1}\label{lst:sub1}
\end{subfigure}
\begin{lrbox}{\verbsavebox}
\begin{minipage}{.34\textwidth}
\begin{Verbatim}
...
\end{Verbatim}
\end{minipage}
\end{lrbox}%
\begin{subfigure}{0.34\textwidth}
\usebox{\verbsavebox}
\caption{sub-2}\label{lst:sub2}
\end{subfigure}
\end{figure}

问题是,这使用了图形计数器,主标题名为“图...”。我想要的是使用列表计数器,并将主标题命名为“列表...”。


编辑这是另一次尝试使用lstlisting

\documentclass{article}
\usepackage{verbatimbox}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage[scaled=.73]{beramono}
\fvset{baselinestretch=0.94}
\parindent=0pt
\parskip = 6pt

% "define" Scala
\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield,then}, % we use `then` in the pseudocode
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

% activate the language and predefine settings
\lstset{
    basicstyle=\linespread{0.94}\ttfamily,%
    language=scala,%
    commentstyle=\itshape,%
    keywordstyle=\bfseries,%
    fancyvrb=true,%
    mathescape=true,%  for pseudocode
    captionpos=b, % captions at the bottom
}

\begin{document}

In Fig.~\ref{fig:label} versus Listings~\ref{lst:label1} and \ref{lst:label2}.

\begin{figure}
Empty
\caption{fig}\label{fig:label}
\end{figure}

\begin{figure} % [htp]
\begin{lstlisting}{caption={list1},label={lst:label1}}
trait Sys[S <: Sys[S]] {
  type ID <: Ident[S#Tx]
  type Tx
}

trait Muta[S <: Sys[S]] {
  def id: S#ID
  // well defined:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}

trait Ident[Tx] {
  def dispose(tx: Tx): Unit
}
\end{lstlisting}
\begin{lstlisting}{caption={list2},label={lst:label2}}
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  def id: S#ID   
  // incompatible type:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\begin{lstlisting}{caption={list3},label={lst:label3}}
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  // evidence required:
  val s: S
  def id: s.ID
  // well defined:
  def dispose(tx: s.Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\caption{Total caption}\label{lst:total}
\end{figure}

\end{document}

问题是,列表框的编号为“图 2”而不是“列表 1”,并且没有子标题 (a) (b) (c)。文本中的引用也已损坏。并且三个列表应该并排显示在三列中。因此,上面的代码如下所示:

在此处输入图片描述

这是一个模型,它应该看:

在此处输入图片描述


编辑2

好吧,我编译 paracol.sty并尝试过:

\documentclass{article}
\usepackage{verbatimbox}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage[scaled=.73]{beramono}
\fvset{baselinestretch=0.94}
\parindent=0pt
\parskip = 6pt
\usepackage{caption}

% "define" Scala
\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield,then}, % we use `then` in the pseudocode
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

% activate the language and predefine settings
\lstset{
    basicstyle=\linespread{0.94}\ttfamily,%
    language=scala,%
    commentstyle=\itshape,%
    keywordstyle=\bfseries,%
    fancyvrb=true,%
    mathescape=true,%  for pseudocode
    captionpos=b, % captions at the bottom
}

\usepackage{capt-of}
\usepackage{paracol}

\begin{document}

In Fig.~\ref{fig:label} versus Listings~\ref{lst:label1} and \ref{lst:label2}.

\begin{paracol}{3}
\begin{lstlisting}[caption={list1},label={lst:label1}]
trait Sys[S <: Sys[S]] {
  type ID <: Ident[S#Tx]
  type Tx
}

trait Muta[S <: Sys[S]] {
  def id: S#ID
  // well defined:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}

trait Ident[Tx] {
  def dispose(tx: Tx): Unit
}
\end{lstlisting}
\switchcolumn
\begin{lstlisting}[caption={list2},label={lst:label2}]
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  def id: S#ID   
  // incompatible type:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\switchcolumn
\begin{lstlisting}[caption={list3},label={lst:label3}]
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  // evidence required:
  val s: S
  def id: s.ID
  // well defined:
  def dispose(tx: s.Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\end{paracol}

\begin{figure}
Empty
\caption{fig}\label{fig:label}
\end{figure}

\end{document}

这将产生三个相同的标题编号,而不是 1a、1b、1c:

在此处输入图片描述

答案1

以下应该符合您的要求。它是借助subcaption以及一些手动浮动管理:

在此处输入图片描述

\documentclass{article}
\usepackage{verbatimbox}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb,listings,caption}
\usepackage[labelformat=simple]{subcaption}
\usepackage[scaled=.73]{beramono}
\fvset{baselinestretch=0.94}
\parindent=0pt
\parskip = 6pt

\makeatletter
\AtBeginDocument{\DeclareCaptionSubType{lstlisting}
\renewcommand{\p@sublstlisting}{\thelstlisting}
\renewcommand{\thesublstlisting}{(\alph{sublstlisting})}
}
\makeatother
% "define" Scala
\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield,then}, % we use `then` in the pseudocode
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

% activate the language and predefine settings
\lstset{
    basicstyle=\linespread{0.94}\ttfamily,%
    language=scala,%
    commentstyle=\itshape,%
    keywordstyle=\bfseries,%
    fancyvrb=true,%
    mathescape=true,%  for pseudocode
    captionpos=b, % captions at the bottom
}
\newsavebox{\verbsavebox}
\begin{document}

In Fig.~\ref{fig:label} versus Listings~\ref{lst:label1} and \ref{lst:label2}.

\begin{figure}
Empty
\caption{fig}\label{fig:label}
\end{figure}

\begin{figure} % [htp]
% Let's make this an lstlisting, not a figure...
\makeatletter\def\@captype{lstlisting}\makeatother
\begin{lrbox}{\verbsavebox}
\begin{lstlisting}[xrightmargin=.6667\linewidth]
trait Sys[S <: Sys[S]] {
  type ID <: Ident[S#Tx]
  type Tx
}

trait Muta[S <: Sys[S]] {
  def id: S#ID
  // well defined:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}

trait Ident[Tx] {
  def dispose(tx: Tx): Unit
}
\end{lstlisting}
\end{lrbox}
\subcaptionbox{list1\label{lst:label1}}{\usebox{\verbsavebox}}\hfill%
\begin{lrbox}{\verbsavebox}
\begin{lstlisting}[xrightmargin=.6667\linewidth]
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  def id: S#ID   
  // incompatible type:
  def dispose(tx: S#Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\end{lrbox}
\subcaptionbox{list2\label{lst:label2}}{\usebox{\verbsavebox}}\hfill%
\begin{lrbox}{\verbsavebox}
\begin{lstlisting}[xrightmargin=.6667\linewidth]
trait Sys {
  type ID <: Ident[Tx]
  type Tx
}

trait Muta[S <: Sys] {
  // evidence required:
  val s: S
  def id: s.ID
  // well defined:
  def dispose(tx: s.Tx) =
    id.dispose(tx)
}
\end{lstlisting}
\end{lrbox}
\subcaptionbox{list3\label{lst:label3}}{\usebox{\verbsavebox}}
\caption{Total caption\label{lst:total}}
\end{figure}

\end{document}

\verbsavebox每个列表都设置在宽度为 的框中.3333\linewidth(通过设置xrightmargin=.6667\linewidth)。这些框一旦设置,就会使用 添加\subcaptionbox{<caption>}{\usebox{\verbsavebox}},其中<caption>包括标题和\label

figure环境中,我们策略性地将 更新\@captypelstlisting。这会让 TeX 误以为它正在处理一个浮点数,lstlisting当你调用 时\caption。这是必要的,因为\subcaptions 被设置为main \caption,它要求正确设置\p@lstlisting(与 subcounter 关联的父计数器)。这一切都与包的功能有关。sublstlistingsubcaption

子标题的定义\AtBeginDocument与浮点定义一致listings,同时完成。subcaption要求有一个浮点数存在,然后才允许为其定义子标题。

相关内容