0

My problem is as follows: I have 3 different listings environments namely List of Matlab Outputs, List of Matlab Code and List of Pseudocodes. However all listings are under List of Matlab Outputs while the List of Codes and Pseudocodes are empty which is not supposed to be the case. I want all Matlab outputs (e.g Matlab Output 1.1,...) to be under List of Matlab Outputs, all Matlab codes (e.g Matlab Code 1.1,...) to be under List of Matlab Codes and all Pseudocodes (e.g Pseudocode 1.1,...) to be under List of Pseudocodes as shown in the picture below: The output I am desiring in the presence of ALL the packages I have loaded
I am looking for a solution that solves the problem in presence of all the packages I loaded. I suspect there are some lines of code that are causing this problem considering the fact that if some lines of code are deleted, I get the desired output. I have attached a picture showing the problem I am facing. I am writing a book so please I want the loaded packages not to be deleted when providing the solution. Below is the picture showing the problem and my MWE respectively:
The problem I am facing

\documentclass[11pt,openany,twoside]{book}
\raggedbottom
\let\cleardoublepage=\clearpage
\usepackage[left=2.5cm, right=2.5cm, top=3cm, bottom=3cm,a4paper] 
{geometry}
\usepackage{parskip}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\newcolumntype{I}{!{\vrule width 1pt}}
\newlength\savedwidth
\newcommand\whline{\noalign{\global\savedwidth\arrayrulewidth
\global\arrayrulewidth 1pt}%
\hline
\noalign{\global\arrayrulewidth\savedwidth}}
\usepackage{xcolor, graphicx}
\newcommand\HRule{\noindent\rule{\linewidth}{1.5pt}}
\usepackage[dotinlabels]{titletoc}
\usepackage{background}
\backgroundsetup{contents={}}
\usepackage{helvet}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{hhline}
\pagestyle{fancy} 
\usepackage{booktabs}
\usepackage[noindentafter,calcwidth]{titlesec}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{colortbl}
\newcommand*\myheaderfooterfont{\normalfont}
\usepackage[nottoc]{tocbibind}
\usepackage{verbatim}
\usepackage[ragged]{sidecap}
\usepackage{caption}
\usepackage{tocloft}
\renewcommand{\cfttoctitlefont}{\hfill\normalfont}
\renewcommand{\cftloftitlefont}{\hfill\normalfont}
\renewcommand{\cftlottitlefont}{\hfill\normalfont}
%\renewcommand{\cftafterloftitle}{\hfill}
\setlength\cftbeforetoctitleskip{-2cm}
\setlength\cftbeforeloftitleskip{-2cm}
\setlength\cftbeforelottitleskip{-2cm}
\renewcommand{\cftchapfont}{\normalfont}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\usepackage{regexpatch}
\usepackage{listings}
\usepackage[numbered, framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\makeatletter % --------------------------------------- C++ \newcommand{\lstlistmatlaboutputname}{List of Matlab Output} \lst@UserCommand\lstlistofmatlaboutput{\bgroup \let\contentsname\lstlistmatlaboutputname \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{loc}}% \tableofcontents \egroup} \lstnewenvironment{matlaboutput}[1][]{% \renewcommand{\lstlistingname}{Matlab Output}% \xpatchcmd{\lst@MakeCaption}{lol}{loc}{}{}% \lstset{language=C++,#1}} {} % --------------------------------------- R \newcommand{\lstlistmatlabcodename}{List of Matlab Code} \lst@UserCommand\lstlistofmatlabcode{\bgroup \let\contentsname\lstlistmatlabcodename \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lor}}% \tableofcontents \egroup} \lstnewenvironment{matlabcode}[1][]{% \renewcommand{\lstlistingname}{Matlab Code}% \xpatchcmd{\lst@MakeCaption}{lol}{lor}{}{}% \lstset{language=R,#1}} {} % --------------------------------------- Pseudocode \newcommand{\lstlistpseudocodename}{List of Pseudocode} \lst@UserCommand\lstlistofpseudocode{\bgroup \let\contentsname\lstlistpseudocodename \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lop}}% \tableofcontents \egroup} \lstnewenvironment{pseudocode}[1][]{% \renewcommand{\lstlistingname}{Pseudocode}% \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}% \lstset{basicstyle=\ttfamily,#1}} {} \makeatother

\renewcommand{\lstlistofmatlaboutput}{\begingroup \tocfile{\lstlistmatlaboutputname}{lol} \endgroup} \renewcommand{\lstlistofmatlabcode}{\begingroup \tocfile{\lstlistmatlabcodename}{lol} \endgroup} \renewcommand{\lstlistofpseudocode}{\begingroup \tocfile{\lstlistpseudocodename}{lol} \endgroup}

\makeatletter \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-4\p@}}} {}{} \makeatother

