Giter Site home page Giter Site logo

pandoc-book-template's Introduction

Pandoc book template

CircleCI License

Description

This repository contains a simple template for building Pandoc documents; Pandoc is a suite of tools to compile markdown files into readable files (PDF, EPUB, HTML...).

Usage

Installing

Please, check this page for more information. On ubuntu, it can be installed as the pandoc package:

sudo apt-get install pandoc

This template uses make to build the output files, so don't forget to install it too:

sudo apt-get install make

To export to PDF files, make sure to install the following packages:

sudo apt-get install texlive-fonts-recommended texlive-xetex

Folder structure

Here's a folder structure for a Pandoc book:

my-book/         # Root directory.
|- build/        # Folder used to store builded (output) files.
|- chapters/     # Markdowns files; one for each chapter.
|- images/       # Images folder.
|  |- cover.png  # Cover page for epub.
|- metadata.yml  # Metadata content (title, author...).
|- Makefile      # Makefile used for building our books.

Setup generic data

Edit the metadata.yml file to set configuration data (note that it must start and end with ---):

---
title: My book title
author: Daniel Herzog
rights: MIT License
lang: en-US
tags: [pandoc, book, my-book, etc]
abstract: |
  Your summary.
mainfont: DejaVu Sans

# Filter preferences:
# - pandoc-crossref
linkReferences: true
---

You can find the list of all available keys on this page.

Creating chapters

Creating a new chapter is as simple as creating a new markdown file in the chapters/ folder; you'll end up with something like this:

chapters/01-introduction.md
chapters/02-installation.md
chapters/03-usage.md
chapters/04-references.md

Pandoc and Make will join them automatically ordered by name; that's why the numeric prefixes are being used.

All you need to specify for each chapter at least one title:

# Introduction

This is the first paragraph of the introduction chapter.

## First

This is the first subsection.

## Second

This is the second subsection.

