5

Considering this input file:

~~~{.html caption="test caption"}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Store</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
~~~

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

When I run command:

pandoc ./DOC.md -o ./doc.pdf

Listing has nice stylings:

styled lysting

I want to have also listing captions. So a lot of people suggest to use --listings package.

But after I run

pandoc ./DOC.md -o ./doc.pdf --listings

The output file is missing styles for code block!

enter image description here

How to configure pandoc to have nice styles of code block and caption at the same time?

Everettss
  • 153
  • do you have the listings package included in the *.tex file? – naphaneal Jan 27 '19 at 17:58
  • No, but I just tried and no luck, still the same results. I've added this header pandoc -H ./template.tex ./DOC.md -o ./doc.pdf --listings and template.tex contains only \usepackage{listings}. – Everettss Jan 27 '19 at 18:14
  • 1
    please provide a full minimal working example, that reproduces your problem. – naphaneal Jan 27 '19 at 18:17
  • This is full example. There is no more code than I provided in question. – Everettss Jan 27 '19 at 18:28
  • you do have the package installed on your system correctly? – naphaneal Jan 27 '19 at 19:23
  • I have no idea. How to check if it is installed on my system? Usually, if I did not have package installed pandoc throws an error, here is not complaining. – Everettss Jan 27 '19 at 19:43
  • run kpsewhich listings.sty from the shell. if no error is thrown, it's installed properly and the error lies somewhere else. – naphaneal Jan 27 '19 at 19:57
  • Yes I have it installed kpsewhich listings.sty produce /usr/local/texlive/2018basic/texmf-dist/tex/latex/listings/listings.sty – Everettss Jan 27 '19 at 20:04
  • 2
    The listings output you have posted isn't missing syntax highlighting. The highlighting is just different, i.e. it is monochrome, roman font and uses bold font for keywords. You can customize the output of listings to fit your needs, here is a good starting point: https://tex.stackexchange.com/a/99638/29873 – DG' Jan 27 '19 at 20:46

2 Answers2

4

One way of adding captions to code blocks is by using pandoc-crossref:

~~~{#lst:captionAttr .html caption="test caption"}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Store</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
~~~


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Invoke pandoc with pandoc-crossref as a filter:

$  pandoc --filter pandoc-crossref doc.md -o doc.pdf

enter image description here


Another way would be to use an existing color scheme for listings. There is an implementation of the solarized theme from which you could start:

  1. Get the package from github: https://github.com/jez/latex-solarized

  2. Tell pandoc to include the package in the LaTeX preamble by adding a YAML block:

---
header-includes: \usepackage{solarized-light}
---

resulting in

---
header-includes: \usepackage{solarized-light}
---

~~~{.html caption="test caption"}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Store</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
~~~

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  1. Run pandoc:
$ pandoc --listings doc.md -o doc.pdf

enter image description here

DG'
  • 21,727
  • sounds like you could also provide a custom highlighting style with \lstset{} – naphaneal Jan 28 '19 at 09:28
  • 1
    @naphaneal Exactly, that is what the solarized package for listings is doing. You can take the color definitions found there and tweak them to match your preferred highlighting style. Or just search on this site, there are nice examples too: https://tex.stackexchange.com/a/99638/29873 – DG' Jan 28 '19 at 09:41
0

I had the same issue, and it turns out that some style was missing for --listings option. I copied parts of the logic from eisvogel.latex template and added some custom styling over it.

\usepackage{listings}
\newcommand{\passthrough}[1]{#1}  % required from pandoc to provide inline

\usepackage{xcolor} % custom colors

\definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{ backgroundcolor=\color{backcolour}, commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false, breaklines=true, captionpos=b, keepspaces=true, %numbers=left, % I recommend disabling this, as one can use .numberLines in markdown numbersep=5pt, showspaces=false, showstringspaces=false, showtabs=false, tabsize=2 }

\lstset{style=mystyle}

% if you want to use cleveref \usepackage[nameinlink]{cleveref} % must come after hyperref package

\crefname{listing}{code}{codes} \Crefname{listing}{Code}{Codes}

and the following works fine:

~~~{label=lst:captionAttr .html .numberLines caption="test caption"}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Store</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
~~~

Referencing [@Lst:captionAttr] using cleveref.

Feel free to test in the Pandoc template for Legrand Orange Book: https://github.com/igormcoelho/pandoc-template-legrand-orange-book

pandoc --listings -s -F pandoc-crossref --citeproc --template=orangelegrand.latex book.md -o book.pdf