
以下の MWE では、2 つのリストに異なるキャプション フォーマットを定義することは可能ですか? 例では、最初のリストは青いキャプション バックグラウンド (現在と同じ) を使用し、2 番目のリストは赤を使用する必要があります。ただし、一般的には、異なるリストに完全に異なるキャプションを使用できるようにしたいと考えています。たとえば、一部のリストにはキャプションが上部に表示され、一部のリストにはキャプションが下部に表示されます。これはcaptionsetup
グローバル オプションのようですが、1 つのリストまたはリストのグループにのみ適用する方法についてのドキュメントは見つかりません。
\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}