我正在使用 RMarkdown 中的 bookdown 包编写一本在线书籍 (bs4_book)。在 markdown 中有 Python 块,具有简单的操作,例如导入库和算术计算。我一直在寻找一种方法,通过更改 style.css
文件内的参数来对
Python块内的行进行编号。虽然我成功了,但这种方法也会对单元格的输出进行编号,我个人不喜欢它。 我知道必须有一种方法可以在
.css
文件中定义它,但我不知道语言。
style.css
文件:p.caption {
color: #777;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}
pre.sourceCode {
counter-reset: line;
line-height: 0.8;
}
pre.sourceCode > code > span {
display: block;
counter-increment: line;
}
pre.sourceCode > code > span:before {
content: counter(line);
display: inline-block;
border-right: 1px solid #ddd;
padding-right: 0.5em;
margin-right: 0.5em;
color: #888;
}
pre.output, pre.verbatim {
counter-reset: none !important;
padding-left: 0 !important;
list-style-type: none !important;
border: none !important;
}
pre.output > code > span:before, pre.verbatim > code > span:before {
content: none !important;
}
_output.yml
:bookdown::bs4_book:
css: style.css
config:
numbering:
fig: true
theme:
bootswatch: litera
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default
_bookdown.yml
文件:book_filename: "online-book"
number_sections: true
fig_caption: true
new_session: true
before_chapter_script: _common.R
delete_merged_file: true
library(knitr)
library(reticulate)
reticulate::use_condaenv("python", required = TRUE)
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, comment = '>>', message = FALSE, highlight = TRUE, cache.lazy = FALSE)
knitr::knit_engines$set(python = reticulate::eng_python)
a = 5
b = 3
c = a/b
print(type(a),type(b),type(c))
html
代码:<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="prueba.html#cb2-1" tabindex="-1"></a>a <span class="op">=</span> <span class="dv">5</span></span>
<span id="cb2-2"><a href="prueba.html#cb2-2" tabindex="-1"></a>b <span class="op">=</span> <span class="dv">3</span></span>
<span id="cb2-3"><a href="prueba.html#cb2-3" tabindex="-1"></a>c <span class="op">=</span> a<span class="op">/</span>b</span>
<span id="cb2-4"><a href="prueba.html#cb2-4" tabindex="-1"></a></span>
<span id="cb2-5"><a href="prueba.html#cb2-5" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(a),<span class="bu">type</span>(b),<span class="bu">type</span>(c))</span>
<span id="cb2-6"><a href="prueba.html#cb2-6" tabindex="-1"></a><span class="co">#> <class 'int'> <class 'int'> <class 'float'></span></span></code></pre></div>
1| a = 5
2| b = 3
3| c = a/b
4|
5| print(type(a),type(b),type(c))
6| >> <class 'int'> <class 'int'> <class 'float'>
1| a = 5
2| b = 3
3| c = a/b
4|
5| print(type(a),type(b),type(c))
>> <class 'int'> <class 'int'> <class 'float'>
希望这有帮助。
从发布的 HTML 代码来看,Python 输出似乎位于标有
<span>
的 class="co"
中。 我不是 CSS 专家,但我相信做到这一点的方法是将你的 CSS 代码修改为如下所示:
pre.sourceCode > code > span.not('co') {
display: block;
counter-increment: line;
}
pre.sourceCode > code > span.not('co'):before {
content: counter(line);
display: inline-block;
border-right: 1px solid #ddd;
padding-right: 0.5em;
margin-right: 0.5em;
color: #888;
}
Hopefully someone who does know CSS better will comment or correct this if I have the syntax wrong.