[Tex/LaTex] Code caption with listing without title, only numbers

captionslistings

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,

Best Answer

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}

enter image description here

Related Question