2

I am kindly requesting for assistance on how to put the headings Matlab Code and Pseudocodes under List of Codes and headings Matlab Output and R Output under List of Output using the below MWE. In other words, I am desiring to have two separate list of listings namely List of Codes and List of Output. However under List of Codes,I want to have sections (unnumbered) namely Matlab Code and Pseudocode, and under List of Output,I want to have sections (unnumbered) as well namely R Output and Matlab Output. Your help is going to be greatly appreciated. The suggested solution to my problem at Multiple listings styles is not producing the desired output since it does not address the following specifications:

  1. List of C++, List of R and List of Pseudocode are in chapter format using book document class which is something I do not like;
  2. List of C++, List of R and List of Pseudocode are supposed to be in an unnumbered section format under the chapter (unnumbered) by the name List of Codes;
  3. I also want to create a second independent listing by the chapter (unnumbered) name List of Output. Under this unnumbered chapter, I want to have unnumbered sections namely Matlab Output and R Output, and
  4. The suggested solution does not work well under the new caption package. Actually, I had already tried that suggested solution and failed to get the desired output before @Tom pointed out the issue of the new caption package at Incorrect classification of multiple listings entries.

The output I am desiring is shown in the attached picture: Desired Output

I would kindly appreciate if other users stop suggesting the solution at Multiple listings styles since it does not solve my problem as I have mentioned above. I may have not explained my case well and I would greatly appreciate if someone could improve this post so that other users may not consider it a duplicate or vote to close it. From my understanding, Tex.SE is meant to provide assistance to those with Latex problems given that they have tried something that did not work out, and not to close questions that are in need of a solution or solutions. I would also appreciate if I can get the solution to my problem.

My MWE takes into account the caption package updates:

\documentclass[openany]{book}
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\usepackage{listings}

% --------------------------------------- C++ \newcommand{\lstlistmatlaboutputname}{List of Matlab Output} \newcommand{\lstlistofmatlaboutput}{\begingroup \tocfile{\lstlistmatlaboutputname}{loc} \endgroup}

\newcommand{\lstlistroutputname}{List of R Output} \newcommand{\lstlistofroutput}{\begingroup \tocfile{\lstlistroutputname}{loo} \endgroup} % --------------------------------------- R \newcommand{\lstlistmatlabcodename}{List of Matlab Code} \newcommand{\lstlistofmatlabcode}{\begingroup \tocfile{\lstlistmatlabcodename}{lor} \endgroup} % --------------------------------------- Pseudocode \newcommand{\lstlistpseudocodename}{List of Pseudocode} \newcommand{\lstlistofpseudocode}{\begingroup \tocfile{\lstlistpseudocodename}{lop} \endgroup}

