Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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][caption=Tip]%
    {
    \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}[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}
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?

share|improve this question
    
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Jan 10 '12 at 20:21
    
@PeterGrill, Ok! I'll do that and edit the question. Meanwhile if anyone know what I am talking about, be my guest and answer the questiot! –  Filip Ekberg Jan 10 '12 at 20:24
    
@PeterGrill, Updated, does it look better like that? –  Filip Ekberg Jan 10 '12 at 20:47
    
Does not compile for me. I get ! Package Listings Error: Couldn't load requested language.' You should also eliminate most of the settings lstset` unless they are necessary to reproduce the problem. –  Peter Grill Jan 10 '12 at 20:56
add comment

1 Answer

up vote 7 down vote accepted

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 http://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}
share|improve this answer
    
Thanks, it works almost as I want it to! Is it possible to get the "Tip 1" to be "Tip 1.1" instead? I tried use numberbychapter=true but that didn't solve it. –  Filip Ekberg Jan 10 '12 at 21:59
    
Which document class are you using? Chapter numbering only applies to book and report, not to article, IIRC. –  Michael Palmer Jan 10 '12 at 22:28
    
Thanks, it works now. I just had to remove \let\thelstlisting=\thetip and the numbering looks as I want. Thanks a bunch! –  Filip Ekberg Jan 10 '12 at 22:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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