3

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.

Situation

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:

\begin{summary}
A bunch of text.
\end{summary}

Here is a sample file structure:

Documents/
  JimsAdventures.tex
  CrazyGorilla.tex
  BrightHorizon.tex
  MoonLanding.tex
  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 '15 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 '15 at 22:01
  • 1
    You can turn a screw with a knife, but a screwdriver is a better tool. – egreg Jun 29 '15 at 22:10
  • @cfr By natively I mean not using external scripting languages to parse external files. Everything done within language. See last paragraph of my question. – Jonathan Komar Jun 29 '15 at 22:18
  • @egreg I agree. However, enabling --shell-escape is not ideal for a couple of reasons: (1) It is a security risk. (2) Solution is often platform-specific. – Jonathan Komar Jun 29 '15 at 22:20
  • 1
    see http://tex.stackexchange.com/questions/57223/automating-quoting-across-latex-documents/57231#57231 – David Carlisle Jun 29 '15 at 22:24
  • 1
    It's certainly possible, but you should make clearer what the contents of the “summary container” file looks like. – egreg Jun 29 '15 at 22:24
  • (1) is false if you wrote or understand the script. Running gawk via shell escape is no more dangerous than running gawk in any other way. Most of the security risk comes from the possibility of compiling a document from an untrusted source. There is, admittedly, a residual risk in that somebody could hide a shell script, say, in a package which your document loads or in the class it uses. But the majority of the risk lies in compiling random documents from others with shell escape enabled. (2) is, however, true. But so is egreg's point. – cfr Jun 29 '15 at 22:24
  • At least, that's how I've always thought of the risk. Maybe @egreg knows better.... – cfr Jun 29 '15 at 22:26
  • @cfr No, the script could be modified maliciously (e.g. if permissions are correct, this will not likely happen, but in a large svn repository, where it is possible to check in malicious code, it is possible to cause some serious damage when I server compiles the documents automatically). – Jonathan Komar Jun 29 '15 at 22:29
  • @macmadness86 Maybe. But I wouldn't trust the protection offered by even no-write - let alone restricted-write - in that case. – cfr Jun 29 '15 at 22:33
  • @Werner I think this is a duplicate too. I tried marking it a duplicate, but it seems even me, as owner of this question, can not properly mark it as a duplicate. The answer with catchfilebetweentags seems to be sufficient. (Although I'd like to automate the file inputs to all files in a given folder--but that is not explicitly part of the question) – Jonathan Komar Jun 30 '15 at 07:47

1 Answers1

2

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

File summaryfile.tex (containing all summaries)

\begin{summary}{notthisone}
Blah blah Blah blah Blah blah Blah blah Blah blah
Blah blah Blah blah Blah blah Blah blah Blah blah
Blah blah Blah blah
\end{summary}

\begin{summary}{test-summary}
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?
\end{summary}

File test-summary.tex

\documentclass{article}
\usepackage{environ,pdftexcmds}

\makeatletter
\NewEnviron{summary}[1]{%
  \ifnum\pdf@strcmp{#1}{\jobname}=\z@
    \begin{center}\bfseries Summary\end{center}
    \begin{quote}\BODY\end{quote}
  \fi
}
\makeatother

\begin{document}

\input{summaryfile}

\end{document}

Output

enter image description here

egreg
  • 1,121,712
  • 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. – Jonathan Komar Jun 30 '15 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 '15 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. – Jonathan Komar Jun 30 '15 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 '15 at 12:40
  • No, it wasn't. See the history. I just added a sample file structure after you answered the question. – Jonathan Komar Jun 30 '15 at 12:41