
我有一个数学相关的博客,里面的文章经常要用到定理环境。以前我的做法是使用 Hexo 自带的
<div id="thm-name"> {% blockquote %} ... {% endblockquote %} </div> 手动加上编号。需要在其它地方引用的时候就用 定理 1 这样的写法。但是显然这个方案很丑,因为一旦定理顺序作了修改,所有的编号都要重新调整,非常麻烦。
现在我找到了一个解决方案分享给大家。
效果地址: https://pywonderland.com/weyl-character-formula/
你需要做这么几件事情:
npm install hexo-renderer-pandoc --save 然后删掉 hexo 自带的 hexo-renderer-marked 。_config.yml 都处于顶层即可。_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] 这样。
希望上面的方法对大家有用。