Each title (#) will represent a chapter, while each subtitle (##) will represent a chapter's section. You can use as many levels of sections as markdown supports.

Manual control over page ordering

You may prefer to have manual control over page ordering instead of using numeric prefixes.

To do so, replace CHAPTERS = chapters/*.md in the Makefile with your own order. For example:

CHAPTERS += $(addprefix ./chapters/,\
 01-introduction.md\
 02-installation.md\
 03-usage.md\
 04-references.md\
)

Links between chapters

Anchor links can be used to link chapters within the book:

// chapters/01-introduction.md
# Introduction

For more information, check the [Usage] chapter.

// chapters/02-installation.md
# Usage

...

If you want to rename the reference, use this syntax:

For more information, check [this](#usage) chapter.

Anchor names should be downcased, and spaces, colons, semicolons... should be replaced with hyphens. Instead of Chapter title: A new era, you have: #chapter-title-a-new-era.

Links between sections

It's the same as anchor links:

# Introduction

## First

For more information, check the [Second] section.

## Second

...

Or, with al alternative name:

For more information, check [this](#second) section.

Inserting objects

Text. That's cool. What about images and tables?

Insert an image

Use Markdown syntax to insert an image with a caption:

![A cool seagull.](images/seagull.png)

Pandoc will automatically convert the image into a figure, using the title (the text between the brackets) as a caption.

If you want to resize the image, you may use this syntax, available since Pandoc 1.16:

![A cool seagull.](images/seagull.png){ width=50% height=50% }

Insert a table

Use markdown table, and use the Table: <Your table description> syntax to add a caption:

| Index | Name |
| ----- | ---- |
| 0     | AAA  |
| 1     | BBB  |
| ...   | ...  |

Table: This is an example table.

Insert an equation

Wrap a LaTeX math equation between $ delimiters for inline (tiny) formulas:

This, $\mu = \sum_{i=0}^{N} \frac{x_i}{N}$, the mean equation, ...

Pandoc will transform them automatically into images using online services.

If you want to center the equation instead of inlining it, use double $$ delimiters:

$$\mu = \sum_{i=0}^{N} \frac{x_i}{N}$$

Here's an online equation editor.

Cross references

Originally, this template used LaTeX labels for auto numbering on images, tables, equations or sections, like this:

Please, admire the gloriousnes of Figure \ref{seagull_image}.

![A cool seagull.\label{seagull_image}](images/seagull.png)

However, these references only works when exporting to a LaTeX-based format (i.e. PDF, LaTeX).

In case you need cross references support on other formats, this template now support cross references using Pandoc filters. If you want to use them, use a valid plugin and with its own syntax.

Using pandoc-crossref is highly recommended, but there are other alternatives which use a similar syntax, like pandoc-xnos.

First, enable the filter on the Makefile by updating the FILTER_ARGS variable with your new filter(s):

FILTER_ARGS = --filter pandoc-crossref

Then, you may use the filter cross references. For example, pandoc-crossref uses {#<type>:<id>} for definitions and @<type>:id for referencing. Some examples:

List of references:

- Check @fig:seagull.
- Check @tbl:table.
- Check @eq:equation.

List of elements to reference:

![A cool seagull](images/seagull.png){#fig:seagull}

$$ y = mx + b $$ {#eq:equation}

| Index | Name |
| ----- | ---- |
| 0     | AAA  |
| 1     | BBB  |
| ...   | ...  |

Table: This is an example table. {#tbl:table}

Check the desired filter settings and usage for more information (pandoc-crossref usage).

Content filters

If you need to modify the MD content before passing it to pandoc, you may use CONTENT_FILTERS. By setting this makefile variable, it will be passed to the markdown content before passing it to pandoc. For example, to replace all occurrences of @pagebreak with <div style="page-break-before: always;"></div> you may use a sed filter:

CONTENT_FILTERS = sed 's/@pagebreak/"<div style=\"page-break-before: always;\"><\/div>"/g'

To use multiple filters, you may include multiple pipes on the CONTENT_FILTERS variable:

CONTENT_FILTERS = \
  sed 's/@pagebreak/"<div style=\"page-break-before: always;\"><\/div>"/g' | \
  sed 's/@image/[Cool image](\/images\/image.png)/g'

Output

This template uses Makefile to automatize the building process. Instead of using the pandoc cli util, we're going to use some make commands.

Export to PDF

Please note that PDF file generation requires some extra dependencies (~ 800 MB):

sudo apt-get install texlive-xetex ttf-dejavu

After installing the dependencies, use this command:

make pdf

The generated file will be placed in build/pdf.

Export to EPUB

Use this command:

make epub

The generated file will be placed in build/epub.

Export to HTML

Use this command:

make html

The generated file(s) will be placed in build/html.

Export to DOCX

Use this command:

make docx

The generated file(s) will be placed in build/docx.

Extra configuration

If you want to configure the output, you'll probably have to look the Pandoc Manual for further information about pdf (LaTeX) generation, custom styles, etc, and modify the Makefile file accordingly.

Templates

Output files are generated using pandoc templates. All templates are located under the templates/ folder, and may be modified as you will. Some basic format templates are already included on this repository, ion case you need something to start with.

References

pandoc-book-template's People

Contributors

ilikerobots avatar mofosyne avatar nnadeau avatar wikiti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pandoc-book-template's Issues

Broken PDF export on macOS

Environment

macOS with homebrew
==> texlive: stable 20220321 (bottled), HEAD
XeTeX 3.141592653-2.6-0.999994 (TeX Live 2022/Homebrew)
==> pandoc: stable 2.19.2 (bottled), HEAD

Problem

jerry@jerrys-MacBook-Pro pandoc-book-template % make pdf
mkdir -p build/pdf
awk 'FNR==1 && NR!=1 {print "\n\n"}{print}' chapters/*.md | tee  | pandoc --toc --toc-depth 2 --webtex --metadata-file metadata.yml   --template templates/pdf.latex --pdf-engine xelatex -o build/pdf/book.pdf
Error producing PDF.
! Argument of \str_uppercase:n has an extra }.
<inserted text>
                \par
l.114   \setmainlanguage[]{}

make: *** [build/pdf/book.pdf] Error 43

I'm unable to get the offending .tex intermediate file because it is piped directly to pandoc. It is possible pandoc is to new and has a bug.

pandoc-book-template license

Hello! I was wondering what license is in use for the code itself. I see the default template references a CC license as an example, but I couldn't find one for the Makefile scripts.

Many thanks!

Skipping Some Files

Is there a reason that this script would skip a few of my chapter files? I'm not sure why. I made sure they all use conforming file names and the # Titles are done properly. Perhaps it is something with Pandoc itself?

[Feature Request] Generate multipe html files instead of standalone

I need multiple html files with toc on the left pane. I tried to remove the "--standalone" parameter in makefile when generating html output, but it doesn't work. I searched on pandoc documents and still cannot get the answer, could you please help on this? thanks a lot!

[help wanted] How to create a page break?

When there is an h1 tag, a new chapter starts. And it always starts from a new page.

But there is certain stuff, which I want to start from a new page.

I read and tried multiple methods, like

  1. This stack overflow answer Pandoc EPUB Page Break
  2. This open issue in Pandoc Page-break in other output formats than LaTeX #1934

Different people have suggested different methods. But nothing seems to be working in markdown --> epub.

I want to create a new page, for certain pages, without using an h1 tag.
That means I don't want to make it a chapter. Just a page.

Any help would be much appreciated!

A word of thanks

Thanks a lot, @wikiti @mofosyne @ilikerobots, for this template and make file automation.

I was struggling for hours to use pandoc commands correctly, and how to organize everything. The official tutorial https://pandoc.org/epub.html was not that helpful. They were typing some long commands.

I was looking for organizing all chapters in a folder, with the filename as chapter name. I was unable to find a proper tutorial on the internet.

This template and the makefile really made it ultra-easy for me to do the job. It saved me a lot of time.
I was looking for something just like this

Thanks a lot to all who created this.

GitHub has not yet rolled out discussions, so I am using the issues.
Please close this issue after you read it.

[bug] "template" (line 11, column 1): unexpected "s" expecting "$endif$"

Error in building epub.

❯ make epub
mkdir -p build/epub
awk 'FNR==1 && NR!=1 {print "\n\n"}{print}' chapters/*.md | tee  | pandoc --toc --toc-depth 2 --webtex --metadata-file metadata.yml   --template templates/epub.html --epub-cover-image images/cover.png -o build/epub/book.epub
"template" (line 11, column 1):
unexpected "s"
expecting "$endif$"
make: *** [Makefile:78: build/epub/book.epub] Error 5

See this gif: I just cloned the repo and ran make epub, which was earlier working perfectly fine.

pandoc_tem

Page break after table of contents?

I would like to start a new page after the table of contents, in order to format this more like a real book. I tried to edit the template to add a \newpage or \pagebreak but that didn't work. Is there a way to do this?

Unclear what the styles.css is for?

First of all - THANK YOU for this awesome package! I've been trying to convert md to pdf in a pretty way and failed miserably because Pandoc simply works better going the latex way than html.


Now, you have the styles.css file in the project, but css is only supported via html. What is the purpose of this file then? Or how would you use it?

Make error: `Makefile:22: *** missing separator. Stop.`

Hi, I found one error when i run make epub without changing any file. i have no idea about that. could u please give me some advice?

what i did

sudo apt-get install pandoc
sudo apt-get install make
sudo apt-get install texlive-latex-base texlive-latex-recommended   texlive-latex-extra texlive-fonts-recommended
git clone https://github.com/wikiti/pandoc-book-template.git
cd pandoc-book-template/
make epub

and then i get the messages as follows:

Makefile:22: *** missing separator.  Stop.

LaTeX cross refs (obviously) don't work in epub and html

Thanks for putting this together - it's working very well with LaTeX and pdflatex.

It would be good to point out in https://github.com/wikiti/pandoc-book-template#insert-an-image that cross refs using \ref{} won't work in epub and html output.

I would also like to know if anyone has used this template with https://lierdakil.github.io/pandoc-crossref/ as this would solve some of those problems. I discovered bookdown but it seems very complex, and I'm not sure it's worth it to have the cross references be working in all the outputs. Your template seems way simpler.

Cover page for PDFs

Hi,

I have a PNG image I want to set as the cover of my document. How can I achieve that with the provided latex file ?

Thank you.

make html got error 5 in "template" (line 18, column 17)

I downloaded the code of this repository and installed packages on ubuntu (Windows Sub System), when I run make html, I got the error message below:

mkdir -p build/html
awk 'FNR==1 && NR!=1 {print "\n\n"}{print}' chapters/*.md | tee | pandoc --toc --toc-depth 2 --webtex --metadata-file metadata.yml --template templates/html.html --standalone --to html5 -o build/html/book.html
"template" (line 18, column 17):
unexpected "("
expecting "." or "$"
make: *** [Makefile:83: build/html/book.html] Error 5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.