4

Not sure how to otherwise formulate this: in the MWE below, I can use {subequations} from amsmath, so I get two equations with the same equation number, and "a" and "b" added instead - and I can refer both to the "master" equation number, and to the "a" and "b" parts individually.

My question is: how could I do this with other types of Latex-numbered items; in the case of MWE below, that being {lstlisting}? This is the output of the MWE:

test02a.png

... and I would like to have "Listing 1a" and "Listing 1b" generated, instead of "Listing 1" & "Listing 2"; and ability to insert refs to "master listing 1", as well as the inner "... 1a" and "...1b" ones. Here is the code:

\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 Answer 1

3

Copy what subequations does, with some modifications for 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}

enter image description here

4
  • 1
    I was a bit late, but didn't see any problem without redefining \theHlstlisting.
    – Werner
    Jul 19, 2014 at 20:57
  • 1
    @Werner I did see them. It doesn't harm, anyway.
    – egreg
    Jul 19, 2014 at 20:58
  • Thanks for that @egreg - I tried making a generic "subanything" macro and posted it below; but I cannot get rid of a csname definition error, so it looks like I'll be using your solution. Cheers!
    – sdaau
    Jul 19, 2014 at 21:32
  • @Werner hyperref knows about subequations and essentially does the same with \theHequation as I do manually for \theHlstlisting.
    – egreg
    Jul 20, 2014 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .