1

I'm working on my thesis that uses many code listing in it. In each listing, I want to put caption with the chapter number followed by the position of that listing on that chapter. The problem is my thesis uses Roman numbering for the chapter yet I want to use Arabic numbering for my listing caption. So, for example my first chapter (Chapter I), I want to caption the first listing as Listing 1.1. Here is small sample text that describe my problem.

\documentclass[a4,12pt]{report}

\renewcommand{\thechapter}{\Roman{chapter}}

\usepackage{listings}

\begin{document}
    \chapter{Introduction}
    Here is chapter 1
    \begin{lstlisting}[caption=First Listing,frame=single]
    #include <stdio.h>

    int main()
    {
        printf("Hello World");
        return 0;
    }
    \end{lstlisting}
    \chapter{Analysis}
    Here is chapter 2
    \begin{lstlisting}[caption=Second Listing,frame=single]
        #include <stdio.h>

        int main()
        {
        printf("Hello World");
        return 0;
        }
    \end{lstlisting}
\end{document}

Is there any way that I can maintain using Roman numbering for the chapter but using Arabic one for the listing caption? Thank you for the help.

closed as off-topic by user31729, Stefan Pinnow, user36296, Troy, siracusa Jan 17 at 22:34

  • This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \renewcommand{\thelstlisting}{\arabic{chapter}.\arabic{lstlisting}} should work, but consider that using Roman chapter numbers for the real chapters and arabic numbers for chapter numbers in compound listing numbers may be confusing. 1.1 could be misunderstood as Listing 1 from section 1 – user31729 Jan 17 at 16:56
  • @ChristianHupfer I got this error when adding that command \thelstlisting undefined. \renewcommand{\thelstlisting} – Akhmad Zaki Jan 17 at 17:04
  • I remember that listings does define the lstlisting counter after \begin{document}, so using \renewcommand{\thelstlisting}{...} in the preamble will fail. Try \AtBeginDocument{\renewcommand{\thelstlisting}{\arabic{chapter}.\arabic{lstlisting}}} – user31729 Jan 17 at 17:07
  • @ChristianHupfer I still got the same error – Akhmad Zaki Jan 17 at 17:10
  • 4
    I'm voting to close this question as off-topic because the issue could be solved in comments – user31729 Jan 17 at 18:44