带序言的排版列表(最好使用列表包)

带序言的排版列表(最好使用列表包)

目前我使用该listings包来排版我的列表。列表是浮动的,使用行号,并有顶部和底部规则,以便与周围的文本区分开来。

我真正想要的是每个列表都有一个简短的序言,由(较轻的)规则分隔,并包含列表的注释,如断言、副作用、要求等。所以我需要的是较重的顶部规则、较轻的中间规则和较重的底部规则,就像从包中知道的那样booktabs。此外,序言的行不应计算在内,因为序言实际上不是列表的一部分。

如何在 LaTeX 中轻松实现这一点?我更愿意使用软件包listings

答案1

以下示例使用tcolorbox包构造浮动列表,但不对列表进行编号。环境preamblelisting将前言作为强制参数。可选参数是任何tcolorbox选项。排版的底层包是listings

注意:您可能必须更新tcolorbox软件包才能进行编译。

\documentclass{report}
\usepackage[skins,listings]{tcolorbox}
\usepackage{lipsum}

\newtcblisting{preamblelisting}[2][]{%
  float=htb,empty,
  boxsep=0pt,left=0pt,right=0pt,boxrule=2pt,leftrule=0pt,rightrule=0pt,
  borderline horizontal={2pt}{0pt}{black},
  segmentation engine=path,
  segmentation style={solid,line width=1pt},
  comment and listing,
  fontupper=\itshape,
  listing options={% put any listing options here
    language={[LaTeX]TeX},
    aboveskip=0pt, belowskip=0pt,numbers=left,numberstyle=\tiny,
    basicstyle=\ttfamily,columns=fullflexible},
  comment={#2},#1
}

\begin{document}

\lipsum[1]

\begin{preamblelisting}{This is the preamble. Lorem ipsum dolor sit amet,
  consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
  adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
\end{test}
\end{preamblelisting}

\begin{preamblelisting}[float=b]{This is the preamble. Lorem ipsum dolor sit amet,
  consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
  adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
This is an example
This is an example
\end{test}
\end{preamblelisting}

\lipsum

\end{document}

在此处输入图片描述

更新:

此更新考虑了 OP 的评论,并添加了编号,标题如下。此外,它与 listings 包中的普通列表融合在一起,即两者可以并行使用。最后,cleveref使用:

\documentclass{report}
\usepackage{cleveref,varwidth}
\usepackage[skins,listings]{tcolorbox}
\usepackage{lipsum}

\Crefname{lstlisting}{Listing}{Listings}

\AtBeginDocument{%
\newtcblisting[blend into=listings]{preamblelisting}[3][]{%
  float=htb,empty,
  boxsep=0pt,left=0pt,right=0pt,boxrule=2pt,leftrule=0pt,rightrule=0pt,
  borderline horizontal={2pt}{0pt}{black},
  segmentation engine=path,
  segmentation style={solid,line width=1pt},
  comment and listing,
  fontupper=\itshape,
  listing options={% put any listing options here
    language={[LaTeX]TeX},
    aboveskip=0pt, belowskip=0pt,numbers=left,numberstyle=\tiny,
    basicstyle=\ttfamily,columns=fullflexible},
  attach boxed title to bottom center,
  varwidth boxed title,
  coltitle=black,
  boxed title style={blanker,top=2mm},
  title={#2},comment={#3},#1
}}


\begin{document}

\lstlistoflistings

\chapter{Example}

\begin{lstlisting}[language={[LaTeX]TeX},
    numbers=left,numberstyle=\tiny,
    basicstyle=\ttfamily,columns=fullflexible,
    caption={My normal listing},
    captionpos=b,
    label=mynormal]
This is a normal listing
\end{lstlisting}

\lipsum[2]

\textcolor{red}{\Cref{mynormal} is a normal listing and \Cref{mypreamble} is a listing
with a preamble.}

\begin{preamblelisting}[label=mypreamble]{My preamble listing}%
{This is the preamble. Lorem ipsum dolor sit amet,
  consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
  adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
\end{test}
\end{preamblelisting}

\begin{preamblelisting}[float=b,label=floatingpreamble]{My floating preamble listing}%
{This is the preamble. Lorem ipsum dolor sit amet,
  consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
  adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
This is an example
This is an example
\end{test}
\end{preamblelisting}

\lipsum

\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容