如何在使用 rmarkdown/bookdown 时为源代码元素换行?
How to get text-wrapping for source code elements while using rmarkdown/bookdown?
我正在使用 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">
```
源代码行太长。
我应该在序言中添加什么以允许代码换行(分成多行),如下所示?
2a46edf77d12d5e4f71c0ffc5fa7e7ea3a
e6d96667a3d39ba8658eb5de634ee48669e
6bc366509e516ba7ecda6986c52ee8cab75
1660a789b6d55a1c8dc8296c
and
<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。
注:
以下代码片段只是 hacky 变通方法,特别是第三个代码片段一般不起作用,并且有能力破坏很多东西......请尝试说服工具,而不是使用它们,创建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坚持...)
我正在使用 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">
```
源代码行太长。
我应该在序言中添加什么以允许代码换行(分成多行),如下所示?
2a46edf77d12d5e4f71c0ffc5fa7e7ea3a e6d96667a3d39ba8658eb5de634ee48669e 6bc366509e516ba7ecda6986c52ee8cab75 1660a789b6d55a1c8dc8296c
and
<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。
注:
以下代码片段只是 hacky 变通方法,特别是第三个代码片段一般不起作用,并且有能力破坏很多东西......请尝试说服工具,而不是使用它们,创建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坚持...)