6

Inspired by (although a possible duplicate) Outputting each chapter of a LaTeX document to a separate PDF, I wonder whether there is a possibility to extract the chapter (or whatever sectioning level) content to separate *.tex files if the .tex source file is not already 'splitted' and uses \include statements?

\documentclass{scrbook}

\begin{document}
\chapter{My first sophisticated chapter}
Some content...

\chapter{My second, even more sophisticated chapter}
Other content....

\end{document}

This means, that everything between (but including the \chapter{...} line!) should be captured and written to separate file, say \jobname_chapter\number\value{chapter}.tex such that

\jobname_chapter1.tex contains

\chapter{My first sophisticated chapter}
Some content...

and

\jobname_chapter2.tex contains

\chapter{My second, even more sophisticated chapter}
 Other content....

Most probably, one way is to redefine the corresponding sectioning command to redirect the output not to be directly processed by TeX/LaTeX.

I think, this could be 'extended' to other grouping commands, if it is possible at all.

For the sake of simplicity: No one should care about that those 'standalone' chapter files are not compilable at all.

I also do not care about performance of such an approach

Edit

If the \chapter command would be such that \chapter{title}{Chapter content...}, this would be 'easy', just catch the 2nd argument of the command and write it (after \unexpand\expandafter commands) to a file via \immediate\write. However, \chapter has another syntax ;-)

Well, still no solution ahead :-(

  • 3
    This is pretty clearly a problem for the text editor and not the interpreter. In any editor you can just select and scroll to the next \chapter; in emacs (at least, with AucTeX) you can use a single command to select the entire chapter at once. As long as you only have to do this once, why automate it? – Ryan Reich Apr 28 '14 at 04:13
  • @RyanReich: I think of something more than just \chapter commands. Say, in a textbook with about hundreds of exercises all in a environment/command it would be nice to split them to individual files and being able to recombine them in another file, say in alternate version of the document. Your comment is true, of course, if there are only few chapters to extract... –  Apr 28 '14 at 04:16
  • 2
    I think the scripts tag may be appropriate for this question...I could imagine a perl script could be written for this task; perhaps even some imaginative use of grep – cmhughes Apr 28 '14 at 04:16
  • @cmhughes: Added the tag, although I am really interested in a solution which uses TeX/LaTeX only. –  Apr 28 '14 at 04:18
  • 9
    Please, let me exhort against using TeX for this clearly algorithmic task unrelated to its typesetting activities. It is possibly the world's worst programming language, and although it's pretty likely that one of the very talented TeX programmers here will enjoy the challenge, you'll get more value from doing this with a more appropriate tool where the solution is, say, readable as code. – Ryan Reich Apr 28 '14 at 04:28
  • @RyanReich: I agree with you that it might look awful and hard to understand (trying) to achieve this with TeX 'programming' tools. –  Apr 28 '14 at 04:31
  • @MikeRenfro: I appreciate all hints about scripts/regex, but I am interested in a pure TeX solution. It is basically a let's test the possibilties of TeX question. If there is no solution/way, I have to accept it. –  Apr 28 '14 at 11:57
  • Seems like this belongs in "code golf" rather than tex.se if it's really a "let's see if this is possible" question. – Andy Clifton May 01 '14 at 16:42
  • This question appears to be off-topic because it is about the text editor. The interpreter is not supposed to split a file. – Masroor Jul 24 '14 at 04:22
  • I doubt grep would be a good choice but something like gawk would make this trivial, I think. – cfr Sep 20 '14 at 22:13
  • @cfr: Yes, sure it would, but I was looking for a purely (La)TeX - based solution :-( –  Sep 20 '14 at 22:14
  • @ChristianHupfer Yes, I know. I disapprove ;). But it was really a response to cmhughes' comment lest anybody try to do this with grep. – cfr Sep 20 '14 at 22:16

1 Answers1

3

After thinking a lot about this question, I decided to answer this question in the negative sense:

Obviously it is by far too difficult to split a .tex input file into logical units such as chapters etc. only with direct means of (La)TeX itself.

Perhaps it could be possible with LuaLaTeX, but I have not used that so far, so I can't not provide a better example.

I also decided to let this question close for the meantime. If someone knows or finds a good solution, I will readily open it again and accept that answer.