0

I wish to copy the content of an environment into a macro (or into a temp file). Previously I use the following code:

\usepackage{environ}
\def\SavedContent{}
\NewEnviron{savecontent}{%
    \BODY%
    % \global\let\SavedContent\BODY%
    \expandafter\gdef\expandafter\SavedContent\expandafter{\BODY}
}

However, this method doesn't work if there's \verb or \lstinline inside the environment.

Later I discovered this question. An answer describes a working way as:

... implement an environment that reads its body verbatimized and then within a macro definition wraps things ...

Unfortunately, the answer is too complicated for me to understand. Thus I would like to ask: how should one define the savecontent environment correctly?

Add: The content doesn't have to be stored into a macro. It would also work if one can save the content into a file and read it later. How could one achieve this?

The motivation of this question is that I want to save the content of the abstract and show it later.

Below is a MWE:

\documentclass{article}

\usepackage{environ}

\def\SavedContent{} \NewEnviron{savecontent}{% \BODY% % \global\let\SavedContent\BODY% \expandafter\gdef\expandafter\SavedContent\expandafter{\BODY} }

\begin{document}

\begin{savecontent} Some text \end{savecontent} \SavedContent

% \begin{savecontent} % \verb|code| % \end{savecontent} % \SavedContent

\end{document}

Jinwen
  • 8,518

2 Answers2

1

If you're free to use LuaLaTeX, the following solution may be of interest to you. It's a slightly simplified version of this answer. Just copy the code between the lines %%% >>>>> and %%% <<<<< to your preamble, surround the verbatim material you wish to save with \Verbatim and \EndVerbatim lines, and use \printverbatim and \useverbatim as needed.

In the earlier answer, I provide a lot more background information about how this approach works and why the code is "bullet proof", in the sense that the verbatim material can be pretty much anything.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{lipsum}  % for filler text

%%% >>>>> \usepackage{luacode} % for "luacode" environment %% Lua-side code: define 1 Lua table and 3 Lua functions \begin{luacode} verb_table = {} function store_lines ( s ) if s:find ( "\EndVerbatim" ) then luatexbase.remove_from_callback ( "process_input_buffer" , "store_lines") else table.insert ( verb_table , s ) end return "" end function register_verbatim () verb_table = {} luatexbase.add_to_callback( "process_input_buffer" , store_lines , "store_lines") end function print_lines ( catcode ) if catcode then tex.print ( catcode , verb_table) else tex.print ( verb_table ) end end \end{luacode}

%% TeX-side code: define several LaTeX macros \def\Verbatim{\directlua{register_verbatim()}} \def\useverbatim{\directlua{print_lines()}} \def\printverbatim{% \par \bgroup \setlength{\parindent}{0pt} \ttfamily \directlua{print_lines(1)} \egroup } \def\createcatcodes{% \bgroup \catcode\\=12 \catcode{=12 \catcode\}=12 \catcode$=12 \catcode\&amp;=12 \catcode^^M=13 \catcode\#=12 \catcode^=12 \catcode\_=12 \catcode\ =13 \catcode\~=12 \catcode%=12 \savecatcodetable 1 % '\savecatcodetable' is a LuaTeX primitive \egroup} \createcatcodes \def\Space{ } \bgroup \catcode\^^M=13\gdef^^M{\quitvmode\par}% \catcode\ = 13\gdef {\quitvmode\Space}% \egroup %%% <<<<<

\begin{document}

\Verbatim \begin{abstract} \noindent \lipsum[1][1-7] % output first 7 sentences of first para \end{abstract} \EndVerbatim

\hrule\smallskip

\printverbatim

\smallskip\hrule

\useverbatim

\hrule \end{document}

Mico
  • 506,678
0

Below is a partial solution, only saves the content of the last savecontent environment. The savecontent environment saves its content to a file \jobname.mytmp, the macro \SavedContent then read this file as input. It uses \VerbatimOut of fancyvrb, see this answer of @egreg.

If one wish to save the content in all of the savecontent environments, one may consider using a counter to mark the corresponding environment.

\documentclass{article}

\usepackage{fancyvrb} \def\SavedContent{\input{\jobname.mytmp}} \newenvironment{savecontent}{% \VerbatimOut{\jobname.mytmp}% }{ \endVerbatimOut% }

\begin{document}

\begin{savecontent} Some text \end{savecontent} \SavedContent

\begin{savecontent} \verb|code| \end{savecontent} \SavedContent

\end{document}

Jinwen
  • 8,518