这个答案提供了一种向内联代码片段添加背景颜色的好方法。使用它,我的 YAML 标头包含以下内容:
---
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.
但是,包含背景颜色的代码周围的框似乎占据了整行,并“接触”了其下方行的大写字符(在 pdf 输出中)。我如何调整该框的大小以仅占据行高的一部分,例如,最大化代码文本大写字母的高度?
编辑:
添加了完整的文档布局。问题在“概述”部分中清晰可见,周围的框SOME_TABLE
触及了其下方行的大写字母的顶部。
另附原文截图:
编辑2
该 markdown 文档生成的 tex 文件:
\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}
答案1
如果你调整egreg 的回答稍微优化一下输出:
---
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.
您可以通过缩小突出显示的文本来进一步改善输出:
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}}}