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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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. – lightonphiri Jan 9 '13 at 7:29

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.