[Tex/LaTex] Markdown to pdf with pandoc : how to get color for code framed by a single back-tick

backgroundscodecolormarkdown

To print .md to .pdf I use pandoc. (due to many contributor in this forum. Thx to them)
problem = I would like to have (in the pdf) the same background color for the code no matter if I use triple backtick or simple backtick
eg:
` code surrounded by one backtick = no background color `
but
“` code with tree backticks = background color OK “`

I'm a noob in Latex, if I could get some help…

my settings

  • My Command :
#!/bin/bash

pandoc "$1" \
    --include-in-header inline_code.tex \
    --include-in-header listings-setup.tex \
    -V linkcolor:blue \
    -V geometry:a4paper \
    -V geometry:margin=2cm \
    -V mainfont="DejaVu Serif" \
    -V monofont="Space Mono" \
    --listings \
    --pdf-engine=xelatex \
    -o "$2"
  • the file listings-setup.texused with --listings
\usepackage{xcolor}

\lstset{
    basicstyle=\ttfamily,
    extendedchars=true
    numbers=left,
    numberstyle=\tiny\ttfamily,
    backgroundcolor=\color[RGB]{248,197,196},
    showstringspaces=false,
    tabsize=2,
    columns=fixed,
    frame=trbl,
    frameround=tttt,
    framesep=7pt,
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},

  • the file inline_code.tex
\definecolor{Light}{HTML}{F4F4F4}

\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{
  \colorbox{Light}{\oldtexttt{#1}}
}

Easier to understand my broken english with a picture :
enter image description here

Best Answer

With the option --listings pandoc uses \lstinline for inline code (not \texttt), so you have to patch \lstlinsting. You can do so with egreg's code here. The code below replaces the code in your listings-setup.tex (you don't need inline_code.tex anymore, since it's all done with listings):

\usepackage{xpatch,realboxes}
\usepackage{xcolor}
\definecolor{Light}{HTML}{F4F4F4}

\lstset{
    basicstyle=\ttfamily,
    extendedchars=true
    numbers=left,
    numberstyle=\tiny\ttfamily,
    backgroundcolor=\color[RGB]{248,197,196},
    showstringspaces=false,
    tabsize=2,
    columns=fixed,
    frame=trbl,
    frameround=tttt,
    framesep=7pt,
    breaklines=true, postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}
}


\makeatletter
\xpretocmd\lstinline{\Colorbox{Light}\bgroup\appto\lst@DeInit{\egroup}}{}{}
\makeatother

Now call

pandoc infile.md \
    --include-in-header listings-setup.tex \
    -V linkcolor:blue \
    -V geometry:a4paper \
    -V geometry:margin=2cm \
    -V mainfont="DejaVu Serif" \
    -V monofont="Space Mono" \
    --listings \
    --pdf-engine=xelatex \
    -o outfile.pdf

The result looks like this:

enter image description here

Related Question