如何將不同的自訂標題樣式套用至不同的清單?

如何將不同的自訂標題樣式套用至不同的清單?

在下面的 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}

在此輸入影像描述

相關內容