\renewcommand\cftchapafterpnum{\vskip-4pt}

\usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=blue }

\usepackage{filecontents} \begin{filecontents}{person.m} classdef person properties %(here, properties is a keyword) mass=80; height=1.80; end methods function BMI = getBMI(height,weight) BMI = person.mass/person.mass^2; end end end \end{filecontents}

\begin{filecontents}{sample.m} %% Code sections are highlighted. % System command are supported... !gzip sample.m % ... as is line continuation. A = [1, 2, 3,... % (mimicking the ouput is good) 4, 5, 6] fid = fopen('testFile.text', 'w') for i=1:10 fprintf(fid,'%6.2f \n', i); end x=1; %% this is just a comment, though % Context-sensitive keywords get highlighted correctly... p = properties(mydate); %(here, properties is a function) x = linspace(0,1,101); y = x(end:-1:1) % ... even in nonsensical code. ]end()()(((end end)end ))))end (function end %{ block comments are supported %} even runaway block comments are \end{filecontents} \begin{document}

\lstlistofmatlaboutput \lstlistofmatlabcode
\lstlistofpseudocode

\chapter{Listings} \begin{matlaboutput}[caption = {Some class definition}] classdef person properties %(here, properties is a keyword) mass=80; height=1.80; end methods function BMI = getBMI(height,weight) BMI = person.mass/person.mass^2; end end end \end{matlaboutput}

\begin{matlabcode}[caption = {For educational purposes}] % example of while loop using placeholders while "\ph{condition}" if "\ph{something-bad-happens}" break else % do something useful end % do more things end \end{matlabcode}

\begin{matlabcode}[caption = {Sample code from Matlab}] %% Code sections are highlighted. % System command are supported... !gzip sample.m % ... as is line continuation. A = [1, 2, 3,... % (mimicking the ouput is good) 4, 5, 6] fid = fopen('testFile.text', 'w') for i=1:10 fprintf(fid,'%6.2f \n', i); end x=1; %% this is just a comment, though % Context-sensitive keywords get highlighted correctly... p = properties(mydate); %(here, properties is a function) x = linspace(0,1,101); y = x(end:-1:1) % ... even in nonsensical code. ]end()()(((end end)end ))))end (function end %{ block comments are supported %} even runaway block comments are \end{matlabcode} \begin{pseudocode}[caption={Hello world}] print "Hello world" \end{pseudocode}

\end{document}

itc
  • 657
  • 1
    Please format your code. It also looks like you're including many packages. Are they all needed for a Minimal Working Example? – Teepeemm Jul 01 '22 at 22:58
  • You've still not formatted everything. And you're including a few paragraphs that have more to do with an autobiography than listings. Can't you delete everything unrelated to the three listings in question? – Teepeemm Jul 01 '22 at 23:37
  • @Teepeemm, I have formatted the code. All the packages in my code are needed. I have deleted the chapters unrelated to the three listings in question. – itc Jul 01 '22 at 23:39
  • @itc I just wrote an answer in chat. Seems you logged out while I was still typing... – Ulrich Diez Jul 03 '22 at 00:48

1 Answers1

2

I delete all unrelated packages, OP should take a look how to provide a MWE. Following is a working example:

Edit: This is working with latest caption package (2022-03-29), earlier version please use commented lines instead.

\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} % --------------------------------------- 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{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 \lstlistofmatlabcode
\lstlistofpseudocode

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

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

enter image description here enter image description here enter image description here

Edit: The problem is caused by the new caption package setup. Following is the working example with all your packages:

\documentclass[11pt,openany,twoside]{book}
\raggedbottom
\let\cleardoublepage=\clearpage
\usepackage[left=2.5cm, right=2.5cm, top=3cm, bottom=3cm,a4paper] 
{geometry}
\usepackage{parskip}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\newcolumntype{I}{!{\vrule width 1pt}}
\newlength\savedwidth
\newcommand\whline{\noalign{\global\savedwidth\arrayrulewidth
        \global\arrayrulewidth 1pt}%
    \hline
    \noalign{\global\arrayrulewidth\savedwidth}}
\usepackage{xcolor, graphicx}
\newcommand\HRule{\noindent\rule{\linewidth}{1.5pt}}
\usepackage[dotinlabels]{titletoc}
\usepackage{background}
\backgroundsetup{contents={}}
\usepackage{helvet}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{hhline}
\pagestyle{fancy} 
\usepackage{booktabs}
\usepackage[noindentafter,calcwidth]{titlesec}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{colortbl}
\newcommand*\myheaderfooterfont{\normalfont}
\usepackage[nottoc]{tocbibind}
\usepackage{verbatim}
\usepackage[ragged]{sidecap}
\usepackage{caption}
\usepackage{tocloft}
\renewcommand{\cfttoctitlefont}{\hfill\normalfont}
\renewcommand{\cftloftitlefont}{\hfill\normalfont}
\renewcommand{\cftlottitlefont}{\hfill\normalfont}
%\renewcommand{\cftafterloftitle}{\hfill}
\setlength\cftbeforetoctitleskip{-2cm}
\setlength\cftbeforeloftitleskip{-2cm}
\setlength\cftbeforelottitleskip{-2cm}
\renewcommand{\cftchapfont}{\normalfont}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\usepackage{regexpatch}
\usepackage{listings}
\usepackage[numbered, framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\makeatletter % --------------------------------------- C++ \newcommand{\lstlistmatlaboutputname}{List of Matlab Output} \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}} {} % --------------------------------------- R \newcommand{\lstlistmatlabcodename}{List of Matlab Code} \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}} {} % --------------------------------------- Pseudocode \newcommand{\lstlistpseudocodename}{List of Pseudocode} \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}} {}

