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 am working with the listings package and trying to change the separator from the default period to a hyphen. However, I cannot seem to figure out how to.

I tried the code snippet below, but it doesn't appear to be working.

\renewcommand{\thelstlisting}{\thechapter-\arabic{lstlisting}}

\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\begin{document}
\chapter{The Chapter}

\begin{lstlisting}[caption=The Listing Caption, language=xml]
 <?xml version="1.0" encoding="utf-8">
 <data>
    <format/>
 </data>
\end{lstlisting}

\end{document}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

The \thelstlisting macro is defined via the \AtBeginDocument hook.

You can either re-define it after \begin{document}, or use the \AtBeginDocument hook yourself (recommended).

Code

\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\AtBeginDocument{\renewcommand*{\thelstlisting}{\thechapter-\arabic{lstlisting}}}
\begin{document}
\chapter{The Chapter}
\begin{lstlisting}[caption=The Listing Caption, language=xml]
 <?xml version="1.0" encoding="utf-8">
 <data>
    <format/>
 </data>
\end{lstlisting}
\end{document}

Output

enter image description here

share|improve this answer
    
Neat! Thank you very much for this. –  phiri Jan 9 '13 at 7:29
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.