I've completed half a semester of Discrete Mathematics.

I have written up all 28 lectures into individual .tex files, labelled Lecture1.tex Lecture 2.tex ... Lecture28.tex.

The beginning of each one (give or take a few packages) is:


\title{Discrete Mathematics -- Lecture 28}
\author{Alec Taylor}
\date{September 16, 2011}


How can I automatically generate a single PDF with the contents of all 28 .tex files with:

  • Consecutive numbering of all pages
  • Table of Contents
  • Cover page
  • Welcome to TeX.sx! You can highlight your syntax by indenting it with 4 spaces as I have done. Also, you don't need to sign your questions since they are signed automatically.
    – raphink
    Sep 17, 2011 at 10:05
  • 1
    I've downloaded the zip file with the .tex files. There are a few problems that you may want to address and then re-upload the files in question: (1) lecture5.tex is missing; (2) files lecture1.tex through lecture9.tex use the (grossly obsolete!) latex209 \documentstyle declaration instead of latex2e's \documentclass; (3) duplicate files: lecture2/3, lecture 27/28; (4) lecture4 does not compile, crashes with a Runaway argument? message. You should probably address these issues before you ask others for advice on how to stitch together the separate files.
    – Mico
    Sep 25, 2011 at 17:31
  • When you say that your code is "up on MediaFire", I assume that the place to look for is stated in your most recent comment to my previously-posted answer, i.e., mediafire.com/?qhpem71z3m3mpzq. If this assumption is not correct, please post the appropriate URL. Thanks.
    – Mico
    Sep 25, 2011 at 17:44
  • I've been learning TeX as I go, so bound to make some mistakes on the way. I've added a bounty, so if you can get the PDF to compile properly, I'll give you the bounty :]. Feel free to edit any of the .tex files to do this (without commenting/deleting out the actual content!).
    – A T
    Sep 25, 2011 at 18:55
  • since a couple of people have reported that their solutions work when they try them with your files, i would suspect package version conflicts. if you include your .log file in your test bundle, that would allow this possibility to be checked. Sep 26, 2011 at 12:13

2 Answers 2


You can use the combine class to merge the individual lectures into one single document. The "master" file would look like this:




\title{Discrete Mathematics}
\author{Alec Taylor}

    % ...

The individual lectures are included with the \import command. Please note that you have to load all the packages you have used in your lectures in your main document. (As an alternative, you could use the packages class option which does this automatically, but this is somewhat experimental.)

In order to include all the lectures into the table of contents, I have added \usepackage{combinet} in the above example - this is an additional package which takes care of it automatically. The nomauthor option suppresses adding the author to the table of contents (as it is alway "Alec Taylor", as far as I have understood you).

  • Thanks, but unfortunately this didn't work. My .tex file: pastebin.tlhiv.org/yPwYU_Pv The error: ! LaTeX Error: Command \xP@lw already defined. Or name \end... illegal, see p.192 of the manual.
    – A T
    Sep 20, 2011 at 10:01
  • \xP@lw seems to be an internal macro used by the xypic package. It is used at the beginning of the document (with \AtBeginDocument) - but as they are multiple \begin{document} now, this confuses the package. The combine manual suggests putting \emptyAtBeginDocument directly after the \begin{document} of your main file when such problems occur, which works fine in my minimal test case.
    – diabonas
    Sep 20, 2011 at 10:16
  • Thanks, but unfortunately that didn't make a difference. :\
    – A T
    Sep 20, 2011 at 12:16
  • 1
    @AT I've set up a main file which should (hopefully :-)) compile correctly: mediafire.com/?iz1cneepddqq86c It doesn't include all your lectures, though: The lectures 4,6,8,10,17 have LaTeX errors which need to be fixed first - try compiling the single files first to see what's wrong with them. Lectures 15 and 16 are valid LaTeX code, but they use ntheorem, a package which is apparently incompatible with the combine class. BTW: Some of your lectures use \documentstyle instead of \documentclass - this is a deprecated LaTeX 2.09 command which should be avoided.
    – diabonas
    Sep 26, 2011 at 16:52
  • 1
    @AT Lecture 10: You're right, it is fine, I just didn't have the included picture, so it produced an error message. Lecture 17: Enclose the double subscript into braces: $P_{g_-}$ instead of $P_g_-$ should work fine. Lecture 4: You mixed up curly and round braces: It's \Vertex{A} (l. 25) and \EA(A){C} instead of \EA{A}(C) etc. (l. 26-34) I uploaded the corrected file here: mediafire.com/?j4tin2kz6c552l3
    – diabonas
    Sep 28, 2011 at 10:14

