2

I'm writing my docstring following the numpy docstring guidelines. Then I use sphinx's autodoc to generate my documentation. Within some docstrings I'm using LaTeX formulas (sphinx.ext.mathjax). It seems that \r means something special like a new line. If I have the following command:

"""
This :math:`\langle a, b\rangle` denotes the dot product of a and b
"""

it doesn't properly render. It puts like angle to a new line and gives me an error: Inline interpreted text or phrase reference start-string without end-string If I replace the \rangle with \langle everything works fine. How can I fix this?

mzjn
  • 48,958
  • 13
  • 128
  • 248
math
  • 1,868
  • 4
  • 26
  • 60
  • 1
    \r is legal SphinxQL space now http://sphinxsearch.com/bugs/view.php?id=332 – planet260 Oct 03 '17 at 11:12
  • Not just in Sphinx - `\r` is the carriage return character in a lot of places. – jonrsharpe Oct 03 '17 at 11:12
  • @planet260 thx for the comment. But how can I then make this work:) – math Oct 03 '17 at 11:15
  • 3
    Escape with a double slash or place a "r" (without the quotes) in from of the triple quotes, this disables the interpretation of the slashes inside the quotes: `r"""" text \langle """` – Pierre de Buyl Oct 03 '17 at 11:21
  • @planet260: The question is about the Sphinx documentation tool, which is unrelated to the Sphinx search engine. – mzjn Oct 03 '17 at 11:42
  • @PierredeBuyl thanks alot! that works like a charm! If you want to add is an answer I will accept it :) – math Oct 03 '17 at 12:55

1 Answers1

5

The solution is to escape with a double slash or place a "r" (without the quotes) in from of the triple quotes, this disables the interpretation of the slashes inside the quotes:

r"""
This :math:`\langle a, b\rangle` denotes the dot product of a and b
"""

There are several prefixes that influence the definition of string literals, see the documentation of Python.

Relevant excerpts:

The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

and

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.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22