我正在按照 numpy 文档字符串指南编写文档字符串。然后我使用 sphinx 的 autodoc 生成我的文档。在一些文档字符串中,我使用 LaTeX 公式(sphinx.ext.mathjax)。似乎 \r
意味着一些特殊的东西,比如换行。如果我有以下命令:
"""
This :math:`\langle a, b\rangle` denotes the dot product of a and b
"""
它无法正确渲染。它将类似角度放入新行并给出错误:内联解释文本或短语引用起始字符串,没有结束字符串 如果我用\langle 替换\rangle 一切正常。我该如何解决这个问题?
最佳答案
解决方案是使用双斜杠转义或在三引号中放置一个“r”(不带引号),这会禁用对引号内斜杠的解释:
r"""
This :math:`\langle a, b\rangle` denotes the dot product of a and b
"""
有几个前缀会影响字符串文字的定义,请参阅 documentation of Python .
相关摘录:
The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
和
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially.
关于python -\r 在 sphinx 中代表什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543194/