我想标记并引用修改后的列表环境,即 Matlab Code 和 Matlab Output。但是,当我添加命令\label
并尝试引用标记的修改列表环境时,我得到的??
是我不想要的结果。下面是我的 MWE,这是 @Tom 针对我的问题提供的解决方案,位于多个列表条目分类错误:
\documentclass[openany]{book}
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\usepackage{listings}
\usepackage{hyperref}
% --------------------------------------- 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}]\label{outp:1}
% example matlab output
\end{matlaboutput}
\begin{matlabcode}[caption = {For educational purposes}]\label{code:1}
% example matlab code 1
\end{matlabcode}
See Output \ref{outp:1} and Code \ref{code:1} above.
\end{document}
答案1
您应该将label
参数用于 listings 环境,而不是尝试在其中设置\label
。后者可以做到,但必须正确转义,并且通常是指行号而不是整个环境。
\documentclass[openany]{book}
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\usepackage{listings}
\usepackage{hyperref}
% --------------------------------------- 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},label={outp:1}]
% example matlab output
\end{matlaboutput}
\begin{matlabcode}[caption = {For educational purposes},label={code:1}]
% example matlab code 1
\end{matlabcode}
See Output \ref{outp:1} and Code \ref{code:1} above.
\end{document}