3

Is it possible to add an anchor (link target, to which I can refer with :ref:) at an arbitrary position in the text? I have a situation like this:

Some text

.. class:: mylist

**Term 1**
  Definition

*Term 2*
  Definition

:customrole:`Term 3` extra
  Definition

And I'd like to add a link to, say Term 2. I cannot simply add .. _target: before the block, because that breaks the list in two, and the second part does not get the mylist class (is this intentional or could it be a bug?). I could write this:

*Term 2*
  .. _target:

  Definition

but then the target is the definition paragraph, not the Term 2 element itself.

Applying an inline role to Term 2 is not simple either, because then there's the problem of nested formatting (I've used different styles for each terms to show that there could be a variety of situations I'd need this for). Maybe if I could apply an inline role to some empty text...

Any suggestion?

Jellby
  • 2,360
  • 3
  • 27
  • 56
  • Possible duplicate of [Python Sphinx anchor on arbitrary line](https://stackoverflow.com/questions/30822880/python-sphinx-anchor-on-arbitrary-line) – pradyunsg Apr 17 '18 at 17:43

2 Answers2

1

I just answered a similar question here, hopefully this solves your problem:

Python Sphinx anchor on arbitrary line

In summary, you can use the ref role: http://sphinx-doc.org/markup/inline.html#role-ref

Specifically read the 2nd bullet point which addresses what happens if the reference is not placed before a title.

Community
  • 1
  • 1
Subbdue
  • 825
  • 1
  • 7
  • 12
  • 1
    My problem is not writing the reference, but placing the anchor, which either breaks the list or puts the anchor in the description, not the item itself. The `:term:` solution only works for glossaries, apparently (or how would I use it in my example?) – Jellby Jun 23 '15 at 11:46
1

The only solution I know is to place your definition list into a glossary directive and refer its terms with the term role ...

Some text

.. glossary::

  Term 1
    Definition

  Term 2
    Definition

A link to :term:`Term 1` or :term:`Term 2`
return42
  • 543
  • 3
  • 10