Thursday, March 24, 2011

Publishing conference proceedings using LaTeX part II: concatenating pdf files

This is the second post on the series "Publishing conference proceedings using LaTeX". The first one dealt with guidelines sent to authors to have coherent files. In this part, I will explain how to merge all articles into a single file.

Concatenating pdf files in LaTeX



It would be easy to do so using pdftk for instance; However, I wanted to add page numbers and some information in the "header", i.e., the first line at the top of a page. So I went for another option, the pdfpages package.

This package lets you include pages of a pdf file directly into you document. I won't go into details (you can read the documentation for that) and only show how I used it:

\includepdf[pages=1-, pagecommand={},%
  addtotoc={1,chapter,0,Title\qquad{\it Authors},paper:X}]%
  {papers/paper_X.pdf} 

This include all pages of the file given in argument. The pagecommand option is used to pass LaTeX code that will be executed on each added page. The default is
pagecommand={\thispagestyle{empty}})
which is not what I wanted (I wanted page numbers and headers!), so I passed an empty code.
The addtotoc creates an entry in the table of contents (TOC), which is nice to have at the beginning of the proceedings; Its options are:
  • 1: the hyperlink will jump to page 1 of the include file.
  • chapter: tells to typeset the line in the TOC as the \chapter command.
  • 0: is the "depth" in sectioning (section is 1, chapter 0, etc.).
  • "Title..." is the text displayed in the toc.
  • "paper:X" is a label on the included pages (probably the first one), in case you want to \ref it.


To include all files more easily, I used the \foreach command of the TikZ package. I also used my trick for adjusting two paragraphs (the \alignpars macro) so that the article title will be align to the left and the authors or the right, with "leaders" (dotted line) linking them (although I had a problem with the \vphantom trick which does not seem to be allowed in a TOC, and had to replace it with \hbox to 0pt{}).

So I first devised a macro to add an article:
\newcommand\addarticle[3]{
  \clearpage{\pagestyle{empty}\cleardoublepage}
  \includepdf[pages=1-, pagecommand={}, 
  addtotoc={1,chapter,1,{\alignpars{#1}{\it #2}},paper:#3}]  
     {papers/paper_#3} 
}

The first line of the macro clears the last page; Then, if we are on an even page we also clears this one (so that every article starts on an odd-numbered page) while removing any headers/footers (e.g., page numbers) from this page.

I then called the macro from inside a \foreach loop. Here is an example with only two dummy articles (I had 10 articles):

\foreach \file/\title/\authors in {
   1.pdf/
   {First article}/
   {Some Authors},
%
   2.pdf/
   {Second article}/
{Some other Authors}}
%
{\addarticle{\title}{\authors}{\file}}

In the next (and last) part, I will explain how to do some more polishing to the proceedings, with sessions in the TOC for instance.

No comments:

Post a Comment