I’m trying to automate the process of taking a blog (or any collection of plain text and images) and producing a printed book, for reasons that will be stated elsewhere. These are my notes on doing so.


Working backwards: I’ll be using CreateSpace to print and bind the book. CS’s main inputs are 1) a PDF of the book’s cover and 2) a PDF of the book’s interior. I’ll figure out how to automate the creation of the cover later; for now I’ll focus on the book’s interior. I’ll be using pandoc to convert HTML or Markdown into LaTeX, which can then be used to generate the PDF. Most of the work will be in creating a suitable LaTeX template.

On my Mac, I set up the development environment for this by installing MacTeX and Homebrew, then installing pandoc from the command line with:

$ brew install pandoc

As a simple test, I took the plain tex of one of my own blog posts and saved it to simplepost.txt. I also manually added some meta-data to the top (as described on pandoc’s user guide), like so:

---
title: Imagination and Self-Doubt
author: Joel Dueck
date: July 18, 2014
---

# Imagination and Self-Doubt

...

and then ‘compiled’ to PDF:

$ pandoc -s -o out.tex simplepost.pdf
$ xelatex out.tex out.pdf

The result is a very basic PDF set in Computer Modern, suitable for printing on loose sheets of 8.5″ × 11″ paper:

In later notes I’ll be working on customizing the LaTeX template used by pandoc to get a good-looking PDF suitable for use with CreateSpace.

Since this effort happens to be geared towards created printed copies of blogs, we’ll have each blog post correspond to a chapter in the book.

Blog posts are a little different than normal book chapters, though. Book chapters usually have numbers, but posts are more commonly marked by the date on which they were posted. I had a hard time finding examples of LaTeX chapter styles which featured a date within the chapter heading itself, so I came up with my own solution.

My preferred starting point was memoir’s “dowding” chapter style, in which the chapter number appears above the chapter title, and both are centered. I copied its definition and changed it so that, instead of printing automatic chapter numbers in numbered headings (e.g., “Chapter One”) it instead prints whatever text I supply:

\makeatletter
\makechapterstyle{dowdingdate}{%
  \setlength{\beforechapskip}{2\onelineskip}
  \setlength{\afterchapskip}{1.5\onelineskip \@plus .1\onelineskip 
                            \@minus 0.167\onelineskip}%
  \renewcommand*{\chapnamefont}{\normalfont}%
  \renewcommand*{\chapnumfont}{\chapnamefont}%
  
  % Remove the word "Chapter" before the date (where the chapter 
  % number would normally be)
  \renewcommand*{\printchaptername}{}%
  
  % Print the contents of \chapterDesc in place of the chapter number
  % (except in appendices, where a simple [roman] numeral is printed)
  \renewcommand*{\printchapternum}{\centering\chapnumfont 
                                   \ifanappendix \thechapter
                                   \else \chapterDesc\fi}
  % (Original, for reference:) % 
  %\renewcommand*{\printchapternum}{\centering\chapnumfont 
  %                                 \ifanappendix \thechapter
  %                                 \else \numtoName{\c@chapter}\fi}%
  \renewcommand*{\chaptitlefont}{\normalfont\itshape\huge\centering}%
  \renewcommand*{\printchapternonum}{%
      \vphantom{\printchaptername}\vskip\midchapskip}}
\makeatother

Then I define a couple of new commands that allow me to pass an additional string of text to be used in place of each chapter’s number:

\newcommand{\setChapterDescription}[1]{%
   \def\chapterDesc{#1}%
}

\newcommand{\ChapterDate}[2]{
    \setChapterDescription{#2}
    \chapter{#1}
    \setChapterDescription{}
}

% define as empty to prevent an error the first time
\def\chapterDesc{} 

And here’s how I use all this in the template:

...
\begin{document} 
\chapterstyle{dowdingdate}

\ChapterDate{My Summer Vacation}{July 22, 2014}
...

Joel (Author) ·

The first version of the LaTeX template pictured in the previous note is now available on github as part of Simple Book Machine, which is a shell script and a system for using LaTeX templates to build a book from a collection of text files.

The output generated can theoretically be uploaded straight to CreateSpace for use in a 5.25″ × 8″ sized bound book. (I have yet to actually try this, but the PDF does meet all their requirements.) I plan to add options for a few other book sizes and designs in the future.

Joel (Author) ·