2

This answer provides a nice way to add background colour to inline code snippets. Using that, my YAML header includes this:

---
title: My title
subtitle: \emph{Documentation last updated on \today}
toc: true
geometry: "left=4cm,right=4cm,top=3cm,bottom=3cm"
output: pdf_document
header-includes:
 - \usepackage{fvextra}
 - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

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

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

\newpage{}

Overview
---

Queries the table `SOME_TABLE` and does something something with the data, validating Stuff and eating Pi. It then goes on to do some other stuff and finishes with doing some more stuff.

Usage
---

This explains the usage.

Area 1
---

The following sequence lays out the process.

1. Query data from `SOME_TABLE` applying the following filters:

    ```python
    COL1 == SOME_DATE
    COL2 == "SOME_STRING"
    COL3 != "ANOTHER_STRING"
    ```

2. Do something something something:

    ```python
    def get_function(param1, param2):
        if param1:
            res = dosomething(param1)
        else:
            res = dosomething(param2)
        return res
    ```

    The example continues.

However, the box around the code that contains the background colour seems to take up the whole line and "touches" capital characters in the line below it (in pdf output). How can I adjust the size of that box to take up only some proportion of the line height, for example, max the height of capital letters of the code text?

EDIT:

Added the complete document layout. The issue is well visible in the Overview section, where the box around SOME_TABLE touches the top of the capital letters in the line below it.

Also a screenshot of the original text:

enter image description here

EDIT2

The resulting tex file from this markdown document:

\newpage{}

\hypertarget{overview}{%
\subsection{Overview}\label{overview}}

Queries the table \texttt{SOME\_TABLE} and does something something with
the data, validating Stuff and eating Pi. It then goes on to do some
other stuff and finishes with doing some more stuff.

\hypertarget{usage}{%
\subsection{Usage}\label{usage}}

This explains the usage.

\hypertarget{area-1}{%
\subsection{Area 1}\label{area-1}}

The following sequence lays out the process.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Query data from \texttt{SOME\_TABLE} applying the following filters:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{COL1 }\OperatorTok{==}\NormalTok{ SOME_DATE}
\NormalTok{COL2 }\OperatorTok{==} \StringTok{"SOME_STRING"}
\NormalTok{COL3 }\OperatorTok{!=} \StringTok{"ANOTHER_STRING"}
\end{Highlighting}
\end{Shaded}
\item
  Do something something something:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ get_function(param1, param2):}
    \ControlFlowTok{if}\NormalTok{ param1:}
\NormalTok{        res }\OperatorTok{=}\NormalTok{ dosomething(param1)}
    \ControlFlowTok{else}\NormalTok{:}
\NormalTok{        res }\OperatorTok{=}\NormalTok{ dosomething(param2)}
    \ControlFlowTok{return}\NormalTok{ res}
\end{Highlighting}
\end{Shaded}

  The example continues.
\end{enumerate}
3
  • 1
    Welcome to TeX.SX! Please don't post code fragments. Instead, put your fragments into a complete compilable document that shows the problem.
    – BambOo
    Sep 5 '19 at 7:56
  • Looks related, however, my example does not have a descending letter and the box still extends down.
    – lukas
    Sep 5 '19 at 11:30
  • I just tried the answer from that question and it does improve the output, but the box still extends too far down. I'd like a formatting just as on SE, where the lower border of the box is flush with the lowest descending symbol in the code text and extends just above the highest ascending symbol (i.e. as in SOME_TABLE in my question).
    – lukas
    Sep 5 '19 at 11:44
3

If you tweak egreg's answer a little, you can optimise the output:

---
title: My title
subtitle: \emph{Documentation last updated on \today}
toc: true
geometry: "left=4cm,right=4cm,top=3cm,bottom=3cm"
output: pdf_document
header-includes:
 - \usepackage{fvextra}
 - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

 - \definecolor{bgcolor}{HTML}{E0E0E0}
 - \let\oldtexttt\texttt
 - \newcommand{\code}[1]{\begingroup\setlength{\fboxsep}{1pt}\colorbox{bgcolor}{\oldtexttt{\hspace*{2pt}\vphantom{A}#1\hspace*{2pt}}}\endgroup}
 - \renewcommand{\texttt}[1]{\code{\oldtexttt{#1}}}
---

\newpage{}

Overview
---

Queries the table `SOME_TABLE` and does something something with the data, validating Stuff and eating Pi. It then goes on to do some other stuff and finishes with doing some more stuff.

Usage
---

This explains the usage.

Area 1
---

The following sequence lays out the process.

1. Query data from `SOME_TABLE` applying the following filters:

    ```python
    COL1 == SOME_DATE
    COL2 == "SOME_STRING"
    COL3 != "ANOTHER_STRING"
    ```

2. Do something something something:

    ```python
    def get_function(param1, param2):
        if param1:
            res = dosomething(param1)
        else:
            res = dosomething(param2)
        return res
    ```

    The example continues.

enter image description here


You can improve the output further by scaling the highlighted text down:

header-includes:
 - \usepackage{fvextra}
 - \usepackage{relsize}
 - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

 - \definecolor{bgcolor}{HTML}{E0E0E0}
 - \let\oldtexttt\texttt
 - \newcommand{\code}[1]{\begingroup\setlength{\fboxsep}{1pt}\colorbox{bgcolor}{\oldtexttt{\hspace*{2pt}\vphantom{A}#1\hspace*{2pt}}}\endgroup}
 - \renewcommand{\texttt}[1]{\code{\relscale{.7}\oldtexttt{#1}}}

enter image description here

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