Search code examples
pythonpython-sphinxsubstitutioncross-reference

Substitute a reference to a method


I'd like to make a table with some methods like so.

+-----------+------+-----+------+
| method    | prop | ref | note |
+-----------+------+-----+------+
| :meth:`m` | x    |     | sad  |
+-----------+------+-----+------+
| :func:`f` | x    | x   |      |
+-----------+------+-----+------+

With my real names however, the table grows quite long, particularly as I want to shorten fully qualified names with :meth:'name <fully.qualified.name>. I'd like to shorten the table definition by using substitutions, but I haven't found a way of making it work. Note I'm replacing backticks to avoid ending the code block early on the post. I do have them in my code. Here are some attempts.

  • .. |m| :meth:'m' using |m|: Undefined substitution 'm' referenced
  • .. _m: :meth:'m' using |m|_: Undefined substitution 'm' referenced
  • .. |m| replace:: :meth:'m' using |m|: Correct text produced, but no link

How can I achieve shortening the reference? Or is it not possible?


Solution

  • Using replace:: is indeed the answer. However, in my case Sphinx cached some information that prevented the link from being produced. After deleting the whole build folder, the links work just fine.

    .. |m| replace:: :meth:`m`
    
    This is |m|.