1

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}

1 Answer 1

1

I think I solved the issue. I'm not sure that it is right/proper, nevertheless I post the solution for ones who come across here.

According to manual of the caption package (see page 4 and 19),

It’s good to know that \captionsetup has an effect on the current environment only.

Hence, I encountered \clearcaptionsetup that gets rid of the parameters marked for an automatic use within a particular environment.

Hence, it is enough to use the \clearcaptionsetup{lstlisting} in the environment definitions.

\lstnewenvironment{code}[1][]%
    {\noindent\minipage{\linewidth}%
            \lstset{#1}%
            \clearcaptionsetup{lstlisting}
            \captionsetup{options=code}
    }
  {\endminipage}

and

\makeatletter
\newcounter{tip}
\lstnewenvironment{tip}[1][]%
    {%
    \noindent\minipage{\linewidth}%
            \def\lstlistingname{Tip}%
            \def\lstlistingautorefname{Tip}%
            \let\c@lstlisting=\c@tip
            \let\thelstlisting=\thetip
            \clearcaptionsetup{lstlisting}
            \lstset{#1,keywordstyle=\color{black},
                    numberbychapter=false,
                    basicstyle=\footnotesize\ttfamily\color{black}\bfseries,
                    backgroundcolor=\color{white},frame=none,captionpos=tc}%
            \captionsetup{options=tip}
    }
 {\endminipage}
\makeatother

Hence, lstlisting, code and tip blocks work without interfering each other.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .