6

I'd like to roll my own commands that achieve something similar, in principle, to how \index places the indexed term in a later section, or how footnotes place text in a footer.

\summaryandextended{
    I'm just a summary of a result, and should appear in main text.
}
{
    I'm a long, complicated derivation of the result above. 

    I belong in the back portion of the book, read only by the nerdiest of nerds.
}

\gatheredappendix % I collect all of the #2 arguments and display them.

I don't want to duplicate either indexing or footnotes per se; I'm curious how one could make commands that build arbitrary collections of content within a specified section of a document.

4 Answers4

5

This task is simply solvable by TeX primitives. You needn't any special package.

\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\gatheredappendix{}
\long\def\summaryandextended#1#2{#1\global\addto\gatheredappendix{#2\par}}

Really, you need only these three lines (independent of used format). On the other hand, when \usepackage{xparse} is used then TeX needs to read 21711 lines of code.

wipet
  • 74,238
4

This is a expl3 syntax way: Store the second argument for later use on seq variable and display it later on with \gatheredappendix.

This way no external file is necessary (as would be for \index)

\documentclass{article}

\usepackage{xparse}



\ExplSyntaxOn
\seq_new:N \l_gather_stuff_seq
\NewDocumentCommand{\storesecondarg}{+m}{%
  \seq_put_right:Nn \l_gather_stuff_seq {#1}
}
\NewDocumentCommand{\gatheredappendix}{}{%
  \seq_use:Nn \l_gather_stuff_seq {\par}
}
\ExplSyntaxOff

\NewDocumentCommand{\summaryandextended}{+m+m}{%
  #1%
  \storesecondarg{#2}%
 }


\begin{document}

\summaryandextended{
    I'm just a summary of a result, and should appear in main text.
}
{
    I'm a long, complicated derivation of the result above. 

    I belong in the back portion of the book, read only by the nerdiest of nerds.
}

\summaryandextended{
  Other stuff
}
{
  Other useless stuff

  Even more useless text
}

\vskip\baselineskip
\hrule

\vskip\baselineskip

\gatheredappendix % I collect all of the #2 arguments and display them.


\end{document}

enter image description here

4

Nothing really complicated: it's a matter of augmenting the definition of a macro:

\makeatletter
\newcommand{\summaryandextended}[2]{%
  #1% typeset the first argument
  \g@addto@macro\sean@extensions{\sean@extension{#2}}%
}
\gdef\sean@extensions{}% initialize

\newcommand{\gatheredappendix}{\sean@extensions}
\newcommand{\sean@extension}[1]{#1\par}% or whatever formatting you'd like
\makeatother

Note that, instead of just pushing the second argument in the replacement text of \sean@extensions, I add some structure making it the argument to \sean@extension that can then be used to specially format the texts. For instance, you may want to use an itemized list. In this case, change the definitions for \gatheredappendix and \sean@extension like

\newcommand{\gatheredappendix}{%
  \begin{itemize}
  \sean@extensions
  \end{itemize}
}
\newcommand{\sean@extension}[1]{\item #1}
egreg
  • 1,121,712
3

You could do the following:

  1. In your preamble, set \gatheredappendix to the empty string:

    \newcommand{\gatheredappendix}{}
    
  2. Then define a command \summaryandextended with two arguments that does the following:

    a) Redefine \gatheredappendix by adding the second argument #2 (see Appending to Variables or other places at tex.stackexchange)

    b) Output the first argument #1.

Of course, \summaryandextended could do additional things, like adding a new subsection title or so whenever content is added to \gatheredappendix.

jarauh
  • 2,762
  • 15
  • 27