# copy and reuse the content of an environment

is there a way to embed an text in several environments (or structures, \chapter, \section etc.) in one file and "copy"/insert the content of single environment items in other files? i've noticed the clipboard and extract packages and had the thought about \def and \newcommand. but those aren't satisfying, mostly because it seems unaesthetic to me; I just want ordinary environments.

The extract package just places the content into a new file, which is what I seek to avoid. Consider an huge collection of aphorisms, quotes or equations (and others, so the solution should be abstract enough to handle different content, I guess environments would fit well). I just don't want a file for each single item, it would be far too many.

So something like "I want to use the environment x, this x shall be named x1, that x2 the other x3", as I would do with the extract package would be pleasant, just I need now access to these x1, x2 and x3 instead of a file. Something like "here I want to place a copy of x1, there of x2, here of x1 again, there of x3".

So as example I want something like:

quotesFile.tex:

\begin{myquote} % any environment
\contentName{quote1} % define some kind of identifier
% for this environments content
This is a quote
\end{myquote}

\begin{myquote}
\contentName{quote2}
lorem ipsum dolor sit amet
\end{myquote}

\begin{myquote}
\contentName{quote3}
foo bar baz foobar foobaz
\end{myquote}


and in otherfile.tex: \include{quotesFile}

\placeContent{quote2} % I want something as simple
% as this to insert the content
% of the environment, here with
% the identifier "quote2"

\placeContent{quote3}


so the output should be:
lorem ipsum dolor sit amet

foo bar baz foobar foobaz

I do not want something like

\newcommand{\quote1}[]{This is a quote}


for several reasons. Well, it kinda would solve the problem but it just doesn't seem aesthetical to me (blame me). I prefer to keep things clear and tidy and all the brackets and commands which are actually just simple text don't seem tidy to me but rather confusing. According to my opinion and understanding commands/macros shall serve to avoid repetition of code but aren't to be used for ordinary text content.

• I think that you need to add more details, and preferably give an example, so that it is clearer what you want. I don't, for example, understand why simply defining \x1, \x2, ... (with different names/codes) and then using these macros (which might expand into environments) doesn't solve your problem.
– user30471
Jul 27, 2014 at 4:24
• You need to provide more information about what you desire, but I suspect you want this tex.stackexchange.com/questions/4889/input-only-part-of-a-file , yes? You can put content in between tags in one file and use that refer that content to be input into other files too. Jul 27, 2014 at 5:09
• @GuidoJorg thank you, the catchfilebetweentags package, as mentioned in the question you linked, solves the problem quite well, actually better than what I was looking for. Thank you a lot!
– Mike
Jul 27, 2014 at 6:02
• I guess I'll put that up as the answer below in case other people search for this too. Jul 27, 2014 at 7:00

The package to use in this case is:

http://www.ctan.org/tex-archive/macros/latex/contrib/catchfilebetweentags/ gives the clearest code for this type of system of documents, each one citing perhaps parts of other.

Basically, a system of organization based on placing tags with some kind indexing convention (in between which a particular environment with a often reused piece of code or text goes) in documents allows using pieces of each as necessary where these may be littered and reused across many documents.

If the indexing convention is clever (use different prime numbers multiplied together to uniquely mark across documents as often done in software, as suggested by the only scientist recognized by posterity for his efforts with a brand of biscuits named after him?) or straightforward, say [Lichtenberg]{B-25},for the 25th aphorism of Lichtenberg's journal B, found in a larger document (a quotes file Lichtenberg, itself broken into sections A, B, C, according to the journals that sources of the quote in this case.

With a slightly different input, the following works just as well:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{myquotes.tex}
\begin{myquote}{quote1}
This is a quote
\end{myquote}

\begin{myquote}{quote2}
lorem ipsum dolor sit amet
\end{myquote}

\begin{myquote}{quote3}
foo bar baz foobar foobaz
\end{myquote}
\end{filecontents*}

\usepackage{environ}
\newcommand{\myquotearg}{}
\NewEnviron{myquote}[1]{%
\renewcommand{\myquotearg}{#1}% Store myquote environment argument
\ifx\myquotearg\contentName% Match argument with content name
\BODY% Print myquote environment body
\fi
\ignorespaces
}
\newcommand{\contentName}{}
\newcommand{\placeContent}[1]{%
\renewcommand{\contentName}{#1}% Store content name
\begin{document}

\placeContent{quote2}

\placeContent{quote3}

\end{document}


The change is to supply an argument to your myquote environment which is then used to check against the argument of \placeContent. environ is the key here, which allows to swallow the entire contents of the environment into \BODY. We just condition on printing it based on whether the argument matches \contentName.

An alternative to the @Werner response. With the scontents package it's easy to get what you're looking for. Everything stored in memory including the content verbatim. If you want you can also save the content to external files using the key write-env=file.tex. Thanks @Werner for the MWE and the idea :).

\documentclass{article}
\usepackage[store-env=quote]{scontents}
\pagestyle{empty}
\begin{scontents}
This is a quote
\end{scontents}

\begin{scontents}
lorem ipsum dolor sit amet
\end{scontents}

\begin{scontents}
foo bar baz foobar foobaz
\end{scontents}
\begin{document}
Some text
\getstored[2]{quote}

\getstored[3]{quote}

\getstored[1]{quote}

\end{document}