Given the nature of the document you're trying to create -- a single set of lecture notes -- I wonder if you'd be better off not more or less simply combining the existing files (which, from your description, are standalone TeX files with \title, \author, etc information) but, instead, took the time to (re)organize them a bit more flexibly, along the following lines.

  1. Take each individual lecture file -- for concreteness, suppose you're working on lecture1.tex -- and cut out everything between (but not including) the \maketitle and \end{document} commands. Paste the cut-out code into a new file named lecture1-body.tex, and insert the instruction \input lecture1-body in the "main" file (lecture1.tex). Repeat 28 times. This way, you'll preserve all of your "old" files, should the need to print them out as separate standalone files arise.
  2. Create a new LaTeX style file named, say, discmath.sty. In this file, you should load the various LaTeX packages you've already been loading as well as all macros and shortcuts you've created so far for the individual lectures. Go through the preambles of your 28 "main" files and scan them for \usepackage, \newcommand, and \renewcommand (and similar) instructions, and then copy-and-paste them into the new "style" file, omitting duplicates as needed. If you've been slipping in the habit of creating new macros "on the fly" inside the main bodies of the lecture files (not encouraged, obviously, but not impossible either), now's the time to move them into the main style file.
  3. Create a new driver file, say discmath-lectures.tex. Given the nature of the document you're trying to create, the book or report document classes would seem more appropriate than the article class for this new driver file. In this file, you'll first load your new style file, create a new overall title as well as a table of contents, and then (in the body of the file) load each of the 28 ...-body.tex files in turn.

This file would look something likeX

\title{Lecture Notes\\Discrete Mathematics}
\author{Alex Taylor}
\date{\nodate} % use \nodate if you don't want to show a date at all
\chapter{Introduction to Discrete Mathematics (or some other title)}
\include lecture1-body
\include lecture2-body
\chapter{Additional thoughts}
\include lecture28-body
... [to be created by you]

An issue you don't raise in your answer is whether or not each lecture is a self-contained unit (or "chapter", for the purposes of this document). The MWE above assumes that this is the case. If that's not the case, however, you should of course adjust the \chapter commands appropriately. Happy TeXing!

Addendum after the OP posted the log file of his program. I noticed the following lines toward the very end, just before "Here is how much of TeX's memory you used":

Chapter 3.
\openout2 = `l.aux'.

No file l.tex

This message strongly suggests that LaTeX is being instructed to open a file named l.tex, which doesn't exist. Is there a typo in your .tex file, possibly in the area where lecture3-body.tex should be loaded?

  • Automated individual document processing: codepad.org/izUMRp01
    – A T
    Sep 21, 2011 at 16:14
  • Unfortunately I only get 10 pages (with just what you have above minus '...'), with no content from my other .tex files. How can I make this work?
    – A T
    Sep 21, 2011 at 16:21
  • If you get 10 pages, does this mean you're getting a title page and then lectures 1 through 9 only? If so, could you check your code for a "feature" that doesn't allow for the possibility of 2-digit numbers? By the way, what does the .log file have to say on this -- does LaTex actually try to load files 10 through 28 according to the log file?
    – Mico
    Sep 21, 2011 at 20:55
  • Here's the logfile: pastebin.com/jt3F1emb - I don't get any content from my lectures (which are all 2 or more pages), just get the chapter section titles from the first tex.
    – A T
    Sep 22, 2011 at 5:23
  • Thanks for posting the .log file. See the addendum to my answer for comments.
    – Mico
    Sep 22, 2011 at 13:37

You must log in to answer this question.

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