You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandoc Markdown中为所有引用前添加非断行空格?

问题场景与示例

我们来看以下示例:

  • test.md:
---
bibliography: test.bib
---

This is just a test text to demonstrate that in some situations, unfortunately, citations will appear at the beginning of a line, which is often considered unaesthetic [@dominici2014overview].
  • test.bib:
@article{dominici2014overview,
    title={An overview of Pandoc},
    author={Dominici, Massimiliano},
    journal={TUGboat},
    volume={35},
    number={1},
    pages={44--50},
    year={2014}
}
  • acm.csl: 使用ACM格式的CSL样式

构建命令:

pandoc test.md --citeproc --csl=acm.csl --bibliography=test.bib --pdf-engine=pdflatex -o test.pdf

生成的PDF中,引用标记[1]会单独占一行(文本末尾换行后,[1]独自在新行)。

需求

如何自动确保引用标记[1]前带有非断行空格,避免其单独成行?手动在Markdown中用\ 替换普通空格会让文件杂乱,因此希望通过配置项或Pandoc过滤器实现自动处理。

尝试过的方案

我写了一个简单的Python过滤器,但非断行空格似乎被忽略,还需要处理句首等特殊情况,因此更倾向于现有成熟解决方案:

#!/usr/bin/env python

import pandocfilters as pf

def add_nonbreaking_space(key, value, format, meta):
    if key == 'Cite':
        value[1] = [{**text, 'c': f"\N{NO-BREAK SPACE}{text['c']}"} for text in value[1]]
        return pf.Cite(*value)
    return None

if __name__ == '__main__':
    pf.toJSONFilter(add_nonbreaking_space)

内容的提问来源于stack exchange,提问作者Christoph Thiede

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:47:53