# Automatic section body generation using text extracted from the rest of the document [duplicate]

Possible Duplicate:
Extract all emphasized words from latex document

I'm writing a report which involves a comparative analysis of some standards. Throughout the body of my report, I have a detailed comparison of minor and major issues. However, at the end of the document, I want a separate section which will only highlight the major differences, for someone who doesn't have time to go through the detailed report.

For this, I basically want to extract specific lines from the rest of the document and insert them in my final section. But I want this insertion to be automatic, such that when I mark a particular line of text in my document, it will automatically get added to this final section, i.e. something that works like automatic Table of Content generation based on the section names.

I don't know how else to phrase this question. Any help would be much appreciated.

-

## marked as duplicate by Werner, Kurt, Martin Schröder, lockstep, percusseNov 8 '12 at 0:17

This question is very similar to Extract all emphasized words from latex document. Please take a look at it as the information there might help you. If so, that's great, and we'll probably close this question as a duplicate just to keep the place tidy and to help people find answers quickly. If not, please edit your question here to explain why so that people can better focus their attention to help you. –  Werner Nov 7 '12 at 18:23
Adding to @Werner's comment: Another such similar question is tex.stackexchange.com/questions/14393/… –  doncherry Nov 7 '12 at 18:32

\documentclass{article}
\newtoks\majortoks

\newcommand{\majorissue}[1]{%
\global\majortoks=\expandafter{\the\majortoks\issue{#1}}%
#1}

\newcommand{\printmajorissues}{%
\section*{Summary of major issues}
\begin{itemize}
\the\majortoks
\end{itemize}
}

\newcommand{\issue}[1]{\item #1}

\begin{document}

\section{First}

This is a prolog. Next we describe the first major issue, that is about
walking \majorissue{barefoot in the park}. A minor issue is not having an
elevator.

\section{Second}

A meaningless text. The second major issue is that \majorissue{some like it hot}.
A minor issue is that nobody's perfect.

\printmajorissues

\end{document}


We collect all we want to typeset at the end in a container (formally, the token register \majortoks). When \majorissue{some text} is found, the macro adds to the container the tokens

\issue{some text}


and also prints "some text" at the point.

TeXnically, in that case we'd execute

\global\majortoks=\expandafter{\the\majortoks\issue{some text}}


so the register is incremented globally, independent of the grouping structure. With \the\majortoks the previous contents of the register is delivered before assigning the new value, thus really doing an incremental update.

Finally, the macro \printmajorissues delivers the contents so accumulated. In my example the contents is delivered in a itemize environment and \issue is defined to give, for instance

\item some text


but you are free to do what you want with it.

Plan carefully what you need and well designed macros will help better.

-
This is fantastic! Thanks a lot @egreg –  Chatterjee Nov 7 '12 at 18:46
@Chatterjee Please, don't be shy in asking for other features. –  egreg Nov 7 '12 at 19:01

One approach to this would be to use the aux file method that LaTeX provides. Below is a simple example that simply collects all desired text in a new aux files with extension .sum and reads that back in. Advantage is that the data is not kept in memory and you can have the summary anywhere in the document, but the disadvantage then is (of course) that you need two runs to get it formatted even if the summry is at the very end.

I've thrown in an itemizeenvironment to format the entries, but effectively any other approach could be used.

\documentclass{article}

\makeatletter
\newcommand\mysummary{%
\begin{itemize}%
\@starttoc{sum}%
\@newlistfalse           %we don't want list errors if there are no items
\end{itemize}%
}

\newcommand\summarytext[1]{%
\leavevmode
{%
\protect\item % standard formatting for each entry could go here
#1%
\par             % final formatting for each entry could go here
}%
#1%               % process the text in mid document
}
\makeatother

\begin{document}

some text \summarytext{first text} some more text

\summarytext{second text with a \par paragraph break} and some more text

\mysummary

\end{document}


As a result you get

As slightly more elaborate solution would be to use \addcontentline in which case you would be able to format the data with page numbers referring back to the source.

-