4

In the MWE below, is it possible to define different caption formats for the two listings? In the example the first listing should use a blue caption background (like it is now) while the second should be red. However, in general I want to be able to have completely different captions on different listings, for example some listings will have captions above while some will have captions below. It seems like captionsetup is a global option, and I can find no documentation on how to apply it only to a single or group of listings.

\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

You could create a new tcolorbox with listings:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.