29

I am creating a lab manual that contains a dozen or so different labs. I want to create each lab manual as their own compilable standalone document and in a main.tex file I would like to put them all in order.

I have looked into the \input, \include, and \import commands. \input is clearly not what I want. \include has not worked for reasons about preamble commands located not in the preamble. The answer may be in \import somewhere and I don't know how to use it. (It would be great if someone could show me.)

Also I could combine the PDFs with a shell script, but if that is the answer I am not sure how to combine the PDFs.

3
  • Your question might have been answered here. Commented Jan 25, 2014 at 7:03
  • 1
    pdfpages is what you want.
    – alexis
    Commented Jan 25, 2014 at 9:25
  • pdftk is a great tool for manipulating PDFs. But if you want a *TeX solution, look at the 'subdocs' catefory of CTAN, perhaps especially 'combine', 'standalone', and 'subfiles'. And of course pdfpages is great for including already exisiting PDFs into your .tex file.
    – jon
    Commented Jan 25, 2014 at 15:12

7 Answers 7

21

You might be looking for the subfiles package. You have one main tex-file (main.tex for example) with the entire preamble you want in each file. Here you choose the documentclass you like and use the package subfiles: \usepackage{subfiles}. Between \begin{document} and \end{document} you use \subfile{chapter1.tex} to include chapter1.tex.

In your seperate files (for example chapter1.tex, chapter2.tex, chapter3.tex) you write the following: \documentclass[main.tex]{subfiles}. This way you can create seperate pdfs from each chapter (just compile the chapter1.tex file for example) and you can create a main.pdf.

A minimal example:

main.tex

\documentclass{report}
\usepackage{subfiles}

\begin{document}
\tableofcontents
\subfile{chapter1.tex}
\end{document}

chapter1.tex

\documentclass[main.tex]{subfiles}
\begin{document}
Here comes some text
\end{document}
1
  • I'm writing my thesis and want to create different files for different chapters which I can later combine using one "master.tex" file. Your suggestion seems useful to me. I wanted to confirm if the numbering of chapters, sections etc will work out fine once I combine the files?
    – PGupta
    Commented Sep 17, 2020 at 16:20
9

I have found that this works:

\documentclass{report}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=-]{doc1.pdf}
\includepdf[pages=-]{doc2.pdf}
%... continue for all docs
\end{document}

This will insert your documents into one file in the order specified.

2
  • One problem with this is the pages size are all changed to article. If my pdf has inbetween different page sizes it does not consider that
    – Santhosh
    Commented Aug 22, 2020 at 12:18
  • Even if we usefitpaper option it will consider the first page as the page size for all the paages.
    – Santhosh
    Commented Aug 22, 2020 at 12:20
4

The commands \input and \include are good for splitting a single TeX document into several files, but the combined text should look like a single document (i.e., only one preamble, and definitions get continued from one text to the next. Since you have several independent files, these commands aren't for you.

You don't make it entirely clear what you want, so here's two scenarios:

a) You simply want to bind the PDFs together into a single PDF, without any changes. In that case, pdfpages is your friend. Create a simple document that loads pdfpages and simply includes each component pdf with \includepdf{docname}. See the pdfpages documentation for more details and options (e.g., partial inclusion).

b) You want to combine the independent documents and compile them as a singe TeX document. In that case, use docmute as explained in this answer.

6
  • Many thanks for the useful answer --- one comment... the link to docmute answer seems to be broken or not pointing where it should be... maybe point to this question and the second answer; tex.stackexchange.com/questions/201898/…
    – tom
    Commented Sep 29, 2019 at 21:35
  • I don't know what problem you encountered, the link in the answer still works (tried it just now). And I think it's a good answer, concise and information-rich. (But thanks for the additional link, it might be useful to someone down the line.)
    – alexis
    Commented Sep 30, 2019 at 7:30
  • Ah, hmm sorry you are correct, don't understand it. Maybe I just didn't see the docmute stuff in the linked answer, many apologies. And yes you are correct it is a good answer - so I now feel a bit stupid, sorry.
    – tom
    Commented Oct 1, 2019 at 21:49
  • np, thanks for offering the other link :-)
    – alexis
    Commented Oct 2, 2019 at 15:40
  • Can I ask, given that you touch upon this, when using the input (or subfile) commands, to create one PDF from several tex files...is it possible to, at the same time, create individual PDF files for each tex? I can't find way to do that (other than including one file per time)
    – faccha
    Commented Sep 1, 2021 at 15:22
3

If you have multiple PDFs (say 1.pdf 2.pdf, ..., 10.pdf). To combine them in a single pdf file, simply place them in a folder that has your main tex file and type the following

\documentclass{article}
\usepackage{pdfpages} % for \includepdf[pages=-]
\usepackage{pgffor}   % for \foreach

\begin{document}

\foreach \x in {1,2,...,10}{
    \includepdf[pages=-]{\x}
}

\end{document}

This will merge all PDFs in the output of your tex file.

1
2

