Julian
Julian

Reputation: 13

Inline code in R markdown PDF file is not displayed correctly

My R markdown PDF file does not show inline code as it should. This only works correct when choosing HTML as output file.

Generally inline code should look like this. So when highlighting some words within my text, I want it to look like this.

E.g. I want to have this sentence in my PDF output: A good way to pronounce %>%when reading code is then.

Instead I get what you see here:

attached image

The image shows how the inline code is displayed in my output. Only changed font and style, instead of grey background.

I use exactly the same code as I am using here at stack overflow: `` these signs before and after the text I want to highlight.

Any tip how to fix this? I need PDF output, and not HTML.

Thanks a lot!

Upvotes: 1

Views: 821

Answers (1)

tarleb
tarleb

Reputation: 22609

Text like `this` is translated into the LaTeX command \texttt{this}. LaTeX commands can be changed, so we can modify the \texttt command to create a color box:

```{=latex}
\definecolor{codegray}{HTML}{cccccc}
\let\textttOrig\texttt
\renewcommand{\texttt}[1]{\textttOrig{\colorbox{codegray}{#1}}}
```

Add the above at the beginning of your Rmd document (right after the YAML header) to get a gray background for you inline code snippets.

Upvotes: 1

Related Questions