如何将不同的自定义标题样式应用到不同的列表?

如何将不同的自定义标题样式应用到不同的列表?

在下面的 MWE 中,是否可以为两个列表定义不同的标题格式?在示例中,第一个列表应使用蓝色标题背景(如现在一样),而第二个列表应使用红色。但是,一般来说,我希望能够在不同的列表上使用完全不同的标题,例如,一些列表的标题在上方,而一些列表的标题在下方。这似乎captionsetup是一个全局选项,我找不到有关如何将其仅应用于单个或一组列表的文档。

\documentclass{article}
\usepackage[svgnames]{xcolor} 
\usepackage{caption}
\usepackage{listings}
\usepackage{calc} 
\lstdefinestyle{outline}{
         basicstyle=\scriptsize\ttfamily,
         numberstyle=\tiny,
         numbersep=5pt,
         tabsize=2,
         extendedchars=true,
         breaklines=true,
         keywordstyle=\color{blue},
         frame=b,
         stringstyle=\color{green!40!black}\ttfamily,
         showspaces=false,
         showtabs=false,
         numbers=left,
         xleftmargin=17pt,
         framexleftmargin=17pt,
         showstringspaces=false,
         backgroundcolor=\color[RGB]{200,200,200},
         belowcaptionskip=-1pt
}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[RGB]{60,100,180}{\parbox{\textwidth - 2 \fboxsep}{\hspace{14pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

\begin{document}
\begin{lstlisting}[style=outline,caption=Blue]
This should have blue caption.
\end{lstlisting}

\begin{lstlisting}[style=outline,caption=Red]
This should have red caption.
\end{lstlisting}
\end{document}

答案1

您可以创建一个包含列表的新 tcolorbox:

\documentclass{article}
\usepackage[svgnames]{xcolor} 
\usepackage{listings}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\lstdefinestyle{outline}{
         basicstyle=\scriptsize\ttfamily,
         numberstyle=\tiny,
         numbersep=5pt,
         tabsize=2,
         extendedchars=true,
         breaklines=true,
         keywordstyle=\color{blue},
         stringstyle=\color{green!40!black}\ttfamily,
         showspaces=false,
         showtabs=false,
         numbers=left,
         showstringspaces=false,
}

\definecolor{myblue}{RGB}{60,100,180}

\newtcblisting[auto counter]{mylist}[2][]{%
    title={Listing \thetcbcounter: #2},
    colback=lightgray,
    colframe=myblue,
    fonttitle={\footnotesize\bfseries},
    sharp corners,
    listing only,
    enhanced,
    left=17pt,
    boxsep=0pt,
    boxrule=0pt,
    toptitle=4pt,
    bottomtitle=4pt,
    top=0pt,
    bottom=0pt,
    listing engine=listings,
    listing options={style=outline},
    #1
}

\begin{document}

\begin{mylist}{Blue and above}
This should have blue caption.
\end{mylist}

\begin{mylist}[%
    flip title={sharp corners},
    colbacktitle=red
    ]{Red and below}
This should have red caption.
\end{mylist}

\end{document}

在此处输入图片描述

相关内容