Can LaTeX natively include only specific expressions from an input file (ignoring the rest)? I am not sure how to best word my question. Below is an example of what I'd like to do.


For example, let's say I have many files with all kinds of code, but each one shares something in common, a summary environment.

I decide to create a document that contains summaries of all of the other documents. For a meaningful document, I need to get the name of the input file and include only the following code from the input:

A bunch of text.

Here is a sample file structure:

  Summaries.tex <-- includes summaries of all other tex files and their file names

It would be great if Summaries.tex could automatically detect new files in the Documents folder such that it is completely automated (obviously that is a bonus and not really a criterion for the question).

I know this is possible with scripting languages by calling a shell script with the --shell-escape option combined with \immediate\write18 or \include{|myscript.sh}, but is this possible natively in LaTeX?

UPDATE June 30, 2015

Solution based on Werner's suggestion:

LaTeX does support this feature with the catchfilebetweentags package. What it does not support is getting a list of files (e.g. within the same folder) to input automatically (unless the only change between file names is a number: in which case you can use a sequence see Including all files within a directory) .

To avoid using --shell-escape and maintain an OS-independent workflow, it is possible to achieve the same thing by creating a separate file (e.g. Documents.txt) that contains the names (or paths depending on whether you want to reconstruct paths with a macro or not) of the documents (stories). To have Summaries.tex automated, you can reference this file (e.g. Documents.txt) instead of having LaTeX generate it with --shell-escape + external script. Of course, 100% automation would require a system that runs a pre-compilation script that collects all of the tex files you want to include and spits them out into Documents.txt beforehand.

see https://tex.stackexchange.com/a/4952/13552

  • 1
    Possible duplicate: \input only part of a file. The main suggestion there would be to use tags and catchfilebetweentags.
    – Werner
    Jun 29, 2015 at 22:00
  • What do you mean by 'natively'? If you use standalone you can set environments of your choice as standalone environments.
    – cfr
    Jun 29, 2015 at 22:01
  • 1
    You can turn a screw with a knife, but a screwdriver is a better tool.
    – egreg
    Jun 29, 2015 at 22:10
  • 1
  • 1
    It's certainly possible, but you should make clearer what the contents of the “summary container” file looks like.
    – egreg
    Jun 29, 2015 at 22:24

1 Answer 1


It's not really clear what your aim is, but this could be what you're looking for.

File summaryfile.tex (containing all summaries)

Blah blah Blah blah Blah blah Blah blah Blah blah
Blah blah Blah blah Blah blah Blah blah Blah blah
Blah blah Blah blah

This is the summary we want to typeset. Let's make
it long enough to be split across two or maybe
three lines. Is this sufficient?

File test-summary.tex


    \begin{center}\bfseries Summary\end{center}





enter image description here

  • You got it backwards, I wrote "create a document that contains summaries". This means that this file is generated. Now I see why you asked in the comments above for an example of a “summary container”. The main documents contain the summaries for their respective contents. When the "summary container" is compiled, it is able to grab all the documents and input only the summary environments from them (I am afraid that getting the files without explicitly knowing their names is an OS function only). This results in a nice list of document names and their associated summaries. Jun 30, 2015 at 12:15
  • @macmadness86 Let me quote your question: “Can LaTeX natively include only specific expressions from an input file (ignoring the rest)? ” I solved this problem. If your problem is different, then also the question should be different. A Perl script (or whatever scripting language you prefer) is the right way to cope with your problem.
    – egreg
    Jun 30, 2015 at 12:33
  • Sorry but I've got to call you out on this one. In the first paragraph I wrote: "I am not sure how to best word my question. Below is an example of what I'd like to do." I think reading the context of the question is quite important in this case. Anyway, this question is a duplicate and should probably be marked as such. Jun 30, 2015 at 12:36
  • @macmadness86 Sorry, but the addition was made after I answered and, to be sincere, I couldn't understand the meaning of it.
    – egreg
    Jun 30, 2015 at 12:40
  • No, it wasn't. See the history. I just added a sample file structure after you answered the question. Jun 30, 2015 at 12:41

Not the answer you're looking for? Browse other questions tagged .