如何在附录中收集文本以供日后使用?

如何在附录中收集文本以供日后使用?

我正在编写一个\newcommand输出图像的程序。我想在附录 ( \lstinputlisting) 中包含用于制作该图像的源。我如何收集整个文档中使用的所有图像的源,然后在制作附录部分时输出它们?

我见过问题,但我梦想过类似的东西\savebox,但可以选择将我的内容添加到现有的内容中。

有什么帮助吗?:)

编辑

我最终使用了基于 Gonzo Medina 推荐的收集包的代码,请参阅 MWE:

\documentclass{article} 
\usepackage[demo]{graphicx}

\usepackage{collect}
\definecollection{myexamples}
% call like this: \example{01-start}{01-start-output-image}{My caption}
\newcommand{\example}[3]{
    \begin{figure}[htbp]
      \begin{center}
        \label{example:#1}
        \includegraphics[height=5cm]{images/#2} 
        \caption{#3}
      \end{center}
    \end{figure}
    % collecting the code for appendix:
    \begin{collect}{myexamples}{}{}
      \subsection{#1}
      Include your code for example #1 here, e.g. using the listings package.

      The example has been printed on page \pageref{example:#1}.
    \end{collect}
}

\begin{document}

\section{First Section}
\example{01-merge}{01-merge-image}{Caption1}

\newpage
\section{Second Section}
\example{02-merge}{02-merge-image}{Caption2}
\example{03-merge}{03-merge-image}{Caption3}

\newpage
\appendix
\section{Code}

\includecollection{myexamples}
\end{document}

非常感谢您的回答!

答案1

collect包在这里很有用。举个小例子:myplots定义了一个新集合;新定义的环境collplot在文档中排版其内容,并使用lstlisting环境将其存储在集合中;\includecollection{myplots}调用时,用于生成图的代码使用带有彩色背景的框架排版:

\documentclass{article}
\usepackage{collect}
\usepackage{listings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6,width=6cm}

\lstset{
  basicstyle=\small\ttfamily,
  frame=rltb,
  backgroundcolor=\color{yellow!30},
  breaklines=true,
  escapeinside=||}

\definecollection{myplots}

\makeatletter
\newenvironment{collplot}[2]
  {\@nameuse{collect*}{myplots}{\begin{tikzpicture}}{\end{tikzpicture}}{\lstlisting[caption={#1. (pag.~\pageref{#2})}]}{\endlstlisting}}
  {\@nameuse{endcollect*}}
\makeatother

\begin{document}

\section{The plots}

\begin{figure}[!ht]
\centering
\begin{collplot}{First plot}{fig:testa}
\begin{axis}|\par|
\addplot[domain=-1:100,blue] {sqrt(x)};|\par|
\end{axis}|\par|
\end{collplot}
\caption{First plot}
\label{fig:testa}
\end{figure}

\begin{figure}[!ht]
\centering
\begin{collplot}{Second plot}{fig:testb}
\begin{axis}|\par|               
\addplot[domain=-3:3,red] {x*x};|\par|
\end{axis}|\par|
\end{collplot}
\caption{Second plot}
\label{fig:testb}
\end{figure}

\appendix
\section{The Code}
\includecollection{myplots}

\end{document}

在此处输入图片描述

答案2

这是一个通过filecontents包解决的解决方案。特点:

  • 环境收集的代码coll是纯(La)TeX(|\par|与 Gonzalo Medina 的回答不同)。
  • 可以使用多个命名集合(由 创建\newcoll)。
  • 使用外部文件(\jobname-<collname>-<n>.tex)来存储每个收集到的代码块。

例子:

在此处输入图片描述

序言(带有注释的宏):

\documentclass[twocolumn]{article}

\makeatletter
% some useful packages
\usepackage{filecontents}
\usepackage{listings}
% utilities
\gdef\coll@currcoll{}
\def\coll@stepcurrcollcount{\stepcounter{coll@count\coll@currcoll}}
\def\coll@currcollcount{\csname thecoll@count\coll@currcoll\endcsname}
\def\coll@currcollshow{\csname thecoll@show\coll@currcoll\endcsname}
\def\coll@currexternfilename{\jobname-\coll@currcoll-\coll@currcollcount}
\def\coll@currlegend{\csname coll@legend\coll@currcoll\coll@currcollcount\endcsname}
\def\coll@setlegend#1{%
  \expandafter\gdef\csname coll@legend\coll@currcoll\coll@currcollcount\endcsname{#1}%
}
\def\coll@getlegend#1{\csname coll@legend\coll@currcoll#1\endcsname}
\def\coll@setref#1{%
  \expandafter\gdef\csname coll@ref\coll@currcoll\coll@currcollcount\endcsname{#1}%
}
\def\coll@getref#1{\csname coll@ref\coll@currcoll#1\endcsname}
% macro to expand collected code into a figure
\newcommand{\coll@execcoll}{%
  \begin{figure}[h!]
    \centering
    \@input{\coll@currexternfilename}%
    \caption{\coll@getlegend{\coll@currcollcount}}
    \label{\coll@getref{\coll@currcollcount}}
  \end{figure}
}
% macro to create a new collection
\newcommand\newcoll[1]{%
  \newcounter{coll@count#1}
  \setcounter{coll@count#1}{0}
  \newcounter{coll@show#1}
  \setcounter{coll@show#1}{0}
}
% environment to add an element to a collection
\newenvironment{coll}[3]{% collection, legend, ref
  \gdef\coll@currcoll{#1}%
  \coll@stepcurrcollcount%
  \coll@setlegend{#2}%
  \coll@setref{#3}%
  \@tempswafalse\filec@ntents%
  {\coll@currexternfilename}%
}
{\endfilecontents\aftergroup\coll@execcoll}
% macro to show one code from current collection
\newcommand{\coll@showonecode}[1]{
  \lstinputlisting[title={Code of figure \ref{\coll@getref{#1}}: \coll@getlegend{#1}}]%
    {\jobname-\coll@currcoll-#1.tex}%
}
% macro to show all collected codes from a collection
\newcommand{\showcoll}[1]{
  \gdef\coll@currcoll{#1}%
  \loop\ifnum\coll@currcollcount>\coll@currcollshow %
    \stepcounter{coll@show#1}
    \coll@showonecode{\coll@currcollshow}
  \repeat
}
\makeatother

该文件:

\usepackage{xcolor}
\lstset{
  basicstyle=\small\ttfamily,
  frame=rltb,
  backgroundcolor=\color{yellow!30},
  breaklines=true,
  escapeinside=||
}
\usepackage{tikz}
\begin{document}
\newcoll{code-type-1}
\newcoll{code-type-2}

\section{First section}
\subsection{a subsection}
\begin{coll}{code-type-1}{first legend}{code:one}
\begin{tikzpicture}
  \draw[fill=orange] circle(3mm);
\end{tikzpicture}
\end{coll}

\subsection{another subsection}
\begin{coll}{code-type-2}{another legend}{code:two}
\textbf{bar}
\end{coll}

\section{Second section}
\subsection{third subsection}
\begin{coll}{code-type-1}{third legend}{code:three}
\begin{tikzpicture}
  \draw[fill=blue] circle(3mm) ++(1cm,0) circle(4mm);
\end{tikzpicture}
\end{coll}

\subsection{fourth subsection}
\begin{coll}{code-type-2}{fourth legend}{code:four}
  \begin{tikzpicture}
    \fill[black] circle(5mm and 3mm);
  \end{tikzpicture}
\end{coll}

\section{The collections}
\subsection{Collected codes type 1}
\showcoll{code-type-1}

\subsection{Collected codes type 2}
\showcoll{code-type-2}
\end{document}

相关内容