[Tex/LaTex] pandoc –listings breaks styling of code block

listingspandocpdftex

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?

Best Answer

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

Related Question