5

Is there a way to change style of a caption in listings without the caption package (make it smaller and non-bold)? I am using \lstnewenvironment to create my own customized environment and then use listings via this new environment.

I tried to do it with the caption package with \captionsetup[lstlisting]{font={scriptsize,tt}} and it worked, but the problem is that I cannot use caption package as it messes up captions in the rest of the document (as soon as I "include" it), and I have to use predefined class.

Thank you for your help!

(EDIT) Class: http://www.acm.org/sigs/publications/sig-alternate.cls

(EDIT) MEW:

\documentclass{sig-alternate}

%\usepackage{caption}
\usepackage{listings}

\begin{document}

\title{The Title}

\numberofauthors{1}
\author{
\alignauthor
       John Doe\\
       \affaddr{Lorem ispum}\\
       \affaddr{Lorem ispum}\\
       \affaddr{Lorem ipsum}\\
       \email{[email protected]}
}
\date{30 July 1999}
\maketitle



\lstnewenvironment{mycode}[1][]
{
  \minipage[b]{0.3\textwidth}
  \lstset{
    captionpos=b,
    language=C,
    numbers=left,
    basicstyle=\tiny\ttfamily,
    columns=fullflexible,
    showstringspaces=false,
    numbersep=3pt,
    #1
  }
}
{
  \endminipage
}
%\captionsetup[lstlisting]{font={small,tt}}

\begin{mycode}[title=Awesome C program,label=code:AwesomeC]
  int main(int argc, char* argv[]) {
    return 0;
  }
\end{mycode}

If I use "caption" package, caption of the figure changes style (it is no longer bold). Otherwise I cannot change caption of the code...

\begin{figure}
  \centering
  This is a figure
  \caption{Caption of the figure}  
\end{figure}

\end{document}
7
  • 2
    It would be awesome if you could just include a minimal working example (MWE) that community members can play with. It could include some of your basic listings settings for starts.
    – Werner
    Aug 20, 2013 at 22:40
  • And also provide a link to the custom class you are using. It might be easier to see what that class does with captions so that the caption package can replicate it. Did you try loading caption with the [style=base] option?
    – Alan Munn
    Aug 21, 2013 at 4:25
  • If a document class uses a customised caption layout it usually redefines \@makecaption. Since the listings package is using this macro to typeset the caption, too, your listings caption should follow this design automatically -- otherwise there is something wrong with your document class.
    – user2574
    Aug 21, 2013 at 7:55
  • I use the following class: acm.org/sigs/publications/proceedings-templates (Option 2).
    – Ivan
    Aug 21, 2013 at 8:10
  • (complete, working) MWE: pastebin.com/47NP6HZS
    – Ivan
    Aug 22, 2013 at 9:26

1 Answer 1

8

One possibility is to use the caption package, but loading it in the form

\usepackage[font=bf,skip=\baselineskip]{caption}

so as to preserve the default caption style implemented by the sig-alternate document class. Then you can invoke

\captionsetup[lstlisting]{font={small,tt}}

to change the listings caption format. A complete example:

\documentclass{sig-alternate}
\usepackage{listings}
\usepackage[font=bf,skip=\baselineskip]{caption}
\captionsetup[lstlisting]{font={small,tt}}

\lstnewenvironment{mycode}[1][]
{
  \minipage[b]{0.3\textwidth}
  \lstset{
    captionpos=b,
    language=C,
    numbers=left,
    basicstyle=\tiny\ttfamily,
    columns=fullflexible,
    showstringspaces=false,
    numbersep=3pt,
    #1
  }
}
{
  \endminipage
}

\begin{document}

\title{The Title}
\numberofauthors{1}
\author{
\alignauthor
       John Doe\\
       \affaddr{Lorem ispum}\\
       \affaddr{Lorem ispum}\\
       \affaddr{Lorem ipsum}\\
       \email{[email protected]}
}
\date{30 July 1999}
\maketitle

\begin{mycode}[title=Awesome C program,label=code:AwesomeC]
  int main(int argc, char* argv[]) {
    return 0;
  }
\end{mycode}

\begin{figure}
  \centering
  This is a figure
  \caption{Caption of the figure}  
\end{figure}

\begin{table}
  \centering
  This is a table
  \caption{Caption of the table}  
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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