TransWikia.com

Making a series of child TeX files and including them as "chapters" in a parent file?

TeX - LaTeX Asked by user1532974 on July 2, 2021

I want to build a big PDF of multiple TeX files which will all have the same preamble. They’ll have newcommand and renewcommand as well as usepackage and documentclass because each file should be standalone and compiled as a smaller PDF. But then, I want to take all of the files we have and put them in a main document. I’ve looked at the other questions:

and the standalone package. I’m not sure I’ll be able to do what I want with these.

Each child document needs to stand on it’s own, with it’s own preamble, and table of contents, etc. But they should be included in a master document with a global table of contents that ignores/copies all preambles.


Below is example code for one of the children files.

documentclass[11pt]{article}
usepackage{amsmath, amsfonts,amssymb,latexsym, multirow}
usepackage{fullpage, graphicx, subfig, float, hyperref, enumerate}
usepackage[parfill]{parskip}
usepackage{pdflscape}%for large figures
usepackage{cancel}

linespread{1.3}

hypersetup{backref,  
    pdfpagemode=FullScreen,  
    colorlinks=true}

renewcommand{dag}{^dagger}
renewcommand{d}{text{d}}
newcommand{D}{text{D}}
newcommand{bra}{langle}
newcommand{ket}{rangle}

newcommand{comment}[1]{}

newcommand{p}{partial}
newcommand{eq}[1]{begin{align*}#1end{align*}}

begin{document}

noindent fbox{
begin{minipage}{6.4in}
  medskip
  textbf{Book} hfill textbf{Author}
  begin{center}
    {Large Chapter #} [3mm]
  end{center}
today hfill Subauthor
medskip
end{minipage}
}

bigskip
tableofcontents
newpage
section{}
subsection{}
end{document}

2 Answers

There are packages to do that kind of combination but if you are worried about unknown artefacts from the command redefinitions you can simply use

ifxpundefined
documentclass[11pt]{article}

. . .

fi
begin{document}

Then when processed on its own the document will work as normal but in a larger document that already has those definitions loaded (and in particular p defined you can use

{defdocument{}defenddocument{}input{file-1}}

Not related to the inclusion question but the boxed heading is lager than the page:

Overfull hbox (36.87251pt too wide) in paragraph at lines 29--40

Answered by David Carlisle on July 2, 2021

The subfiles package does exactly what you are looking for. There are is a great documentation on Overleaf even with live examples. The main document includes the child files as this:

documentclass{book}
usepackage{subfiles}
% global preamble
begin{document}
subfile{childA}
subfile{childB}
subfile{childC}
end{document}

The child files can be compiled individually and even have their own preamble.

documentclass[main.tex]{subfiles}
% individual preamble
begin{document}
content
end{document}

Also cross-references between the files are supported.

Answered by Martin on July 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP