帶有序言的排版清單(最好使用清單包)

帶有序言的排版清單(最好使用清單包)

目前我使用該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 的評論,並添加了下面設定的標題編號。此外,這會混合到清單包中的正常清單中,即兩者可以並行使用。最後,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}

在此輸入影像描述 在此輸入影像描述 在此輸入影像描述

相關內容