5

I have a number of latex files section1.tex, section2.tex, etc. Each of them should be able to be compiled individually to give a normal LaTeX document. I would like to write a file master.tex that will \include or \input these files and combine them into a single document.

Note that I do not want to edit the section1.tex file. Is there an easy way to do this?

I have heard that the standalone package can do this, but I am having trouble getting the desired behavior.

My attempted master.tex file:

\documentclass{article}
\usepackage{standalone}
\title{My Book}
\author{Me}
\begin{document}
\maketitle
\include{Section1.tex}
\end{document}

My attempted Section1.tex file

\documentclass{standalone}
\standaloneconfig{class=article,crop=false}
\begin{document}
\section{Chapter 1}
Contents of Chapter 1.
\end{document}

The main problems I am having are

  1. There are errors when compiling Section1.tex . It states that this file might be missing an \item . There are also an error about needing to insert a } and then later an error that there are too many }'s
  2. The \section{Chapter 1} line is being cropped and placed as a second page. The "Contents of Chapter 1" are placed at the top of a full page, with no top margin.
  3. None of the text from Section1.tex seems to be included in master.tex.

Thank you for your assistance.

  • it's probably possible but it's always going to be fragile and introduce weird package dependencies on packages that work doing this, why not just do the much simpler thing of just having the section bodies in files that you input (or include) and then to produce individual sections use \includeonly or have a small document with just a preamble and \input the section you want? – David Carlisle Jul 22 '16 at 17:20
  • Don't use the standalone-class for section1 but also article. – Ulrike Fischer Jul 22 '16 at 17:21
  • @DavidCarlisle I had a project with hundreds of files which all could be compiled indidually but also input in larger master files. It work flawlessly and was very useful, as the single files had to be edited by various peoples on sharelatex. One naturally has to use the same preamble everywhere and understand what it does to labels. – Ulrike Fischer Jul 22 '16 at 17:26
  • @UlrikeFischer The documentation for standalone states that the included files should have document class standalone. I have the same errors and issues if I use \documentclass[article]{standalone} . – Stephen Flood Jul 22 '16 at 17:29
  • @UlrikeFischer as I say I know it can work, but I think (from what I see on questions here, not particularly this one) that it's over-used and people overlook the simpler (and often more robust) approaches to obtain the same thing, – David Carlisle Jul 22 '16 at 17:37

1 Answers1

8

Use this master:

\documentclass{article}
\usepackage{standalone}
\title{My Book}
\author{Me}
\begin{document}
\maketitle
\input{Section1.tex}
\end{document}

and this Section1

\documentclass{article}
\begin{document}
\section{Chapter 1}
Contents of Chapter 1.
\end{document}

If your files are in different folders relative pathes can be wrong. In this case you can use for example the import package.

Ulrike Fischer
  • 327,261
  • Thanks! This works perfectly. I suppose I misunderstood the documentation. Also your use of the\input command works where the \include command I was using didn't. – Stephen Flood Jul 22 '16 at 17:36
  • A problem with this approach: If the section1.tex file is in a folder sections and I use \input{diagram} in it (relative path to the file diagram.tex also in the sections folder) then when compiling the master file I get an error File diagram.tex not found. – Silv Jun 05 '20 at 10:02
  • @SilvanEgger I know but that wasn't the question here. You can find a number of question about relative pathes, search for import and subfiles. – Ulrike Fischer Jun 05 '20 at 10:17
  • True. It's just that I used your answer and immediately faced the issue with the relative file path in section1.tex. Therefore at least a note like "don't use this approach when working with relative file paths in section1.tex" would have been helpful. – Silv Jun 05 '20 at 10:28