# Create summary section from marked text at the end of chapter/section?

I am trying to automatically create a summary section/subsection at the end of a chapter/section, by marking text within the main text as part of the summary. Is there a package that does that?

I am trying to do this manually. The text in the summary section/subsection is copied from marked text within the main text. I used the collect package to do that (inspired from Gonzalo's answer).

I'm having a few issues so far:

• Text within the collect* environment is placed within its own paragraph in the main text.
• The beforecol and aftercol parameters of the collect package are separated from the text entry with a \par token, even when I do not provide it explicitly. I do not want that. I want them to be part of the same paragraph, unless explicitly stated.
• If I do not provide \par at the end of aftercol, the aftercol content and begincol content of the subsequent collection are merged into one.
• I am not sure how to use \includecollection in an enumerate environment such that each \begin{collect*} ... \end{collect*} becomes an \item in enumerate. Right now, the whole collection is treated as one big \item.
• Automation. I guess what I am really looking for is the ability to use something like \summary[Before text.][After text.]{Text within main text} which will typeset and collect the text within main text normally, collect the before after parts, and produce a summary section/subsection at the end of every chapter/section, where every \summary is an \item in an enumerate environment. This is the least of my concerns though. :-)

Below is what I have so far. The second summary subsection is what I would like to get.

# MWE:

\documentclass{article}
\usepackage{collect}
\usepackage{multicol}

%% Gonzalo's answer: http://tex.stackexchange.com/a/166978/32206

