7

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}
8

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.