2

I want to be able to have files chapter1.tex, chapter2.tex, etc. (with preambles) that can compile on their own and a file allChapters.tex that gather the actual contents of the chapters.

So far, I have been able to do this with the package catchfilebetweentags.

But, it seems that the package catchfilebetweentags intefers with the package verbatim.

Is there an alternative to catchfilebetweentags to achieve my initial goal?

edit

Now, a long time after having asked this question, I see solution. You create variables in the main file and you test in the subfiles if the variables exist. If they don’t exist, you go with \documentclass{article}, etc.

10
  • 1
    I would simply have a file for each chapter rather than rely on delicate packaegs matching subsections of your main file. if the ody of chapter2 is \include into your main document have a file chap2.tex that has a preamble then includes the same file. Feb 7, 2021 at 15:36
  • Thanks. I'm not sure to understand your idea. Could you more precise/explicit (maybe in an answer)? My conclusion so far is that I should create an external script.
    – Colas
    Feb 7, 2021 at 18:25
  • I am not sure what you need, why do you need more than the standard \include mechanism that is designed to allow individual chapters to be processed separately Feb 7, 2021 at 18:27
  • I feel it is not convenient at all when the file that I compile is not the file where I write the content.
    – Colas
    Feb 7, 2021 at 19:48
  • 1
    I don't know texshop but any reasonable editor would allow you to specify a master file so editing chapter1.tex you could run latex on main.tex with a key shortcut without having to bring main.tex into the editor (the editor I use (emacs) has had that feature since the 1980s) Feb 7, 2021 at 21:30

2 Answers 2

3

The two packages I know of that can do that are the subfiles package and the standalone package. My favorite is subfiles, but each file uses the main preamble, so if you want individual preambles for each, you probably want standalone. I will describe both.

The standalone package

With this package, each and every file in the project must be able to work individually, hence its name "standalone." It has a default command for inserting the files, \input{}, but it requres that each file is in the same directory, which quickly becomes unruly, so I strongly reccomend you use the import package as well. Below is an example of syntax, it is not a MWE.

\documentclass{article}
    
\usepackage[subpreambles=true]{standalone}
\usepackage{import}
    
\begin{document}
    
Hi, this is not a MWE, just an example of the syntax the standalone package uses.
I did that because it requres multiple files.
    
\import{directory}{filename}
    
\end{document}

The subfiles package

In this package, which I prefer to standalone, the documents are not independant of each other. The subfiles use the main document's preamble. This package has one catch: the main file must be named main.tex, and if you want to nest subfiles, each nested main file must also be called main.tex, which can get confusing if you dont name your directories appropriately. Below is an example of syntax, it is not a MWE.

\documentclass{article}

\usepackage{subfiles}

\begin{document}

Hi, this is not a MWE, just an example of the syntax the subfiles package uses.
I did that because it requires multiple files.

\subfile{directory/filename}

Note that each subfile must start with \documentclass[../main.tex]{subfiles}.
If you nest subfiles, add a .. to the begining of the argument for each level of nesting.

\end{document}
1

If you structure your document chapter-wise, then use \include and \includeonly. You will compile always main.tex, but can control via \includeonly, which chapters are actually typeset. In this solution, there is only one preamble (in main.tex) which is used for all runs.

% main.tex
\documentclass{article}
\includeonly{chapter1,chapter4}
% ... preamble with definitions and \usepackages
\begin{document}
\include{chapter1}% will be included 
\include{chapter2}% will be skipped
\include{chapter3}% will be skipped
\include{chapter4}% will be included
\end{document}

%chapter1.tex
\chapter{My first chapter}
Text of first chapter.

%chapter2.tex
\chapter{My second chapter}
Text of second chapter.

% and so on ...

Solution proposed by David Carlisle

% allChapters.tex
\documentclass{...}
% ... preamble for allChapters
\begin{document}
\input{chapter1}
\input{chapter2}
\input{chapter3)
\end{document}

% chap1.tex typesets only chapter1.tex
\documentclass{...}
% ... preamble for chapter1.tex
\begin{document}
\input{chapter1}
\end{document}

David's solution with shared preamble

% mypreamble.tex
% ... all the definitions and \usepackages

% allChapters.tex
\documentclass{...}
\input{mypreamble} % load the common preamble
\begin{document}
\input{chapter1}
\input{chapter2}
\input{chapter3)
\end{document}

% chap1.tex typesets only chapter1.tex
\documentclass{...}
\input{mypreamble} % load the common preamble
\begin{document}
\input{chapter1}
\end{document}

What to do when the files reside in different directories, when the chapter files have further subfiles etc.

In this case use the answer of @Will and use standalone or subfiles.

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