[Tex/LaTex] Define a new caption in a listing environment

captionslistings

I want to have different listings with different types of captions, consider that I've got the following:

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{caption}

\definecolor{listinggray}{gray}{0.98}
\definecolor{lbcolor}{rgb}{0.98,0.98,0.98}

\lstset{
    basicstyle=\footnotesize\ttfamily,
    numberstyle=\tiny,
    numberbychapter=true,    
    numbersep=5pt,        
    float=tp,     
    tabsize=2,     
    xleftmargin=5pt,
    framexleftmargin=5pt,
    extendedchars=true,         
    breaklines=true,
    frame=tb,   
    showspaces=false,       
    showtabs=false,        
    backgroundcolor=\color{lbcolor},
    rulecolor=\color[rgb]{0.78,0.78,0.78},
    framerule=0.5pt,
    showstringspaces=false,
    keywordstyle=\color[rgb]{1.0,0,0}  
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

\lstnewenvironment{code}[1][]%
    {\noindent\minipage{\linewidth} 
            \lstset{#1}
    }
  {\endminipage} 

\lstnewenvironment{tip}[1]%
    {
    \noindent\minipage{\linewidth} 
            \def\lstlistingautorefname{Tip}
            \lstset{#1,keywordstyle=\color{black},numberbychapter=false,basicstyle=\footnotesize\ttfamily\color{black}\bfseries, backgroundcolor=\color{white},frame=none,captionpos=bc }
    }
 {\endminipage}

\begin{document}

\begin{code}
Code test..
\end{code}

....Look, just because I don't be givin' no man a foot massage don't make it right for Marsellus....

\begin{tip}
A tip!
\end{tip}

{\begin{figure}%
This is what I want!
\caption{}%
\label{}%
\end{figure}

\end{document}

This generates the following:

enter image description here

Basically I want all listings to have the caption in the Top Left, but for the environment tip I want it to be Bottom Center as it looks at the third example but the caption should say "Tip 1".

Is this possible? If So, How?

Best Answer

The caption package offers the possibility to define and execute own option sets via \captionsetup[<name>]{...} and \captionsetup{options=<name>}. This way you can define different looks for the captions of code and tip.

(Unfortunately the key options is not documented well, currently it's only listed within the alphabetical reference of the caption package documentation.)

To get tip labeled and counted independently one needs to define an own counter for tip. This part of the solution was taken from https://stackoverflow.com/questions/3900847/latex-listings-different-counters-for-different-listing-environments

\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}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[code]{format=listing,labelfont=white,textfont=white, 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} 

\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=bc}%
            \captionsetup{options=tip}% execute options set with \captionsetup[tip]{...}
    }
 {\endminipage}
\makeatother

\begin{document}

\begin{code}[caption=Test]
Code test..
\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=Test]
A tip!
\end{tip}

\end{document}
Related Question