如何对块的代码进行编号而不对其输出进行编号?

问题描述 投票:0回答:1

我正在使用 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)

示例 Python 块:

a = 5
b = 3
c = a/b

print(type(a),type(b),type(c))

Python
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">#&gt; &lt;class 'int'&gt; &lt;class 'int'&gt; &lt;class 'float'&gt;</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'>

希望这有帮助。

css r r-markdown markdown bookdown
1个回答
0
投票

从发布的 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.
© www.soinside.com 2019 - 2024. All rights reserved.