If you create a master file that lists the document class, and all the packages that you want to use throughout each of your standalone documents (assuming that you're using the same formatting etc), then you could then use the \include command to call any or all of the standalone sections. In your master file you could then comment out the various sections you don't want to include in your final PDF.

Here's a MWE example.

Filename: 00.master.tex

\documentclass[12pt,a4paper]{report}

\begin{document}

\pagenumbering{roman}
\include{01.title}
\include{02.contents}
\pagenumbering{arabic}
\include{03.labreport1}
\include{04.labreport2}
\include{05.labreport3}

\end{document}

Filename: 01.title.tex

\label{01.title}
\begin{titlepage}
\begin{center}
\vspace*{1in}
{\LARGE Your title here}
\par
\vspace{1.5in}
\end{center}
\end{titlepage}

Filename: 02.contents.tex

\label{02.contents}
\tableofcontents

Filename: 03.labreport1.tex

\label{03.labreport1}
\chapter{Your Chapter 1 Title Here}
\section{Your Section Here}
\subsection{Your Sub-section Here}

Filename: 04.labreport1.tex

\label{04.labreport2}
\chapter{Your Chapter 2 Title Here}
\section{Your Section Here}
\subsection{Your Sub-section Here}

Filename: 05.labreport1.tex

\label{05.labreport3}
\chapter{Your Chapter 3 Title Here}
\section{Your Section Here}
\subsection{Your Sub-section Here}

For any section you don't want to include just put a % symbol at the start of that line in the master file that has the \include command for the section you want to skip/ignore.

I hope that helps.

1
  • I think someone downvoted this because the OP specifically stated that \include doesn't offer a sophisticated enough solution.
    – jon
    Commented Jan 25, 2014 at 18:21
1

(Work in progress, but usable and tested on Linux)

I was having fun when merging around 5000 PDFs (4957 to 2018-01-18, to be exact; the UVA ARENA collection, https://github.com/dipu-bd/UVA-Arena). This is my first attempt (tested on Linux). I was thinking to add a simple Table of Contents (let's say with the name of the files), for now it's just a plain and fast merge with sort -g added.

Let's say we have those files in the pdfs folder, then we run bash mal-combine-pdfs.sh pdfs. It generates a TeX file (taking advantage of the pdfpages package) and processes it with lualatex/pdflatex.

I was wondering if we could process the TeX code directly without intermediate TeX file, we can, taken from lualatex --help:

or: luatex --lua=FILE [OPTION]... \FIRST-LINE

At the moment, I'm not 100% sure if I could publicly show you the result of my efforts (I am writing an email to the author to be sure), for now, this is it.

I am removing spaces and special (TeX) characters in the filenames manually, see How to include graphics with spaces in their path? for more details how to handle that. For non-TeX way, please see Making one PDF file from multiple PDFs or Tex?.

#!/bin/bash
# mal-combine-pdfs.sh
# mal, version 1, 2018-01-18
# Concatenate PDFs into one PDF file

# Usage:
# chmod +x mal-combine-pdfs.sh
# ./mal-combine.pdfs.sh pdfs
# or the actual directory:
# ./mal-combine.pdfs

# The disadvantage:
# It is not working when filenames contain spaces and some special characters.
# Untested when filenames contain diacritical letters.

IFS=$'\n'           # more lines to be added to the variable, bash thing
ulimit -n 10000     # increase the limit in the linux environment
tempcore=malarticle # temporary file, the core name
temp=$tempcore.tex  #      dtto     , the TeX file
rm -f $tempcore.pdf # delete our previous attempt, if any, just in case
# rm -f texput.log  # delete a log file, if previous attempt failed

malcode="\\documentclass[a4paper]{article}
\\usepackage{pdfpages}
\\begin{document}
"
malcounter=0    # number of PDF files to be concatenated
maldirectory=$1 # e.g. "pdfs", by default it is the actual directory, "."
echo "Processing PDFs from the \"$1\" directory..." # a message to the terminal

# The core, find all the PDFs
for thepdf in `find $maldirectory -type f -iname \*.pdf | sort -g`; do
let malcounter=malcounter+1
echo "Adding $thepdf..."
malcode+="\\includepdf[pages={-}]{$thepdf}
"
# A portion of files, if needed for testing
# To be uncommented (1 line)
#if [ $malcounter -eq 10 ]; then break; fi 
done
malcode+="\\end{document}"

echo "No. of PDFs: $malcounter..." # a message to the the terminal
sleep 2 # Relax for a while

echo "The TeX code to be processed is:" 
echo $malcode        # show the TeX code to the terminal
echo $malcode >$temp # save variable to the TeX file

echo "Generating the $tempcore.pdf file..."
lualatex $temp   # generating PDF from within the TeX file
# pdflatex $temp # It usually works as well, but lualatex has less restrictions.
rm -f $tempcore.{aux,log} # Kid, clean the kitchen!... ;-)
#rm -f $tempcore.tex # Kid, clean the garage!... ;-)

# A chunk for TeXists, we do not need the TeX file, a direct processing
# To be uncommented (1 line)
#lualatex $malcode   # generating PDF without TeX file

echo "The work is done! Have a nice day..." # A final message to the terminal.
0

PDF pages works really well for combining pages from several PDF documents into one document, but it does not retain the functionality of links on PDF pages. The functionality will be retained if you use Adobe, Chrome, or Preview for your PDF reader, but it does not work for Safari or Evince (for instance).

I do not have a solution for your question if you want to retain the functionality of links on your PDF documents.

You must log in to answer this question.

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