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 :-(
\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\chaptercommands. 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:16scriptstag may be appropriate for this question...I could imagine aperlscript could be written for this task; perhaps even some imaginative use ofgrep– cmhughes Apr 28 '14 at 04:16grepwould be a good choice but something likegawkwould make this trivial, I think. – cfr Sep 20 '14 at 22:13grep. – cfr Sep 20 '14 at 22:16