\makeatletter
\newenvironment{summary}[2]
{\@nameuse{collect*}{thesummary}{}{}{{#1}}{{#2}}}
{\@nameuse{endcollect*}}
\makeatother

\definecollection{thesummary} % Collect summary content here.

\begin{document}

\section{El Section}
% This part should be in the summary, with some text before.
\begin{summary}{Some text before.}{}
Far quitting dwelling graceful the likewise received building. An fact so to that show am shed sold cold.
\end{summary}
Unaffected remarkably get yet introduced excellence terminated led. Result either design saw she esteem and. On ashamed no inhabit ferrars it ye besides resolve. Own judgment directly few trifling.
% This part should be in the summary, with some text after.
\begin{summary}{}{Some text after.}
Elderly as pursuit at regular do parlors. Rank what has into fond she.
\end{summary}
%% End of paragraph 1.

% This part should be in the summary, with some text before and after.
\begin{summary}{Some text before.}{Some text after.}
Lose away off why half led have near bed. At engage simple father of period others except.
\end{summary}
My giving do summer of though narrow marked at. Spring formal no county ye waited.
%% End of paragraph 2.

% Generate summary subsection. Split into two columns and enumerate.
\subsection*{Summary}
\begin{multicols}{2}
\begin{enumerate}
\item \includecollection{thesummary}
\end{enumerate}
\end{multicols}

% Ideal outcome.
\subsection*{Summary}
\begin{multicols}{2}
\begin{enumerate}
\item Some text before. Far quitting dwelling graceful the likewise received building. An fact so to that show am shed sold cold.
\item Elderly as pursuit at regular do parlors. Rank what has into fond she. Some text after.
\item Some text before. Lose away off why half led have near bed. At engage simple father of period others except. Some text after.
\end{enumerate}
\end{multicols}

\end{document}


# Output:

-

## 2 Answers

It is not exactly what you asked, but probably you can adapt it to your needs:

\documentclass{article}
\usepackage{framed, blindtext}

%%%%%%%%%%%%%%%%
\newcommand\lorname{\mdseries\textit{List of recommendations:}}%list of recommendations
\newcommand\lorext{lor} % another aux-file is needed
\newcommand\lortmp{}
\newcounter{recommendations}
\newcommand{\Empf}[2][\empty]{%
\textit{Recommendations
\refstepcounter{recommendations}\therecommendations{}:}%
\vspace{-0.75\baselineskip} \begin{framed} #2 \end{framed}%
\addcontentsline{\lorext}{subsection}{\therecommendations\quad\lortmp}
}
\makeatletter
\newcommand\listofrecommendations{%
\footnotesize
\section*{\lorname}
\markboth{\lorname}{}
\@starttoc{\lorext}
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{whatever}
\label{sec:whatever}

\blindtext
\Empf{Remember this}
\blindtext
\Empf{Remember that}
\blindtext

\listofrecommendations{}

\end{document}


-
This is all Chinese to me! :-) Thanks for your help, but I have managed to achieve something closer to the desired outcome. I've just added my answer. –  sudosensei Apr 3 at 16:41

I found that most of my issues could be solved by renewing the collect* environment and (a) disabling the use of the preenv and postenv arguments; and (b) removing the \par token at the end of the environment which meant that every call to collect* was placed within its own paragraph.

I probably broke every rule in the book, but it'll have to do for now!

# Renewed collect*:

%% Renew collect* environment to ignore preenv and postenv, both in
%% the file collected and in the file typeset. Also, remove final \par
%% so that the environment content is not necessarily in its own paragraph.
\makeatletter
\renewenvironment{collect*}[5]{%
\global\toks@{}%
\def\CE@file{#1}%
\def\CE@preenv{#2}%
\def\CE@postenv{#3}%
\def\CE@prefileenv{#4}%
\def\CE@postfileenv{#5}%
\CE@get@env@body@start%
}{%
\CE@ensure@opened{\CE@file}%
\edef\@tempa{\csname CE@@\CE@file @out\endcsname}%
\immediate\write\@tempa{\CE@meaning\CE@prefileenv}%
% \immediate\write\@tempa{\CE@meaning\CE@preenv}%
\immediate\write\@tempa{\the\toks@}%
% \immediate\write\@tempa{\CE@meaning\CE@postenv}%
\immediate\write\@tempa{\CE@meaning\CE@postfileenv}%
\immediate\openout\CE@tmp@out=\jobname.tmp%
% \immediate\write\CE@tmp@out{\CE@meaning\CE@preenv}%
\immediate\write\CE@tmp@out{\the\toks@}%
% \immediate\write\CE@tmp@out{\CE@meaning\CE@postenv}%
\immediate\closeout\CE@tmp@out%
\@input{\jobname.tmp}%
% \par%
}
\makeatother


# New summary environment:

Then, we can create our own environment and collection that takes the beforecol and aftercol arguments. Credits to Gonzalo for his excellent answer.

\makeatletter
\newenvironment{summary}[2]
{\@nameuse{collect*}{thesummary}{}{}{\item #1}{#2}}
{\@nameuse{endcollect*}}
\makeatother

\definecollection{thesummary} % Collect summary content here.


# Summary in enumerate:

By preceding the beforecol argument (#1) with \item, the collected data are suitable for use in \enumerate and \itemize environments, like so:

  \begin{enumerate}
\includecollection{thesummary}
\end{enumerate}


# Solution:

\documentclass[a4paper,10pt]{article}
\usepackage{multicol}
\usepackage[draft]{graphicx}
\usepackage[colorlinks,linkcolor={blue}]{hyperref}
\usepackage[nameinlink,capitalise,noabbrev]{cleveref}

\usepackage{collect}

%% Renew collect* environment to ignore preenv and postenv, both in
%% the file collected and in the file typeset. Also, remove final \par
%% so that the environment content is not necessarily in its own paragraph.
\makeatletter
\renewenvironment{collect*}[5]{%
\global\toks@{}%
\def\CE@file{#1}%
\def\CE@preenv{#2}%
\def\CE@postenv{#3}%
\def\CE@prefileenv{#4}%
\def\CE@postfileenv{#5}%
\CE@get@env@body@start%
}{%
\CE@ensure@opened{\CE@file}%
\edef\@tempa{\csname CE@@\CE@file @out\endcsname}%
\immediate\write\@tempa{\CE@meaning\CE@prefileenv}%
% \immediate\write\@tempa{\CE@meaning\CE@preenv}%
\immediate\write\@tempa{\the\toks@}%
% \immediate\write\@tempa{\CE@meaning\CE@postenv}%
\immediate\write\@tempa{\CE@meaning\CE@postfileenv}%
\immediate\openout\CE@tmp@out=\jobname.tmp%
% \immediate\write\CE@tmp@out{\CE@meaning\CE@preenv}%
\immediate\write\CE@tmp@out{\the\toks@}%
% \immediate\write\CE@tmp@out{\CE@meaning\CE@postenv}%
\immediate\closeout\CE@tmp@out%
\@input{\jobname.tmp}%
% \par%
}
\makeatother

%% Gonzalo's answer: http://tex.stackexchange.com/a/166978/32206

\makeatletter
\newenvironment{summary}[2]
{\@nameuse{collect*}{thesummary}{}{}{\item #1}{#2}}
{\@nameuse{endcollect*}}
\makeatother

\definecollection{thesummary} % Collect summary content here.

\begin{document}

\section{El Section}
% This part should be in the summary, with some text before.
\begin{summary}{Text before.}{}
Far away whatever it was quitting dwelling graceful the likewise received building. An fact so to that show am shed sold cold.
\end{summary}
Unaffected remarkably get yet introduced excellence terminated led. Result either design saw she esteem and. On ashamed no inhabit ferrars it ye besides resolve. Own judgment directly few trifling.
% This part should be in the summary, with some text after.
\begin{summary}{}{See \cref{fig:elfigure}.}
Elderly as pursuit at regular do parlors. Rank what has into fond she.
\end{summary}

\begin{figure}
\centering
\includegraphics[width=0.3\textwidth]{./figure.pdf}
\caption{El caption}
\label{fig:elfigure}
\end{figure}

% This part should be in the summary, with some text before and after.
\begin{summary}{Text before.}{Text after.}
Lose away off why half led have near bed. At engage simple father of period others except.
\end{summary}
My giving do summer of though narrow marked at. Spring formal no county ye waited.

% Generate summary subsection. Split into two columns and enumerate.
\subsection*{Summary}
Actual outcome:
\begin{multicols}{2}
\begin{enumerate}
\includecollection{thesummary}
\end{enumerate}
\end{multicols}

\subsection*{Summary}
Ideal outcome:
\begin{multicols}{2}
\begin{enumerate}
\item Text before. Far away whatever it was quitting dwelling graceful the likewise received building. An fact so to that show am shed sold cold.
\item Elderly as pursuit at regular do parlors. Rank what has into fond she. See \cref{fig:elfigure}.
\item Text before. Lose away off why half led have near bed. At engage simple father of period others except. Text after.
\end{enumerate}
\end{multicols}

\end{document}

-