恳请帮助我如何放置标题Matlab 代码和伪代码在下面代码列表和标题Matlab 输出和R输出在下面输出列表使用下面的 MWE。换句话说,我希望有两个单独的列表,即代码列表和输出列表。然而在代码列表,我想要有部分(未编号),即Matlab 代码和伪代码,以及输出列表,我也想有部分(未编号),即R输出和Matlab 输出。非常感谢您的帮助。针对我的问题的建议解决方案多种列表样式由于没有满足以下规范,因此未产生所需的输出:
- C++ 列表、R 列表和伪代码列表采用
book
文档类的章节格式,这是我不喜欢的; - C++ 列表、R 列表和伪代码列表应采用未编号部分格式,位于章节(未编号)下,名称为代码列表;
- 我还想按章节(未编号)名称创建第二个独立列表输出列表。在这个未编号的章节下,我希望有未编号的部分,即 Matlab 输出和 R 输出,以及
- 建议的解决方案在新的 caption 包下效果不佳。实际上,在 @Tom 指出新
caption
包的问题之前,我已经尝试过建议的解决方案,但未能获得所需的输出多个列表条目分类错误。
如果其他用户停止建议解决方案,我将不胜感激多种列表样式因为它不能解决我的问题,正如我上面提到的。我可能没有很好地解释我的情况,如果有人能改进这篇文章,让其他用户不会认为它是重复的或投票关闭它,我将不胜感激。据我所知,Tex.SE 旨在为那些遇到 Latex 问题的人提供帮助,因为他们尝试了一些没有奏效的方法,而不是关闭需要解决方案的问题。如果我能得到我的问题的解决方案,我也会很感激。
我的 MWE 考虑了caption
包更新:
\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}
答案1
经过反复试验后,我想我已经找到了解决第一个问题的方法。
关于你的第三和第四点,我已经做了以下工作:
我尝试过实现这个答案将代码列表和输出列表设为节而不是章节,如您在第一点中指定的那样,但没有成功。我希望这个答案至少在某种程度上对您有所帮助。
终于,我看见了同一问题的另一个答案,令人惊讶的是它似乎有效!请检查我更新的代码。一定有办法简化它,但我认为它工作得很好。
较新的代码:
\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}
早期代码:
\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}