这个问题主要涉及多种列表样式。如果我\usepackage{caption}
在文件头部有定义,自定义列表就会消失。如何让它工作?
\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
\let\contentsname\lstlistcplusplusname
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
\tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
\renewcommand{\lstlistingname}{C++ Code}%
\xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
\lstset{language=C++,#1}}
{}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
\let\contentsname\lstlistrcodename
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
\tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
\renewcommand{\lstlistingname}{R 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
\begin{document}
\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode
\begin{cplusplus}[caption={Hello world}]
// 'Hello World!' program
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
\end{cplusplus}
\begin{rcode}[caption={Hello world}]
cat('Hello, world!\n')
\end{rcode}
\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}
\end{document}
答案1
软件包caption
需要修补\lst@MakeCaption
,以使listings
软件包适应软件包的功能caption
。它将的原始定义存储(并使用)\lst@MakeCaption
到名为的宏中\caption@ORI@lst@MakeCaption
,因此要使您的解决方案有效,需要修补此宏:
\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
\let\contentsname\lstlistcplusplusname
\let\lst@temp@starttoc \def@starttoc##1{\lst@temp{loc}}%
\tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
\renewcommand{\lstlistingname}{C++ Code}%
\xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{loc}{}{}%
\lstset{language=C++,#1}}
{}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
\let\contentsname\lstlistrcodename
\let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lor}}%
\tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
\renewcommand{\lstlistingname}{R Code}%
\xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
\xpatchcmd*{\caption@ORI@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}{}{}%
\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lop}{}{}%
\lstset{basicstyle=\ttfamily,#1}}
{}
\makeatother
\begin{document}
\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode
\begin{cplusplus}[caption={Hello world}]
// 'Hello World!' program
include
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
\end{cplusplus}
\begin{rcode}[caption={Hello world}]
cat('Hello, world!\n')
\end{rcode}
\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}
\end{document}
遗憾的是,该listings
功能没有定义和使用\def\ext@lstlisting{lol}
,就像所有其他提供“列表”功能的环境一样。在这种情况下,重新定义\ext@lstlisting
就足够了,无论有没有caption
包(或任何其他包修补\lst@MakeCaption
)。
答案2
作为该caption
软件包的替代方案,我capt-of
在加拿大运输安全局。当我使用这个包时一切都运行顺利。
\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{capt-of}
\usepackage{graphicx}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
\let\contentsname\lstlistcplusplusname
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
\tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
\renewcommand{\lstlistingname}{C++ Code}%
\xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
\lstset{language=C++,#1}}
{}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
\let\contentsname\lstlistrcodename
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
\tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
\renewcommand{\lstlistingname}{R 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
\begin{document}
\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode
\begin{cplusplus}[caption={Hello world}]
// 'Hello World!' program
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
\end{cplusplus}
\begin{rcode}[caption={Hello world}]
cat('Hello, world!\n')
\end{rcode}
\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}
\begin{figure}[h]
\centering
\includegraphics[scale=0.5]{example-image-a}
\captionof{figure}{Example image a.}
\end{figure}
\end{document}