Pandoc: Howto: How do I include line numbers in the final PDF?

Created on 5 Oct 2016  路  5Comments  路  Source: jgm/pandoc

My apologies if this is noob-ish, but I was wondering if it might be possible to include line numbers for the text when converting from markdown to PDF? In case it helps, my use case is that I'm writing academic papers in markdown, but need to submit a PDF version (that includes line numbers in the output) when submitting it online.

Most helpful comment

I think you have to give the \linenumbers command as well.

``` .markdown

header-includes:

  • usepackage[left]{lineno}
  • linenumbers
    ...

My text.
```

@ericmjl -- Note that pandoc produces PDFs by going through LaTeX (by default). So if you want to know how to add a feature, it's usually a question of finding the right LaTeX package.

All 5 comments

Either in separately included LaTeX file or using YAML metadata on top of your Markdown document, add \usepackage[left]{lineno}. If you decided to use YAML metadata then it would something like this:

---
header-includes:
  - \usepackage[left]{lineno}
...

My text.

I think you have to give the \linenumbers command as well.

``` .markdown

header-includes:

  • usepackage[left]{lineno}
  • linenumbers
    ...

My text.
```

@ericmjl -- Note that pandoc produces PDFs by going through LaTeX (by default). So if you want to know how to add a feature, it's usually a question of finding the right LaTeX package.

@wilx @jkr: wow you guys are amazing!!!!! :smile: :smile: :smile:

I think I get the logic now - if I want to use a LaTeX package, basically include it in the header-includes section of the YAML header. Is that correct?

I think I get the logic now - if I want to use a LaTeX package,
basically include it in the header-includes section of the YAML
header. Is that correct?

Yes. Or, if you want more control, you can change the template that
pandoc uses to create latex output.

  1. type pandoc -D latex > mytemplate.latex to get the default
    template.
  2. add any packages / make any changes you want to it.
  3. save mytemplate.latex in $DATADIR/templates (~/.pandoc/templates
    on linux or OSX)
  4. run pandoc --template=mytemplate foo.md -o foo.pdf

This works the same for other output formats (html, etc) as well.

See http://pandoc.org/MANUAL.html#templates for more info.

One more possible way to do this is to put the \usepackage and setup lines into separate, say, header.tex file and then include it in preamble by invoking pandoc with --include-in-header=header.tex.

Was this page helpful?
0 / 5 - 0 ratings