[Tex/LaTex] Including parts of a LaTeX document in another document WITHOUT splitting up original document

commentsrevision control

I'm working on a paper with some collaborators. At the same time, I'm writing my dissertation for my defense next month.

I need to include the paper in the dissertation, but it doesn't just become a chapter. Parts go in one chapter, and parts of it go in another. There are too many parts to just put each in their own file; at least 5 or 6, so I think my collaborators would be upset. Each dissertation chapter is currently a separate file (separate directory even).

So what I really want is to slice-and-dice the paper, occasionally even at the level of individual paragraphs, into my thesis, and at the same time be able to merge in changes made by myself and my collaborators on the base paper. This merging should be easy, or preferably done automatically when I request it. (The paper and dissertation are both in subversion.)

Do I have any options for accomplishing this fairly easily? Would \includecomment and \excludecomment from the comment package help me? I've read a little bit about the extract package, which sounded pretty useful except that it's designed to produce free-stranding documents via extraction, whereas I want to extract chunks of LaTeX to be included with \input. Does anyone know how well this package works, and whether it could help me? Could I conditionalize the preamble in the paper somehow so that it could all get commented out when I input the extracted tex file? I.e., #ifndef EXTRACT, and #define EXTRACT before I do \input?

(Also, I presume I could place any new packages directly in the directory so that my collaborators can get it automatically.)

I've used TeX for years but am really not an expert, and I really need to get my dissertation written so I can defend. So far I've just forked the paper and was hoping to do subversion merges, but that's not going to be sufficient–subversion won't be able to track paragraphs I relocated because it doesn't have a concept of relocating a text block.

Best Answer

You can probably hack it with something like this (I'm not sure if it would work). You may have to split the TeX file into the wrapper for the paper, and the body for the paper. In the wrapper for the paper, include the comment package, and set

\usepackage{comment}
\includecomment{bodyOfText}
\includecomment{paraA}
\includecomment{paraB}
% One for each separate unit you want to split up.

\newenvironment\commented[1]{\end{bodyOfText}\begin{#1}}%
  {\end{#1}\begin{bodyOfText}}

\begin{document}
\begin{bodyOfText}
\input{body}
\end{bodyOfText}
\end{document}

Inside body.tex where you keep the body of your text, each part you want to extract should be demarcated by

\begin{commented}{paraA}
%text here
\end{commented}{paraA}

And in your thesis, you can do

\usepackage{comment}
\excludecomment{bodyOfText}
\excludecomment{paraA}
% again, one for each extraction.
\newcommand\extracttext[1]{\includecomment{#1}\begin{bodyOfText}%
  \input{body}\end{bodyOfText}\excludecomment{#1}}

\begin{document}
%some other text

\extracttext{paraA}

%some other text

\extracttext{paraB}
\end{document}
Related Question