Self-Publishing with Pandoc and Latex: A Basic Guide

When I first started writing my novel it was in LibreOffice Writer, but I quickly realized that while it worked well for essays and even my thesis, it was not ideal for writing fiction. At least, not for me. I wanted something stable and flexible enough to handle tens of thousands of words of dark, realistic fantasy. In addition, I wanted to make sure that when those tens of thousands of words were ready for publishing, I could convert the manuscript fairly easily from a single master file.

This guide is what I wish I had when I started, and I'm putting it together in case anyone else is curious or wants to use free and open source software to write and publish their novel.

Step 1. Markdown

Markdown is a markup language with the goal of being natural to read and use. If you are familiar with HTML, it should be a quick start.

You don't really need to install anything to start using it, however I would suggest a dedicated markdown editor such as my personal favorite, Ghostwriter, to make the experience more streamlined. It's fairly minimal, but that's kind of the point. Writing requires focus, and Markdown does a great job of being practical and flexible while getting out of the way of the words.

Honestly, markdown is so streamlined you can use pretty much any program you want to write fiction in it. Here's a quick example:

Markdown:

# Part I


## Chapter 1
 

Once upon a time, there was a writer who wanted to write *in italics.* He felt, however, that the sentence was not strong enough *in italics,* so he wrote it **in bold**. Satisfied, he moved on to the next part by making a horizontal line.


---


*__Then he wrote the most important sentence he had ever written. So he bolded and italicized it.__*

>Then when it came time to write a memoir about it, he put it in a block quote.

# The End

Result:

Part I

Chapter 1

Once upon a time, there was a writer who wanted to write in italics. He felt, however, that the sentence was not strong enough in italics, so he wrote it in bold. Satisfied, he moved on to the next part by making a horizontal line.


Then he wrote the most important sentence he had ever written, so he bolded and italicized it.

Then when it came time to write a memoir about it, he put it in a block quote.

The End

You can refer to a guide for more detailed information, but as you can see, it's fairly easy to get the hang of – especially since fiction writing does not require complex formatting. In addition, Markdown let me use a cool digital typewriter to do most of my drafting. Sometimes you have to go to extreme measures to avoid distractions.

Step 2. Pandoc

Markdown would not be that useful for authors if Pandoc did not exist. Pandoc allows you to convert your glorious manuscript.md into pretty much any file format under the face of the sun. Follow the installation instructions for your OS/distro and let's roll.

Keep in mind Pandoc is a command-line program, which might be intimidating if you have never used a command line before, but their documentation is top-notch and with a little patience you'll be generating .epubs and .pdfs like a real hacker.

E-book

The .epub file type is the standard for e-books. Amazon has .mobi, but since you can upload to KDP with .epub it's not really worth it to generate with Pandoc unless you have a kindle that you want to export your manuscript to.

Generating .epubs is fairly simple with Pandoc, as the formatting requirements are not as strict as print-ready .pdfs, but it is not without its challenges. If you feel confident you can skip my guide and go right to Pandoc's guide for creating .epubs. Otherwise here's a basic step by step:

First, navigate to the directory where your manuscript is located, then open a terminal/shell. On the command line type:

pandoc yourmanuscript.md -o yourbookname.epub

Then press enter. Boom! You now have an .epub. Well done!

We're not done, however. Something useful to include is a table of contents, and fortunately, Pandoc can handle that. Simply add --toc after Pandoc.

Another option I used is --top-level-division=part. This will tell Pandoc to define the highest level heading in your manuscript as a part rather than a chapter. If you don't use parts, you can skip this because it is set to chapter by default. Altogether it will look something like this:

pandoc --toc --top-level-division=part yourmanuscript.md -o yourbookname.epub

Before you upload and become a self-published millionaire, make you sure you take care of your metadata. This is pretty easy with Pandoc. Just add a yaml metadata block to the top of your manuscript. It'll look something like this:

---
title:
- type: main
  text: My Awesome Title
creator:
- role: author
  text: My Awesome Name
