# Making the abstract text available as a macro

Is there an easy way to make the abstract's text available in a macro, say \theabstract? I can do this with the following code:

\documentclass{article}
\usepackage{collect}

\title{A test document}
\author{John Smith}
\date{\today}

\begin{document}
\maketitle
\begin{abstract}
\begin{collectinmacro}{\theabstract}{}{}
This is a test.
\end{collectinmacro}
\theabstract
\end{abstract}

The abstract is: \theabstract

\end{document}


However, I would prefer for the abstract code to be simple, i.e.:

\begin{abstract}
This is a test.
\end{abstract}


I have tried without success using \AtBeginEnvironment and \AtEndEnvironment from etoolbox (though I don't quite understand how they work), and also redefining the abstract environment. Both return the error ! File ended while scanning use of \@tempa.. Minimum working example:

\documentclass{article}
\usepackage{collect,etoolbox}

\title{A test document}
\author{John Smith}
\date{\today}

\AtBeginEnvironment{abstract}{\begin{collectinmacro}{\theabstract}{}{}}
\AtEndEnvironment{abstract}{\end{collectinmacro}\theabstract}
% ! File ended while scanning use of \@tempa.

%\renewenvironment{abstract}{%
%\begin{collectinmacro}{\theabstract}{}{}
%}{%
%\end{collectinmacro}
%\theabstract
%}
% ! File ended while scanning use of \@tempa.

\begin{document}
\maketitle
\begin{abstract}
This is a test.
\end{abstract}

The abstract is: \theabstract

\end{document}


While I am using the collect package above I'm open to solutions without this package too. I typically use LuaLaTeX so a LuaTeX solution would be welcome.

(It is of course also possible to simply write \newcommand{\theabstract}{This is a test} and then put \theabstract in the abstract environment, but ultimately I'd like the code for the abstract environment to be the same as normal.)

With the environ package.

\documentclass{article}
\usepackage{environ}

\title{A test document}
\author{John Smith}
\date{\today}
\let\svabstract\abstract
\let\svendabstract\endabstract
\RenewEnviron{abstract}{
\svabstract\BODY\svendabstract
\expandafter\gdef\expandafter\theabstract\expandafter{\BODY}
}
\begin{document}
\maketitle
\begin{abstract}
This is a test.
\end{abstract}

The abstract is: \theabstract
\end{document}


And not that this approach is in any way preferable to that given above, but I wanted to make sure I could do it with a tokcycle pseudo-environment, which requires a slightly different invocation syntax. The tokcycle approach might only prove advantageous if one wanted an automatic adjustment of the abstract content, such as this:

\documentclass{article}
\usepackage{tokcycle}

\title{A test document}
\author{John Smith}
\date{\today}
\let\svabstract\abstract
\let\svendabstract\endabstract
\xtokcycleenvironment\abstract
{\processtoks{##1}}
{\tcafterenv{\expandafter\def\expandafter\theabstract
\expandafter{\the\cytoks}}\def\capnext{T}}

\begin{document}
\maketitle
\abstract
This is a test to see if the abstract is automatically adjusted
to my specifications.

Multi-paragraphs.
\endabstract

The abstract is: \theabstract
\end{document}


• Thanks! I knew there had to be a better way to redefine the environment! – Ben Trettel May 9 at 19:20
• @BenTrettel You are welcome. As a bonus, I added an alternate approach. – Steven B. Segletes May 9 at 19:26

With a fairly recent version of LaTeX, that incorporates facilities of environ, but making the management easier and has robust ways to copy commands.

\documentclass{article}

\NewCommandCopy{\originalabstract}{\abstract}
\NewCommandCopy{\originalendabstract}{\endabstract}

\RenewDocumentEnvironment{abstract}{+b}
{% save the contents
\gdef\theabstract{#1}%
% now typeset the abstract
\originalabstract
#1%
}
{% finish
\originalendabstract
}

\begin{document}

\title{A test document}
\author{John Smith}
\date{\today}

\maketitle

\begin{abstract}
This is a test.
\end{abstract}

The abstract is: \theabstract

\end{document}