Take the 2-minute tour ×

Imagine I have a complete latex file and I want to extract only the text that appears in a specified environment (i.e., within a custom hypothesis environment)

For example:

Lots of stuff that I don't want extracted
 The content that I want to extract
Lots more stuff I don't want to exract
 Some more content that I want to extract

The question: What's a simple way of taking a complete latex source file and extracting just the text in a specified environment and saving it to a new text file?

Although I'm not an expert I've heard a lot of people talk about Perl scripts being good for string manipulation. I also sometimes use regular expressions. Thus, in addition to a specific solution to the above problem, I would be interested to hear about general approaches to related LaTeX text manipulation tasks.

Update: Copying and pasting is not a desired option because the environment occurs over 20 times in a 20,000 word document.

share|improve this question
It's simple to write a perl script to do what you ask, particularly with the layout that you use: cat file.tex | perl -lne '/^\\end{hypothesis}/ && $c = 0; $c and print; /^\\begin{hypothesis}/ && $c = 1;'. An answer more suitable for this site would have LaTeX write out the file when the document was compiled! This is certainly possible as it is what beamer does for "fragile" frames. –  Loop Space Aug 31 '10 at 11:58
@Andrew Thanks for that. The solution in Perl does look elegant. And the write-while-compiling option sounds useful and possibly easier to learn for someone who has never used Perl. Hopefully someone will provide an answer on how to write-while-compiling. –  Jeromy Anglim Aug 31 '10 at 12:22
Could you modify your question to explain why just firing up notepad* and copy/pasting the text is not a viable option? (*or your editor of choice) –  Seamus Aug 31 '10 at 12:27
To avoid copy and paste, you could make a separate .tex file where you define a bunch of macros like \def\hypothesis1{Ice melts faster than wood} ... \def\hypothesisN{....} Then you could use the macros in both places. –  Geoff Aug 31 '10 at 12:41
Do you expect to have LaTeX code/macros within the hypothesis block? If so, do you want them to be expanded before the external file gets written? For example, if you have \begin{hypothesis}\textbf{Foo}\end{hypothesis}, can you tell us specifically how you want the output file to look? –  ESultanik Aug 31 '10 at 12:49

3 Answers 3

up vote 15 down vote accepted

As well as the options provided by Ulrike, you might also be interested in the extract package, which was written for exactly the problem you're looking to solve.

To export all text within the hypothesis environment to a file called filename, add the following code to the preamble:

\usepackage[active, generate=filename, extract-env={hypothesis}]{extract}
share|improve this answer
awesome. I'll have a look. –  Jeromy Anglim Aug 31 '10 at 13:54

Writing the content of one environment to a file is easy: LaTeX already knows the filecontents-environment. The filecontents packages extends this environment. fancyvrb defines VerbatimOut. The listings package has some internal code (used e.g. by the showexpl) too. If you want to collect the content of more than one environment you will have to change their definition so that the end doesn't close the file.

If you don't care about the line breaks and the comments in your environment you can also try something like this:




abc % cde

some text
share|improve this answer
Thanks. This looks good. –  Jeromy Anglim Sep 1 '10 at 7:49
@ulrike: is it possible to loop this command to create a new file for each problem? I think I could manually name each environment, say testA, testB, or test1, test2, etc. –  MeTutel Apr 26 '13 at 18:36

As Will Robertson let me know, the extract package makes the task very easy.

For the sake of completeness, the following code in the preamble did what I wanted:


exporting all text within the hypothesis environment to a file called filename.

share|improve this answer
Useful. I've edited this information (i.e. the actual code) into Will Robertson's answer. (I did it that way round mainly because you'd already accepted that one.) Anyway, wanted to give you kudos for not just saying 'that package worked', but also coming back and posting the code. –  Esteis Mar 8 '13 at 20:55

Your Answer


