Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable syntax highlighting for Markdown inline code with Pandoc?

The Pandoc manual says:

--no-highlight

Disables syntax highlighting for code blocks and inlines, even when a language attribute is given.

This sounds like there should be syntax highlighting for inline code. But whenever I use Markdown inline code like

This is `print("Hello world")` inline code.

there is no syntax highlighting.

like image 835
finefoot Avatar asked Dec 20 '18 14:12

finefoot


People also ask

How do you get syntax highlighting in Markdown?

Many Markdown processors support syntax highlighting for fenced code blocks. This feature allows you to add color highlighting for whatever language your code was written in. To add syntax highlighting, specify a language next to the backticks before the fenced code block.

How do you format inline code Markdown?

There are two ways to format code in Markdown. You can either use inline code, by putting backticks (`) around parts of a line, or you can use a code block, which some renderers will apply syntax highlighting to.

Can pandoc convert HTML to Markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.


2 Answers

Okay, should have read a little bit further... found the solution. It's called Extension: inline_code_attributes:

Attributes can be attached to verbatim text, just as with fenced code blocks:

`<$>`{.haskell}

So the example above becomes:

This is `print("Hello world")`{.python} inline code.

Makes sense if you think about it... I'll still leave this up in case someone else has this problem.

like image 163
finefoot Avatar answered Nov 15 '22 11:11

finefoot


Behind the hood, when converting Markdown to PDF, Pandoc use the \texttt command for inline code. We can hack the \texttt command to add background color for text. Add the following command to head.tex:

\definecolor{bgcolor}{HTML}{E0E0E0}
\let\oldtexttt\texttt

\renewcommand{\texttt}[1]{
  \colorbox{bgcolor}{\oldtexttt{#1}}
}

To use head.tex, use the -H option for pandoc:

pandoc --pdf-engine=xelatex -H head.tex test.md -o test.pdf
like image 20
jdhao Avatar answered Nov 15 '22 11:11

jdhao