다양한 목록에 다양한 사용자 정의 캡션 스타일을 적용하려면 어떻게 해야 합니까?

다양한 목록에 다양한 사용자 정의 캡션 스타일을 적용하려면 어떻게 해야 합니까?

아래 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}

여기에 이미지 설명을 입력하세요

관련 정보