如何在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
相关产品推荐
相关产品推荐

