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.

  • I have to confess that although the answers sounded like they might work, in the end I had to give in and just maintain two separate documents. My highest priority was getting the dissertation done so I could pass my defense. :) Merging with subversion worked surprisingly well in practice most of the time.
    – Paul
    Commented Nov 19, 2010 at 18:12

5 Answers 5


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

% One for each separate unit you want to split up.



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

%text here

And in your thesis, you can do

% again, one for each extraction.

%some other text


%some other text


It's very difficult to try to come up with a single solution that would match everyone's needs. You might want to look at packages like newclude, includex and subdocs. But it's entirely possible none of them will have what you want.

Personally I do something very similar to what Willie was suggesting. (Sorry if this is a little redundant. I wrote most of it before Willie's showed up.) Note, however, that you can do pretty much all of it without even needing the comment package.

Create different "wrapper" documents containing a different preamble with a bunch of command definitions, different for each version, followed only by \input{...} and \include{...} commands, calling the "core" documents which made use of those commands but interpreting them differently depending on the different definitions. E.g., in one you have:


in another you have, conversely:


and in the body.tex things like


 Some stuff that only goes here in the article version.


Of course you have have multiple included files if that's easier too, especially, for e.g., something that goes one place in one version and somewhere else in another. Then you can put \articleversiononly{\input{movingpiece.tex}} in one place in body.tex, but \thesisversiononly{\input{movingpiece.tex}} in another place, and so on. Compiling the master versions will give you what you need.

That's always been enough for me, but I don't know whether my needs have ever been similar to yours. If I needed more than that, I'd create one "master version" and then write a script to create subversions using sed, perhaps using cues stuck in with comments, or something like that.

  • Your solution is in fact technically simpler. Two of the small advantages that I can see for the more complicated way I did it are that (a) you don't have to chase down opening/closing braces, which may get tiresome sometimes and (b) it is slightly easier to search for where each included section begins or ends. Commented Oct 15, 2010 at 20:26
  • Agreed, Willie. Partly also a matter of taste/style.
    – frabjous
    Commented Oct 15, 2010 at 21:04
  • 2
    Your answer was very good, but since I had to choose just one I ended up with Willie's. Unfortunately I don't have enough rep to bump your answer either.
    – Paul
    Commented Oct 16, 2010 at 0:28
  • The TeX FAQ suggests using the comment package over a macro that gobbles its arguments. To quote: \gobble is pretty inefficient at consuming anything non-trivial, since all the matter to be skipped is copied to the argument stack before being ignored.
    – Aditya
    Commented Oct 16, 2010 at 5:34

Copying from Will Robertson's answer to this question, the catchfilebetweentags package looks like a good option.


I think the filecontents package could be used for this: the main document document has some boilerplate that spits out files for each part and then immediately includes them. Once the document is run, then additional documents can be run that are just wrappers for these exported files.

Not tested.

  • This is definitely an interesting possibility.
    – Paul
    Commented Nov 19, 2010 at 18:13

the pdfpages package will save you a lot of headache.

In your preamble: \usepackage{pdfpages}

and in your body, where you want to include the pages:


  • 1
    You'll get wacky page numbering with this. Commented Nov 18, 2010 at 9:58
  • ... and I don't think that this is what the OP intended to do. Commented Nov 18, 2010 at 10:13
  • Yeah, incorrect page numbering is a killer problem in a scientific publication, and more importantly page was the wrong granularity. Still, I could see there are times this would be very useful. But my university won't let me just paste the paper into my dissertation for some reason. :)
    – Paul
    Commented Nov 19, 2010 at 18:14

You must log in to answer this question.

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