Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Self-Publishing via Markdown

short url: caseywatts.com/selfpublish


Markdown --> PDF (as a booklet!)

Markdown --> EPUB and MOBI

I'm working on a book, "Debugging Your Brain" which I plan to self-publish. I'm writing the chapters in Markdown, and I want to have it both printed on paperback and available as an eBook on Kindle etc. Here are the tools I used to get my markdown files (from my Jekyll based blog) into the PDF, EPUB, and MOBI formats.

Overview

  • pandoc is a command line tool used to convert from certain file types to other file types
  • latex is a typesetting language / software used to make pdfs. I've seen this used most often in academia that uses equations, like math, physics, and computer science classes in undergraduate.
  • epub is the standard eBook format. As far as I've seen, this is used by everything except Kindle.
  • mobi is Kindle's eBook format. We'll use kindle's command line tool to convert an epub to mobi.

Pandoc Setup

pandoc is the main tool to convert markdown to other things

sudo apt-get update 
sudo apt-get install pandoc

Amazon Self-Publishing

Amazon Royalties: https://blog.reedsy.com/amazon-self-publishing-royalties/   https://kdp.amazon.com/en_US/help/topic/G201834330  

https://www.amazon.com/gp/seller-account/mm-summary-page.html/ref=footer_publishing?ld=AZFooterSelfPublish&topic=200260520&ie=UTF8   https://kdp.amazon.com/en_US/help/topic/G200735480 

first set up pandoc (above)

Markdown to eBook

To get your markdown into an eBook format, you'll first convert it to epub which pandoc can do on its own. Many eBook readers can use epub directly. To make a file compatible with Kindle, you'll then need to convert the epub file to mobi using the kindlegen command line command.

Markdown to EPUB

pandoc -o BOOKTITLE.epub -f markdown_github -t epub SOURCE1.md SOURCE2.md

Markdown to MOBI

Kindle needs mobi files. You can use the kindlegen command line command to convert epub to mobi. I got this download trick from this article, and the single-file-extract trick from this article.

  1. Get the most recent download url for the kindlegen "Linux Download" from the kindlegen webpage.
  2. Download, un-tar (unzip), and move the executable to /usr/bin so it will available as a command.
curl KINDLEGEN_DOWNLOAD_URL | tar xvz kindlegen
sudo mv kindlegen /usr/bin/kindlegen

Confirm you have it by restarting your terminal and running:

kindlegen -v

(actually this isn't working for me - I might try the Kindle Previewer for conversion instead for now)

first set up pandoc (above)

Markdown to Printed Booklet

Getting the pdflatex command

To use pandoc to generate pdf you must have the pdflatex command line command installed. The easiest way to get that is through these texlive packages. I got this trick from this github gist.

sudo apt-get install texlive-latex-base
sudo apt-get install texlive-fonts-recommended
sudo apt-get install texlive-fonts-extra
sudo apt-get install texlive-latex-extra

If these succeeded you should see a version number when you run this command:

pdflatex -v

Example PDF Command

pandoc --output OUTPUT_FILE_NAME.pdf  --from markdown_github+yaml_metadata_block --to latex SOURCE_FILE_1.md SOURCE_FILE_2.md
  • --from: If you are using markdown pages from a Jekyll blog site, these files will be a combination of markdown_github and yaml_metadata_block, and we can combine these with the +.
  • --to: to make a pdf we're using latex as our intermediary
  • --output: put your destination file name here. If you want it to be a pdf, make sure the extension is .pdf. For troubleshooting, you can change the file extension to .tex to see the intermediary LaTeX file that's used to make the pdf.

LaTeX Configuration  

The main reference for this is Pandoc's PDF creation documentation.

You can put LaTeX configuration in the yaml frontmatter block in the first file, and this will apply to all following pages as well. The frontmatter needs a --- at the top of your file, your context after that, and then another --- after, just like Jekyll blog posts. I opted to have a separate file for just this configuration versus having it at the top of my first chapter, but that's up to you.

Gotcha: make sure your pandoc command's --from contains BOTH markdown and yaml frontmatter (see above).

LaTeX Booklet Settings

To get latex to export to half-sheets, here are my yaml frontmatter with the booklet settings. I got some tips from this post. You can see the documentclass options visually on this blog post, and you can format them in yaml according to pandoc's Variables for LaTeX section. Passing options to the geometry package is interesting, because you have to pass "paperheight=8.5in" as a string with quotes, not as a nested key:value pair like other things.

---
title: Debugging Your Brain
author: Casey Watts
documentclass: book
classoption:
- openany
geometry:
- "paperheight=8.5in"
- "paperwidth=5.5in"
---
  • title and author go into the file's metadata as well as cover page
  • documentclass: book sets a lot of settings to reasonable ones for us (like asymmetrical margins to account for the book binding taking up space)
  • openany says that chapters can start on the left side pages as well as right
  • geometry settings here specify half of a "letter" sized piece of paper (folding the 11" side to be 5.5")

LaTeX Double Spacing

You may want to double-space the document for reviewers to write comments in. Add this to the bottom of your yaml frontmatter.

---
linestretch: 2
---

Adobe Booklet Printing

Adobe PDF Reader has booklet printing built in! There is a "Booklet" setting under "Page Sizing & Handling". See more detailed instructions in this Adobe help article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.