像写论文一样写 Hexo:支持自动编号的定理环境,以及 bibtex 参考文献

69 天前
 mathzhaoliang

我有一个数学相关的博客,里面的文章经常要用到定理环境。以前我的做法是使用 Hexo 自带的

<div id="thm-name">
{% blockquote %}
...
{% endblockquote %}
</div>

手动加上编号。需要在其它地方引用的时候就用 定理 1 这样的写法。但是显然这个方案很丑,因为一旦定理顺序作了修改,所有的编号都要重新调整,非常麻烦。

现在我找到了一个解决方案分享给大家。

效果地址: https://pywonderland.com/weyl-character-formula/

你需要做这么几件事情:

  1. 使用 hexo-renderer-pandoc 插件作为 md 的解释器. npm install hexo-renderer-pandoc --save 然后删掉 hexo 自带的 hexo-renderer-marked 。
  2. 这个 repo clone 到你的本地,放在 blog 目录,和 _config.yml 都处于顶层即可。
  3. 同样在你的 blog 目录下,和 _confi.yml 处于同一级,新建一个 metadata.yml,里面写上
---
link-citations: true
reference-section-title: "References"
bibliography: "./ref.bib"
statement:
  supply-header: no
  count-within: section
  crossref-prefixes: true
statement-kinds:
  theorem:
    label: 定理
  lemma:
    label: 引理
    counter: theorem
  proposition:
    label: 命题
    counter: theorem
  corollary:
    label: 推论
    counter: theorem
  definition:
    label: 定义
    counter: theorem
  proof:
    label: 证明
    counter: none
    style: definition
  note:
    label: 注记
    counter: none
    style: definition
  example:
    label: 例
    counter: none
    style: definition
  question:
    label: 问题
    counter: theorem
    style: definition
statement-styles:
  definition:
    punctuation: ':'
  note:
    punctuation: ':'
---

上面这个是控制各种定理、引理等环境的显示方式,比如定理环境会统一显示为 **定理 1.**,而例子会统一显示为 例: 这样。此外里面还指定了定理编号是跟着 section 走,以及参考文献的路径。

然后, 在你的 _config.yml 里面加上

pandoc:
  args:
    - '--to'
    - 'html5'
    - '-L'
    - './statement/statement.lua'
    - '--citeproc'
    - '--metadata-file'
    - './metadata.yml'
    - '--mathjax'

这一步是为了把编译参数传给 pandoc 。最后,还要在你的博客的 css/styl 文件里面加上:


.statement.plain
    background-color: rgba(0, 0, 0, 0.03);
    padding: 0.6rem 0.6rem 0.6rem 1.2rem;
    border-left: 5px solid #ee6e73;
    margin: 1.5em 0em;
    color: black;

.statement.example.definition
    background-color: rgba(0, 0, 0, 0.03);
    padding: 0.6rem 0.6rem 0.6rem 1.2rem;
    border-left: 5px solid #0565f6;
    margin: 1.5em 0em;
    color: black;

.statement.definition
    background-color: rgba(0, 0, 0, 0.03);
    padding: 0.6rem 0.6rem 0.6rem 1.2rem;
    border-left: 5px solid #0565f6;
    margin: 1.5em 0em;
    color: black;

.statement.plain .statement-label
    font-weight: bold;

.statement.note.unnumbered
    margin: 2em auto;
    padding: 0.5em;
    font-size: 85%;
    width: 90%;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    border-radius: 5px;
    padding: 10px;

.statement.definition .statement-label
    font-weight: bold;


.statement.proof .statement-label
    font-weight: bold;

.statement.plain p:first-child
     text-indent: 0pt;


.statement.plain .statement-info
    font-style: normal;
    font-weight: normal;
    font-variant: normal;

.csl-entry
    border-left: 1px solid #777;
    padding-left: 1em;
    margin-left: 2em;
    margin-top: 1.5em;
    margin-bottom: 1.5em;
    margin-right: 0em;
    font-size: 100%;
    line-height: 150%;

以控制定理环境和参考文献的样式。

然后 hexo clean && hexo g 你的博客现在就可以支持自动编号的定理环境了。

在正文中,你可以这样写:

::: {.theorem #thm-name}
xxxxx
:::

引用的时候使用 @Pre:thm-name ,所有的编号就都自动可以完成了。如果你不想给某个定理编号,那么可以加上 .unnumbered 标签:

::: {.theorem .unnumbered #thm-name}
xxxxx
:::

如果你想用 bibtex 管理参考文献的话,可以把参考文献放在 blog 目录下的比如 ref.bib 文件中 (见上面 metadata.yml 中 reference 路径的设置)。然后在正文中引用方式为 [@Einstein section 1.1] 这样。

希望上面的方法对大家有用。

1457 次点击
所在节点    分享创造
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1036101

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX