
No MWE abaixo é possível definir diferentes formatos de legenda para os dois anúncios? No exemplo, a primeira listagem deve usar um fundo de legenda azul (como está agora), enquanto a segunda deve ser vermelha. No entanto, em geral, quero poder ter legendas completamente diferentes em listagens diferentes. Por exemplo, algumas listagens terão legendas acima, enquanto outras terão legendas abaixo. Parece que captionsetup
é uma opção global e não consigo encontrar documentação sobre como aplicá-la apenas a uma única listagem ou a um grupo de listagens.
\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}
Responder1
Você poderia criar um novo tcolorbox com listagens:
\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}