0

I am using \package{lstlisting} to display some matlab code in the latex. My codes are placed in the appendix with labels. Given below is excerpt of my code:

% block of codes
 \clearpage
 \appendix
 \chapter{Appendices}
 \section*{Appendix A.1}\label{appendix_A.1}
   \begin{lstlisting}[caption={Simulation 1},label={simulation_1},language=Matlab]
        % some matlabcode
    \end{lstlisting}

    \begin{lstlisting}[caption={Simulation 2},label={simulation_2},language=Matlab]
        % some matlabcode
    \end{lstlisting}
 \section*{Appendix A.2}\label{appendix_A.2}

And here is my output:

Chapter A: Appendices Section: Appendix A.1

"block of matlab code" with caption Listing A.1: Simulation 1

"block of matlab code" with caption Listing A.2: Simulation 2

Section: Appendix A.2

The thing that I dislike about this is that when I refer sections and listings it seem to share same name. It would be nice if I could have something like Listing 1: Simulation 1, Listing 2: Simulation 2. Anyone for the help? Thank you for your time in advance.

0

It is easy enough to remove \thechapter from \thelstlisting. Note that \section* by itself cannot be referenced by \label.

\documentclass{report}
\usepackage{listings}

 \begin{document}
 \chapter{Yada Yada}

 \clearpage
 \appendix
  \renewcommand{\thelstlisting}{\arabic{lstlisting}}
 \chapter{Appendices}
 \section*{\refstepcounter{section}Appendix \thesection}\label{appendix_A.1}
   \begin{lstlisting}[caption={Simulation 1},label={simulation_1},language=Matlab]
        % some matlabcode
    \end{lstlisting}

    \begin{lstlisting}[caption={Simulation 2},label={simulation_2},language=Matlab]
        % some matlabcode
    \end{lstlisting}
  %
 \section*{\refstepcounter{section}Appendix \thesection}\label{appendix_A.2}

\end{document}

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.