This issue is mainly realated to Define a new caption in a listing environment.
I want to define caption setup for default lstlisting
but also want to define caption setups for new listing environments. But somehow \captionsetup[lstlisting]
overrides other caption setups defined for new listing environments. I borrowed example from the answer of the related question.
How can I make to work the following code properly?
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{caption}
\definecolor{listinggray}{gray}{0.98}
\definecolor{lbcolor}{rgb}{0.98,0.98,0.98}
\lstset{
% ...
float=tp,
% ...
}
\usepackage{caption}
% caption setup for default listing
\DeclareCaptionFont{listingcapfont}{\color{green}}
\DeclareCaptionFormat{listingcapfor}{
\colorbox[cmyk]{0.9, 0.1, 0.9,0.5}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[lstlisting]{
format=listingcapfor,
labelfont=listingcapfont,
textfont=listingcapfont,
singlelinecheck=false,
margin=0pt,
font={bf,footnotesize}
}
% caption setup for code
\DeclareCaptionFont{codecapfont}{\color{white}}
\DeclareCaptionFormat{codecapfor}{
\colorbox[cmyk]{0.43,0.35,0.35,0.01}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[code]{
format=codecapfor,
labelfont=codecapfont,
textfont=codecapfont,
singlelinecheck=false,
margin=0pt,font={bf,footnotesize}
}
\lstnewenvironment{code}[1][]%
{\noindent\minipage{\linewidth}%
\lstset{#1}%
\captionsetup{options=code}% execute options set with \captionsetup[code]{...}
}
{\endminipage}
% caption setup for tip
\DeclareCaptionFont{tipcapfont}{\color{red}}
\DeclareCaptionFormat{tipcapfor}{
\colorbox[cmyk]{0.0,0.35,0.35,0.01}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[tip]{
format=tipcapfor,
labelfont=tipcapfont,
textfont=tipcapfont,
singlelinecheck=false,
margin=0pt,
font={bf,footnotesize}
}
\makeatletter
\newcounter{tip}
\lstnewenvironment{tip}[1][]%
{%
\noindent\minipage{\linewidth}%
\def\lstlistingname{Tip}%
\def\lstlistingautorefname{Tip}%
\let\c@lstlisting=\c@tip
\let\thelstlisting=\thetip
\lstset{#1,keywordstyle=\color{black},
numberbychapter=false,
basicstyle=\footnotesize\ttfamily\color{black}\bfseries,
backgroundcolor=\color{white},frame=none,captionpos=tc}%
\captionsetup{options=tip}% execute options set with \captionsetup[tip]{...}
}
{\endminipage}
\makeatother
\begin{document}
\begin{lstlisting}[caption=This is a lstlisting block]
Default listing code...
\end{lstlisting}
....Look, just because I don't be givin' no man a foot massage don't make it
right for Marsellus....
\begin{code}[caption=This is a code block]
Wonderfull code...
\end{code}
....Look, just because I don't be givin' no man a foot massage don't make it
right for Marsellus....
\begin{tip}[caption=This is a tip block]
A tip!
\end{tip}
\end{document}