2

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?

  • do you have the listings package included in the *.tex file? – naphaneal Jan 27 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 at 18:14
  • please provide a full minimal working example, that reproduces your problem. – naphaneal Jan 27 at 18:17
  • This is full example. There is no more code than I provided in question. – Everettss Jan 27 at 18:28
  • 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: tex.stackexchange.com/a/99638/29873 – DG' Jan 27 at 20:46
3

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

  • sounds like you could also provide a custom highlighting style with \lstset{} – naphaneal Jan 28 at 9: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: tex.stackexchange.com/a/99638/29873 – DG' Jan 28 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.