如何复制列表?

如何复制列表?

我想在 Section Intro 中展示出现在 Section Listings 中的列表。因此我创建了宏,\ShowCase旨在在两个部分中展开。但这会导致错误。展望未来,计数器的问题也必须得到解决。我知道我可以覆盖计数器,但也许有一个更接近重复概念(没有副作用)的解决方案。

PS: 辅助问题:如何列出列表的标题?(见代码中的尝试)

\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}

答案1

此解决方案使用保存箱。每个列表都需要一个不同的保存箱。foo每次使用时,标签都会写入辅助文件。可以使用不同的计数器以不同的名称复制标签(或多或少)。但是,需要运行 3 次才能显示正确的数字。

\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}

答案2

这只是为了显示问题所在,并提供一种简单但远非完美的方法来处理问题。问题是清单希望看到一个明确的\end{listing},参见例如这里针对一个密切相关的问题。一个简单(但有效)的方法是摆脱\end{listing}宏定义。

\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}

有多种复杂的方法可以避免这种情况。在我看来,更简洁的方法是定义要使用的列表的样式,然后使用这些样式代替宏。

相关内容