拆分任意编号项的参考计数器(如子方程式)

拆分任意编号项的参考计数器(如子方程式)

不确定如何以其他方式表述这一点:在下面的 MWE 中,我可以使用{subequations}amsmath因此我得到两个具有相同方程编号的方程,并且添加了“a”和“b” - 并且我可以同时引用“主”方程编号以及“a”和“b”部分分别。

我的问题是:我怎样才能对其他类型的 Latex 编号项目执行此操作;在下面的 MWE 的情况下,即{lstlisting}?这是 MWE 的输出:

测试02a.png

... 并且我希望生成“Listing 1a”和“Listing 1b”,而不是“Listing 1”和“Listing 2”;并且能够插入对“主列表 1”的引用,以及内部的“... 1a”和“... 1b”。代码如下:

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{xcolor} % \pagecolor
\usepackage{hyperref}
\pagecolor{yellow!15}

\begin{document}

Hello...

\begin{subequations}\label{eq:ex1}
\begin{minipage}{0.45\textwidth}
\begin{align}
\label{eq:ex1a}
a = b + c
\end{align}
\end{minipage}
\begin{minipage}{0.45\textwidth}
\begin{align}
\label{eq:ex1b}
x = y + z
\end{align}
\end{minipage}
\end{subequations}

\vspace{\baselineskip}
Ref'ing: master eq.~\ref{eq:ex1};
inner a eq.~\ref{eq:ex1a}, inner b eq.~\ref{eq:ex1b} ...

\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1a,
columns=fixed,
frame=tlrb]
080484c4 <list>:
 80484c4: cmd one
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1b,
columns=fullflexible,
% basewidth=\tlen,
frame=tlrb]
080484c4 <list>:
 80484c4: cmd one
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}

Ref'ing:
inner a listing~\ref{lst:ex1a}, inner b listing~\ref{lst:ex1b} ...

\end{document}

答案1

复制以下内容subequations,并进行一些修改hyperref

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage[colorlinks]{hyperref}

\makeatletter
\newcounter{parentlstlisting}% Counter for ``parent equation''.
\newenvironment{sublstlisting}{%
  \refstepcounter{lstlisting}%
  \protected@edef\theparentlstlisting{\thelstlisting}%
  \setcounter{parentlstlisting}{\value{lstlisting}}%
  \setcounter{lstlisting}{0}%
  \ifdefined\theHlstlisting
    \def\theHlstlisting{\theparentlstlisting\alph{lstlisting}}%
  \fi
  \def\thelstlisting{\theparentlstlisting\alph{lstlisting}}%
  \ignorespaces
}{%
  \setcounter{lstlisting}{\value{parentlstlisting}}%
  \ignorespacesafterend
}
\makeatother


\begin{document}

Hello...

\begin{subequations}\label{eq:ex1}
\begin{minipage}{0.45\textwidth}
\begin{align}
\label{eq:ex1a}
a = b + c
\end{align}
\end{minipage}
\begin{minipage}{0.45\textwidth}
\begin{align}
\label{eq:ex1b}
x = y + z
\end{align}
\end{minipage}
\end{subequations}

\vspace{\baselineskip}
Ref'ing: master eq.~\ref{eq:ex1};
inner a eq.~\ref{eq:ex1a}, inner b eq.~\ref{eq:ex1b} ...

\begin{sublstlisting}
\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1a,
columns=fixed,
frame=tlrb]
080484c4 <list>:
 80484c4: cmd one
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1b,
columns=fullflexible,
% basewidth=\tlen,
frame=tlrb]
080484c4 <list>:
 80484c4: cmd one
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\end{sublstlisting}

Ref'ing:
inner a listing~\ref{lst:ex1a}, inner b listing~\ref{lst:ex1b} ...

\end{document}

在此处输入图片描述

相关内容