Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc Syntax Highlighting in PDF not working

pandoc --version yields:

pandoc 1.12.2.1
Compiled with texmath 0.6.5.2, highlighting-kate 0.5.5.1.
Syntax highlighting is supported for the following languages:
    actionscript, ada, apache, asn1, asp, awk, bash, bibtex, boo, c, changelog, clojure, cmake, coffee, coldfusion, commonlisp, cpp, cs, css, curry, d, diff, djangotemplate, doxygen, doxygenlua, dtd, eiffel, email, erlang, fortran, fsharp, gnuassembler, go, haskell, haxe, html, ini, java, javadoc, javascript, json, jsp, julia, latex, lex, literatecurry, literatehaskell, lua, makefile, mandoc, markdown, matlab, maxima, metafont, mips, modelines, modula2, modula3, monobasic, nasm, noweb, objectivec, objectivecpp, ocaml, octave, pascal, perl, php, pike, postscript, prolog, python, r, relaxngcompact, rhtml, roff, ruby, rust, scala, scheme, sci, sed, sgml, sql, sqlmysql, sqlpostgresql, tcl, texinfo, verilog, vhdl, xml, xorg, xslt, xul, yacc, yaml
Default user data directory: /home/xiaolong/.pandoc
Copyright (C) 2006-2013 John MacFarlane
Web:  http://johnmacfarlane.net/pandoc
This is free software; see the source for copying conditions.  There is no
warranty, not even for merchantability or fitness for a particular purpose.

I am trying the following:

```python
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Oct 19 2015, 21:52:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random  # seed is determined here, if not set by the developer themself
>>> random.randint(0, 100)
50
>>> random.randint(0, 100)
62
>>> random.randint(0, 100)
53
>>> random.randint(0, 100)
17
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.randint(0, 100)
88
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.randint(0, 100)
88
````````````````````````````````````````````````````````````````````````````````

The command to compile my PDF I use is:

pandoc --read=markdown --table-of-contents --toc-depth=2 --preserve-tabs --standalone --template=template.latex --latex-engine=xelatex Hausarbeit.md --highlight-style=pygments -o Hausarbeit.pdf

This is code I have in my template.latex:

$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$

According to the Pandoc documentation Pandoc documentation this should result in highlighted text for the Python language. However, everything in that code block only becomes monospace and no colors are applied.

What's more is, that there is an error when compiling:

pandoc: Error producing PDF from TeX source.
! LaTeX Error: Environment Shaded undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

\GenericError  ...                                
                                                  \endgroup 
l.200 \begin{Shaded}

My guess is, that there is something missing in the template to define Shaded. I even installed some package of highlighting-kate named libghc-highlighting-kate-dev, because I read somewhere, that this is what pandoc uses, but to no avail, the text stays black.

How do I get the syntax highlighting working?

EDIT#1

That error did not appear, when I simply indented the code to use standard markdown syntax for code. But in that case I'll not have any syntax highlighting per se.

EDIT#2

The tags "PDF", "Python" and "syntax-highlighting" are indeed related, since this is specifically about creating a PDF file and specifically about syntax highlighting. Also it could be that there is only a problem with highlighting python code in the created PDF files, so the python tag is also related. Please read the question before concluding too quickly.

like image 603
Zelphir Kaltstahl Avatar asked Dec 09 '15 13:12

Zelphir Kaltstahl


People also ask

Can pandoc convert from PDF?

You can't. You can try opening the PDF in Word or Google Docs and saving in a format from which pandoc can convert directly.

Can pandoc convert PDF to markdown?

Commonly used markup languages include Markdown, ReStructuredText, HTML, LaTex, ePub, and Microsoft Word DOCX. In plain English, Pandoc allows you to convert a bunch of files from one markup language into another one. Typical examples include converting a Markdown file into a presentation, LaTeX, PDF, or even ePub.

Does pandoc need LaTeX?

By default, pandoc will use LaTeX to create the PDF, which requires that a LaTeX engine be installed (see --pdf-engine below). Alternatively, pandoc can use ConTeXt, roff ms, or HTML as an intermediate format.


1 Answers

Try to add --listings option to your pandoc compile command:

pandoc --read=markdown --table-of-contents --toc-depth=2 --preserve-tabs --standalone --template=template.latex --latex-engine=xelatex --listings Hausarbeit.md --highlight-style=pygments -o Hausarbeit.pdf
like image 187
kvaleev Avatar answered Sep 23 '22 01:09

kvaleev