\newcommand{\lstlistofmatlaboutput}{\begingroup \tocfile{\lstlistmatlaboutputname}{loc} \endgroup} \newcommand{\lstlistofmatlabcode}{\begingroup \tocfile{\lstlistmatlabcodename}{lor} \endgroup} \newcommand{\lstlistofpseudocode}{\begingroup \tocfile{\lstlistpseudocodename}{lop} \endgroup} \makeatother

\makeatletter \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-4\p@}}} {}{} \makeatother

\renewcommand\cftchapafterpnum{\vskip-4pt}

\usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=blue }

%\usepackage{filecontents} \begin{filecontents}{person.m} classdef person properties %(here, properties is a keyword) mass=80; height=1.80; end methods function BMI = getBMI(height,weight) BMI = person.mass/person.mass^2; end end end \end{filecontents}

\begin{filecontents}{sample.m} %% Code sections are highlighted. % System command are supported... !gzip sample.m % ... as is line continuation. A = [1, 2, 3,... % (mimicking the ouput is good) 4, 5, 6] fid = fopen('testFile.text', 'w') for i=1:10 fprintf(fid,'%6.2f \n', i); end x=1; %% this is just a comment, though % Context-sensitive keywords get highlighted correctly... p = properties(mydate); %(here, properties is a function) x = linspace(0,1,101); y = x(end:-1:1) % ... even in nonsensical code. ]end()()(((end end)end ))))end (function end %{ block comments are supported %} even runaway block comments are \end{filecontents} \begin{document}

\lstlistofmatlaboutput
\lstlistofmatlabcode    
\lstlistofpseudocode

\chapter{Listings}
\begin{matlaboutput}[breaklines,escapeinside=!!,caption = {Some class definition}]
    classdef person
    properties %(here, properties is a keyword)
    mass=80;
    height=1.80;
    end
    methods
    function BMI = getBMI(height,weight)
    BMI = person.mass/person.mass^2;
    end
    end
    end
\end{matlaboutput}

\begin{matlabcode}[caption = {For educational purposes}]
    % example of while loop using placeholders
    while "\ph{condition}"
    if "\ph{something-bad-happens}"
    break
    else
    % do something useful
    end
    % do more things
    end
\end{matlabcode}

\begin{matlabcode}[caption = {Sample code from Matlab}]
    %% Code sections are highlighted.
    % System command are supported...
    !gzip sample.m
    % ... as is line continuation.
    A = [1, 2, 3,... % (mimicking the ouput is good)
    4, 5, 6]
    fid = fopen('testFile.text', 'w')
    for i=1:10
    fprintf(fid,'%6.2f \n', i);
    end
    x=1; %% this is just a comment, though
    % Context-sensitive keywords get highlighted correctly...
    p = properties(mydate); %(here, properties is a function)
    x = linspace(0,1,101);
    y = x(end:-1:1)
    % ... even in nonsensical code.
    ]end()()(((end end)end ))))end (function end
    %{
        block comments are supported
        %} even
    runaway block comments
    are
\end{matlabcode}
\begin{pseudocode}[caption={Hello world}]
    print "Hello world"
\end{pseudocode}    

\end{document}

enter image description here enter image description here enter image description here

Tom
  • 7,318
  • 4
  • 21
  • I am writing a book and I am using all those packages. Is there any package that is causing my problem from the code I provided above? If you delete some packages like you did, everything works. However, can you assist me with a solution that works when all the packages in my MWE are loaded? I want all the packages in my preamble not to be deleted please because I need a solution in the presence of all those packages. Thank you. – itc Jul 02 '22 at 09:02
  • @itc As I said, I delete all unrelated packages associate with the problem. It should work if you have other packages. Have you try the solution yet? – Tom Jul 02 '22 at 10:11
  • 1
    @itc I tested it with all your packages. It is working for me. And I post the working example with your packages. If this one is still not working for you. Just let me know. Maybe some packages you didn't include the in the example cause the problem. – Tom Jul 02 '22 at 10:38
  • when I run the last code you posted that includes all the packages, the List of Matlab Outputs, Code and Pseudocode pages are all blank. I am using TeXLive 2022 alongside Texstudio.What could be the problem? – itc Jul 02 '22 at 11:47
  • I also checked the LOC, LOR, and LOP files in the folder with my TEX file. Their size is 0Kb. – itc Jul 02 '22 at 12:03
  • the code is still not working. I copied and pasted the last code you posted and I am now getting blank List of Matlab Outputs, Codes and Pseudocodes. I am stuck. I need your assistance please. – itc Jul 02 '22 at 16:41
  • 1
    @itc I update the answer. The problem is caused by latest caption package. Now it should be working. – Tom Jul 02 '22 at 23:53
  • Thank you very much @Tom, the problem has been solved. I wouldn't have done it without your assistance. I didn't know that the updated caption package can have such effects on the listings environments. – itc Jul 03 '22 at 00:29
  • @itc You are welcome! – Tom Jul 03 '22 at 00:35
  • Dear @Tom, I have a question that I am kindly requesting for a solution at https://tex.stackexchange.com/questions/656728/classifying-matlab-code-and-pseudocodes-under-list-of-codes-and-matlab-and-r-out?noredirect=1#comment1636761_656728. I would greatly appreciate if you can offer some assistance. – itc Sep 12 '22 at 15:19