\makeatletter \lstnewenvironment{matlaboutput}[1][]{% \renewcommand{\lstlistingname}{Matlab Output}% \renewcommand{\ext@lstlisting}{loc}% %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{loc}{}{}% use this with earlier version caption package \lstset{language=C++,#1}}% {}

\lstnewenvironment{routput}[1][]{% \renewcommand{\lstlistingname}{R Output}% \renewcommand{\ext@lstlisting}{loo}% %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{loc}{}{}% use this with earlier version caption package \lstset{language=R,#1}}% {}

\lstnewenvironment{matlabcode}[1][]{% \renewcommand{\lstlistingname}{Matlab Code}% \renewcommand{\ext@lstlisting}{lor}% %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lor}{}{}% use this with earlier version caption package \lstset{language=R,#1}} {}

\lstnewenvironment{pseudocode}[1][]{% \renewcommand{\lstlistingname}{Pseudocode}% \renewcommand{\ext@lstlisting}{lop}% %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lop}{}{}% use this with earlier version caption package \lstset{basicstyle=\ttfamily,#1}}% {} \makeatother

\begin{document}

\lstlistofmatlaboutput
\lstlistofroutput
\lstlistofmatlabcode    
\lstlistofpseudocode

\chapter{Listings}
\begin{matlaboutput}[caption = {Some class definition}]
    % example matlab output
\end{matlaboutput}

\begin{routput}[caption = {Some class definition}]
    % example matlab output
\end{routput}

\begin{matlabcode}[caption = {For educational purposes}]
    % example matlab code 1
\end{matlabcode}

\begin{matlabcode}[caption = {Sample code from Matlab}]
    % example matlab code 2
\end{matlabcode}

\begin{pseudocode}[caption={Hello world}]
    % example pseudocode
\end{pseudocode}

\end{document}

itc
  • 657

1 Answers1

0

After some trial and error, I've come up with a solution to your first problem, I think.

Regarding your third and fourth points, I've got this to work:

I've tried to implement this answer to make the list of code and the list of output as sections and not as chapters, as you specified in your first point, without success. I hope this answer is helpful to you, at least in some way.

Finally, I've seen this other answer to the same question, and surprisingly it seems to work! Please check my updated code. There must be a way to simplify it, but I think it works fine.

Newer code:

\documentclass[openany]{book}
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\usepackage{listings}
\usepackage{tocbasic}
% \setuptoc{loc}{leveldown}
% \setuptoc{loo}{leveldown} %these do not work

\newcommand{\lstlistcodesname}{List of codes} \newcommand{\lstlistofcodes}{\begingroup \tocfile{\lstlistcodesname}{loc} \endgroup}

\newcommand{\lstlistoutputname}{List of output} \newcommand{\lstlistofoutput}{\begingroup \tocfile{\lstlistoutputname}{loo} \endgroup}

\makeatletter \lstnewenvironment{matlaboutput}[1][]{% \renewcommand{\lstlistingname}{Matlab Output}% \renewcommand{\ext@lstlisting}{loo}% \lstset{language=C++,#1}}% {}

\lstnewenvironment{routput}[1][]{% \renewcommand{\lstlistingname}{R Output}% \renewcommand{\ext@lstlisting}{loo}% \lstset{language=R,#1}}% {}

\lstnewenvironment{matlabcode}[1][]{% \renewcommand{\lstlistingname}{Matlab Code}% \renewcommand{\ext@lstlisting}{loc}% \lstset{language=R,#1}} {}

\lstnewenvironment{pseudocode}[1][]{% \renewcommand{\lstlistingname}{Pseudocode}% \renewcommand{\ext@lstlisting}{loc}% \lstset{basicstyle=\ttfamily,#1}}% {} \makeatother

\makeatletter \renewcommand{\lstlistofcodes}{% \begingroup \clearpage \section{\lstlistcodesname% Taken from article.cls.... @mkboth{% \MakeUppercase\lstlistcodesname}{\MakeUppercase
\lstlistcodesname} }% @starttoc{loc} \endgroup } \makeatother \makeatletter \renewcommand{\lstlistofoutput}{% \begingroup \clearpage \section
{\lstlistoutputname% Taken from article.cls.... @mkboth{% \MakeUppercase\lstlistoutputname}{\MakeUppercase
\lstlistoutputname} }% @starttoc{loo} \endgroup } \makeatother

\begin{document}

\lstlistofcodes
\lstlistofoutput

\chapter{Listings}
\begin{matlaboutput}[caption = {Some class definition}]
    % example matlab output
\end{matlaboutput}

\begin{routput}[caption = {Some class definition}]
    % example matlab output
\end{routput}

\begin{matlabcode}[caption = {For educational purposes}]
    % example matlab code 1
\end{matlabcode}

\begin{matlabcode}[caption = {Sample code from Matlab}]
    % example matlab code 2
\end{matlabcode}

\begin{pseudocode}[caption={Hello world}]
    % example pseudocode
\end{pseudocode}

\end{document}

Earlier code:

\documentclass[openany]{book}
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\usepackage{listings}
\usepackage{tocbasic}
\setuptoc{loc}{leveldown}
\setuptoc{loo}{leveldown}

\newcommand{\lstlistcodesname}{List of codes} \newcommand{\lstlistofcodes}{\begingroup \tocfile{\lstlistcodesname}{loc} \endgroup}

\newcommand{\lstlistoutputname}{List of output} \newcommand{\lstlistofoutput}{\begingroup \tocfile{\lstlistoutputname}{loo} \endgroup}

\makeatletter \lstnewenvironment{matlaboutput}[1][]{% \renewcommand{\lstlistingname}{Matlab Output}% \renewcommand{\ext@lstlisting}{loo}% \lstset{language=C++,#1}}% {}

\lstnewenvironment{routput}[1][]{% \renewcommand{\lstlistingname}{R Output}% \renewcommand{\ext@lstlisting}{loo}% \lstset{language=R,#1}}% {}

\lstnewenvironment{matlabcode}[1][]{% \renewcommand{\lstlistingname}{Matlab Code}% \renewcommand{\ext@lstlisting}{loc}% \lstset{language=R,#1}} {}

\lstnewenvironment{pseudocode}[1][]{% \renewcommand{\lstlistingname}{Pseudocode}% \renewcommand{\ext@lstlisting}{loc}% \lstset{basicstyle=\ttfamily,#1}}% {} \makeatother

\begin{document}

\lstlistofcodes
\lstlistofoutput

\chapter{Listings}
\begin{matlaboutput}[caption = {Some class definition}]
    % example matlab output
\end{matlaboutput}

\begin{routput}[caption = {Some class definition}]
    % example matlab output
\end{routput}

\begin{matlabcode}[caption = {For educational purposes}]
    % example matlab code 1
\end{matlabcode}

\begin{matlabcode}[caption = {Sample code from Matlab}]
    % example matlab code 2
\end{matlabcode}

\begin{pseudocode}[caption={Hello world}]
    % example pseudocode
\end{pseudocode}

\end{document}

Ana
  • 25
  • 8
  • The solution managed to create 2 different listings namely List of Codes and List of Output as desired. However, List of Codes and List of Output are supposed to be unnumbered chapters not sections. The solution however could not create unnumbered sections under each listing e.g under List of Output, there is supposed to be unnumbered sections namely Matlab Output and R Output . I appreciate your answer. – itc Sep 13 '22 at 13:54
  • Thank you @Ana for the efforts. However, I think u misinterpreted my specifications. I want List of Codes and List of Output as chapters. On top of that I want to have sections under List of Output namely List of R Output and List of Matlab Output. I also want the same for the List of Codes as a chapter and under this I want to have sections namely List of Matlab Codes and Pseudocodes. – itc Sep 13 '22 at 17:46
  • My point number 1 is saying that the list of List of C++, List of R and List of Pseudocode are supposed to be sections under a listing chapter which can be List of Codes or List of Output. – itc Sep 13 '22 at 17:49
  • Oh, I misunderstood. Later on, I will try a different approach. – Ana Sep 13 '22 at 17:58
  • Any updates @Ana? – itc Sep 14 '22 at 18:03
  • I am sorry, I couldn't find a solution to your problem – Ana Sep 15 '22 at 06:32