publisher: My Awesome Publishing Company
identifier: 
- scheme: ISBN-13
  text: 978-0-57-855858-5
rights: © Year My Awesome Name
rights: All Rights Reserved
---

With the yaml block at the top of your document, Pandoc will be able to read it and attribute it to the .epub. For more documentation click here.

Step 3. Latex

Here's where it gets juicy. Pandoc does a pretty decent job of outputting .pdfs by default, but figuring out how to format them for print on demand took me a lot longer than I thought it would.

Pandoc uses a default template to format the .pdfs, and while they look okay, they were not adequate for print on demand. I decided the easiest way to get the .pdfs I wanted was to modify the Pandoc Latex template and tell Pandoc to use that template. Fortunately, you don't have to sit through the long hours of tinkering it took me to get that working.

First, make sure you have latex installed. Latex is a .pdf engine that is capable of making beautiful print-ready documents. On most linux distributions, there is a handy “texlive-all” package you can install to get all the dependencies and extensions. On Windows and Mac, Pandoc recommends installing latex via MiKTeX.

Next, let's copy the default Pandoc template so we can modify it. The easiest way to do this is to tell Pandoc to output its default latex template into our custom template with:

pandoc -D latex > custombook.latex

Alternatively, you can go to the directory where Pandoc stores the templates, find “default.latex”, copy it, and rename it.

Next, open your custom template file and add these modifications after line 7:

% DEFINE DOCUMENT CLASS HD
\documentclass{book}


% DEFINE PHYSICAL DOCUMENT SETTINGS HD
% media settings
\usepackage[paperwidth=5.5in, paperheight=8.5in]{geometry}

% FORMAT CHAPS AND HEADER HD
\usepackage{titlesec} % make chapters start on a new page, and remove auto-generated chapter headings HD
\titleformat{\chapter}[display]
	{\normalfont\bfseries}{}{0pt}{\Large}

\usepackage{tocstyle} %make the TOC pretty HD
\usetocstyle{noonewithdot}


\usepackage{fancyhdr} % make the headers pretty HD
\pagestyle{fancy}
\fancypagestyle{plain}{%
	\fancyhead{}
	\renewcommand{\headrulewidth}{0pt}
	}
\fancyhead{}
\fancyhead[RO, LE]{\leftmark}
\renewcommand{\headrulewidth}{0pt}

\makeatletter % remove header from chapter pages automatically HD
  \def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
  \hbox{}
  \vspace*{\fill}
  \vspace{\fill}
  \thispagestyle{empty}
  \newpage
  \if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother

After you modify the custom template, you have to tell Pandoc to use it with --template=custombook.latex. Make sure your template is in the same directory as Pandoc's defaults. If you don't know where those are you can search for the file “default.latex”.

Now we're ready to generate it. Here's what your final command should look like:

pandoc --toc --template=custombook.latex yourmanuscript.md -o yourbook.pdf

In order to format the table of contents satisfactorily I had to remove the numbers from my chapter headings in my manuscript because --toc numbers the sections and it looked odd. Your mileage may vary but if you figure out an easier way to do this, let me know.

Step 4. Publish

Congrats! Now you can upload your manuscript to Amazon, Barnes and Noble, Kobo, Ingram Spark, and just about anywhere you want. I won't bore you with a tutorial for those because if you got this far, you can figure out those web interfaces pretty easily.

For a cover, I used Canva and a vector drawing of the moon I did with Inkscape. I was able to use the same .png for all e-book platforms, but for print, I downloaded the .pdf templates for each one and used LibreOffice Draw to modify them. There are myriad cover tutorials out there if you need help, or you can hire a designer at a marketplace like Reedsy.

#nonfiction #howto #publishing


First, thank you for reading! I sincerely hope it helps. To echo a sentiment from Thomas Hardy, it is a great regret that I will never be able to meet many of you in person and shake your hand, but perhaps we can virtually shake hands. It is a poor substitute, but it will have to do in this strange world. I promise I will not gum up your inbox.


Send me a kind word or a cup of coffee:

Patreon | Ko-Fi | Podcast | Mastodon | Twitter | Github