
En el MWE siguiente, ¿es posible definir diferentes formatos de subtítulos para los dos listados? En el ejemplo, la primera lista debe usar un fondo de título azul (como ahora), mientras que la segunda debe ser roja. Sin embargo, en general quiero poder tener subtítulos completamente diferentes en diferentes listados; por ejemplo, algunos listados tendrán subtítulos arriba mientras que otros tendrán subtítulos debajo. Parece que captionsetup
es una opción global y no puedo encontrar documentación sobre cómo aplicarla solo a un listado o a un grupo de listados.
\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}
Respuesta1
Podrías crear un nuevo tcolorbox con listados:
\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}