# Code caption with listing without title, only numbers

I'm writing a book in spanish which has R code in it. I need some of the code listings to have a caption but I don't really need a title. I want the caption to show also the number of caption and chapter, something like: Código 1.1 in courier and bold. So I did the following

\usepackage{caption}
\usepackage{listings}
\lstset{ %
backgroundcolor=\color{white},
\usepackage{color} or \usepackage{xcolor}
basicstyle=\footnotesize\ttfamily,
breakatwhitespace=false,
breaklines=true,
captionpos=t,
extendedchars=true,
keepspaces=true,
language=R,
numbers=none,
}

\renewcommand\lstlistingname{Código}
\renewcommand\lstlistlistingname{Código}

\DeclareCaptionStyle{listing} [justification=raggedright,indention=0pt, labelfont=bf]{#1}
\captionsetup[lstlisting]{style=listing,, labelsep=none}


However, when I insert a listing by

\begin{lstlisting}[title={}, label="codigo1.1"]
a <- test()
b <- lm(y~ a + b, data=datos)
\end{lstlisting}


I get a bunch of errors and the code caption doesn't show. The odd thing is that if I use tite={""}, then I get the expected caption, but still get a bunch of errors from the compiler. These errors normal point to the title definition and asks if I wanted to type ## instead of #

Not sure what I'm doing wrong. Any help would be greatly appreciated

eric,

Don't use " in labels: it's a shorthand character for Spanish.

You just need to fake an empty caption: not really empty, but with empty contents.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{xcolor}

\usepackage{caption}
\usepackage{listings}
\lstset{
backgroundcolor=\color{white},
basicstyle=\footnotesize\ttfamily,
breakatwhitespace=false,
breaklines=true,
captionpos=t,
extendedchars=true,
keepspaces=true,
language=R,
numbers=none,
}

\renewcommand\lstlistingname{Código}
\renewcommand\lstlistlistingname{Código}
\DeclareCaptionStyle{listing} [justification=raggedright,indention=0pt, labelfont=bf]{}
\captionsetup[lstlisting]{style=listing, labelsep=none}

\begin{document}

\begin{lstlisting}[caption=\mbox{}, label=codigo1.1]
a <- test()
b <- lm(y~ a + b, data=datos)
\end{lstlisting}

\end{document}


• Thanks. This seemed to work. I'm still a bit confused on the second set of curly braces in the \DeclareCaptionStyle command, since I thought that including #1 should only refer to the caption. this is very unclear in the listing documentation. – Efuchs Mar 5 '15 at 20:06
• One last thing...is it possible to add a listing without any caption? or do I have to define a new environment? – Efuchs Mar 5 '15 at 20:06
• @user1623734 Just specify neither title= nor caption=. Of course label would not make sense. – egreg Mar 5 '15 at 22:05