多个列表条目分类错误

多个列表条目分类错误

我的问题如下:我有 3 个不同的列表环境,即 Matlab 输出列表、Matlab 代码列表和伪代码列表。但是,所有列表都在 Matlab 输出列表下,而代码和伪代码列表为空,这不应该是这种情况。我希望所有 Matlab 输出(例如 Matlab 输出 1.1,...)都位于 Matlab 输出列表下,所有 Matlab 代码(例如 Matlab 代码 1.1,...)都位于 Matlab 代码列表下,所有伪代码(例如伪代码 1.1,...)都位于伪代码列表下,如下图所示: 在我加载的所有软件包存在的情况下,我希望得到以下输出
我正在寻找一种解决方案,可以在所有我加载的包都存在的情况下解决问题。我怀疑有些代码行导致了这个问题,因为如果删除一些代码行,我就会得到所需的输出。我附上了一张显示我所面临问题的图片。我正在写一本书,所以请不要在提供解决方案时删除加载的包。下面是分别显示问题和我的 MWE 的图片:
我面临的问题

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

答案1

我删除了所有不相关的包,楼主应该看看如何提供一个平均能量损失。以下是一个工作示例:

编辑:这适用于最新caption软件包(2022-03-29),早期版本请使用注释行。

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

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

编辑:问题是由新caption软件包设置引起的。以下是包含所有软件包的工作示例:

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

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容