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

試著忘記壹切 提交于 2019-12-22 20:43:11

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/53870290/how-to-enable-syntax-highlighting-for-markdown-inline-code-with-pandoc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!