1

I want to showcase in Section Intro, a listing that appears in Section Listings. So I created macro to that effect, \ShowCase, meant to be expanded in both sections. But that causes an error. Looking up ahead, the issue of the counter will also have to be addressed. I know I could override the counter, but perhaps there is a solution that is closer to the notion of duplication (no side effect).

PS: Subsidiary question: how do I list the listings' headers? (see try in the code)

\documentclass[full]{l3doc}
\usepackage{mwe}
\usepackage{tcolorbox}
\usepackage{xparse}

\tcbuselibrary{listings, breakable}

\newtcblisting[auto counter]
{listing}[2][]
{
  noparskip,
  breakable,
  colback=white,
  colframe=black,
  opacitybacktitle=.8,%
  fonttitle=\bfseries,
  title=Listing~\thetcbcounter. #1,
  arc=0pt,
  outer arc=0pt,
  boxrule=0pt,
  #2
}

%\NewDocumentCommand{\ShowCase}
%{}
\def\ShowCase
{
  \begin{listing}[Bar]
    % {label=bar,listing only}
    {label=bar}
    \lipsum[2]
  \end{listing}
}

\begin{document}

%\tcblistof{listing}{Listings} % I just want the listings's headers, this show its contents. 

\section{Intro}\label{intro}

My favorite Listing has number~\autoref{bar}, here it is:
\ShowCase % ERROR

Youll findit again under \autoref{bar}

\section{Listings}\label{listings}

\begin{listing}[Foo]
%  {label=foo,listing only}
  {label=foo}
  \lipsum[1]
\end{listing}

%\ShowCase % ERROR

\end{document}
Erwann
  • 2,100

2 Answers2

2

This is merely to show where the problem is, and to provide a simple but far from perfect way to deal with the problem. The problem is that the listing wants to see an explicit \end{listing}, see e.g. here for a closely related problem. One simple-minded (yet working) way to go is to move \end{listing} out of the macro definition.

\documentclass[full]{l3doc}
\usepackage{mwe}
\usepackage{tcolorbox}
\usepackage{xparse}

\tcbuselibrary{listings, breakable}

\newtcblisting[auto counter]
{listing}[2][]
{
  noparskip,
  breakable,
  colback=white,
  colframe=black,
  opacitybacktitle=.8,%
  fonttitle=\bfseries,
  title=Listing~\thetcbcounter. #1,
  arc=0pt,
  outer arc=0pt,
  boxrule=0pt,
  #2
}

%\NewDocumentCommand{\ShowCase}
%{}
\def\ShowCase{\begin{listing}[Bar]
    % {label=bar,listing only}
    {label=bar}
    \lipsum[2]
}

\begin{document}

%\tcblistof{listing}{Listings} % I just want the listings's headers, this show its contents. 

\section{Intro}\label{intro}

My favorite Listing has number~\autoref{bar}, here it is:
\ShowCase % NO ERROR
\end{listing}

Youll findit again under \autoref{bar}

\section{Listings}\label{listings}

\begin{listing}[Foo]
%  {label=foo,listing only}
  {label=foo}
  \lipsum[1]
\end{listing}

\ShowCase %  NO ERROR
\end{listing}

\end{document}

There are complicated ways to avoid this. The IMHO much cleaner way will be to define styles for the listings that you want to use, and use these styles instead of macros.

2

This solution uses a savebox. You need a different savebox for every listing. The foo label will be written to the aux file each time it is used. One can duplicate the label (more or less) under a different name using a different counter. However, it takes 3 runs before the correct number shows up.

\documentclass[full]{l3doc}
\usepackage{mwe}
\usepackage{tcolorbox}
\usepackage{xparse}

\tcbuselibrary{listings, breakable}

\newtcblisting[auto counter]
{listing}[2][]
{
  noparskip,
  breakable,
  colback=white,
  colframe=black,
  opacitybacktitle=.8,%
  fonttitle=\bfseries,
  title=Listing~\thetcbcounter. #1,
  arc=0pt,
  outer arc=0pt,
  boxrule=0pt,
  #2
}

\newcounter{alt}
\newsavebox{\ListboxA}

\begin{document}

%\tcblistof{listing}{Listings} % I just want the listings's headers, this show its contents. 

\section{Intro}\label{intro}

My favorite Listing has number~\autoref{bar}, here it is:

\global\setbox\ListboxA=\hbox{%
  \begin{listing}
    % {label=bar,listing only}
    {label=foo}
    \lipsum[2]
  \end{listing}%
}\noindent\usebox\ListboxA

Youll find it again under \autoref{bar}

\section{Listings}\label{listings}

\setcounter{alt}{\getrefnumber{foo}}% create new label
\addtocounter{alt}{-1}%
\refstepcounter{alt}\label{bar}
\noindent\usebox\ListboxA

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120