使用rmarkdown / bookdown时如何获取源代码元素的文本换行?

问题描述

我正在使用书本准备一些文档。下面是简单的最小可重现示例:

---
papersize: a6
site: bookdown::bookdown_site

output:
  bookdown::pdf_document2: 
    keep_tex: true

---

This paragraph has inline code (really it is SHA512 sum of some object) - `2a46edf77d12d5e4f71c0ffc5fa7e7ea3ae6d96667a3d39ba8658eb5de634ee48669e6bc366509e516ba7ecda6986c52ee8cab751660a789b6d55a1c8dc8296c`.

The code block is below:

```
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
```

other style with highlightning:

```html
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
```

代码行太长。

rendering of above example

我应该在序言中添加什么以允许代码换行(分成多行),如下所示?

2a46edf77d12d5e4f71c0ffc5fa7e7ea3a
e6d96667a3d39ba8658eb5de634ee48669e  
6bc366509e516ba7ecda6986c52ee8cab75
1660a789b6d55a1c8dc8296c

<Meta http-equiv="Content-Type"
content="text/html; 
charset=utf-8">

更新:添加

header-includes:
- \usepackage{listings} \let\verbatim\undefined \let\verbatimend\undefined \lstnewenvironment{verbatim}{\lstset{breaklines,basicstyle=\ttfamily}}{}

有助于解决普通代码的问题,但是我发现我在使用一些突出显示代码片段。

中间LaTeX是here

解决方法

注意:

以下代码片段只是一些棘手的变通办法,特别是第三个代码片段通常无法正常工作,并且能够破坏很多事情……而不是使用它们,请尝试说服创建tex的工具代码,以使用合适的环境和宏。

要在Verbatim环境中添加换行符:

\usepackage{listings} 
\let\verbatim\undefined 
\let\verbatimend\undefined 
\lstnewenvironment{verbatim}{\lstset{breaklines,basicstyle=\ttfamily}}{}

要在Highlight环境中添加换行符:

\usepackage{fvextra} 
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

要在您的哈希码中添加换行符

\usepackage{seqsplit}
\renewcommand{\texttt}[1]{{\ttfamily\seqsplit{#1}}}

(再说一次,我不建议重新定义\texttt,这确实不是一个好主意,但是OP坚持要...)