Pandoc:如何才能不转义 YAML 元数据中的反斜杠?

发布于 2025-01-10 22:26:29 字数 1093 浏览 3 评论 0原文

Pandoc 可以在 Markdown 中使用 YAML 标头,或者对所有源类型使用默认值或元数据文件,其中可以存储要在模板中使用的变量。是否可以在这些元数据变量中不转义反斜杠和 LaTeX 命令?

例如,我的 YAML 标头中包含以下行:phone: +49\,123\,456789。转换时,Pandoc 会对反斜杠进行转义,因此变为 +49\textbackslash,123\textbackslash,456789。这是不需要的,因为我希望 LaTeX 解释 \, 并将其转换为薄空格。

我还尝试将其包装在 '' 中或用 | 作为块,因此 YAML 本身不会转义任何内容,但这似乎是 Pandoc 本身在进行转换,而不是YAML 解释器。

最小可重现示例

src.md:

Some text

defaults.yaml:

template: ./tpl.tex
metadata:
  phone: +49\,123\,456789

tpl.tex:

Phone number: $phone$

Markdown body:
$body$

使用 pandoc --defaults defaults.yaml src.md --to Latex 输出:

Phone number: +49\textbackslash,123\textbackslash,456789

Markdown body:
Some text

应该是:

Phone number: +49\,123\,456789

Markdown body:
Some text

Pandoc can use a YAML header in Markdown or a defaults or metadata file for all source types where one can store variables to use in the template. Is it possible to not escape backslashes and LaTeX commands inside these metadata variables?

For example, I'd have this line in my YAML header: phone: +49\,123\,456789. When converting, Pandoc escapes the backslashes, so it becomes +49\textbackslash,123\textbackslash,456789. This is unwanted, as I want LaTeX to interpret \, and turn it into thin spaces.

I also tried wrapping it in '' or as block with |, so YAML itself doesn't escape anything, but this seems to be Pandoc itself doing the conversion, not the YAML interpreter.

Minimum reproducible example

src.md:

Some text

defaults.yaml:

template: ./tpl.tex
metadata:
  phone: +49\,123\,456789

tpl.tex:

Phone number: $phone$

Markdown body:
$body$

Output with pandoc --defaults defaults.yaml src.md --to latex:

Phone number: +49\textbackslash,123\textbackslash,456789

Markdown body:
Some text

Should be:

Phone number: +49\,123\,456789

Markdown body:
Some text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2025-01-17 22:26:29

元数据总是会被转义;最简单的方法是做你想做的事,定义一个变量而不是元数据。

---
variables:
  phone: +49\,123\,456789
---

与元数据值相反,变量被逐字插入到模板中。

Metadata will always be escaped; the easiest way is to do what you want is to define a variable instead of metadata.

---
variables:
  phone: +49\,123\,456789
---

Contrary to metadata values, variables are inserted verbatim into the template.

心病无药医 2025-01-17 22:26:29

您可以使用以下 unicode 字符: https://www.compart.com/en/unicode /U+202F

然后

template: ./tpl.tex
metadata:
  phone: +49 123 456789

通过 pandoc --defaults defaults.yaml src.md --to Latex -o test.tex 翻译为以下 .tex文件:

Phone number: +49\,123\,456789

Markdown body:
Some text

You can use this unicode character: https://www.compart.com/en/unicode/U+202F

Then

template: ./tpl.tex
metadata:
  phone: +49 123 456789

gets translated via pandoc --defaults defaults.yaml src.md --to latex -o test.tex to the following .tex file:

Phone number: +49\,123\,456789

Markdown body:
Some text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文