MyST-Parser icon indicating copy to clipboard operation
MyST-Parser copied to clipboard

Make target reference work for arbitrary locations

Open arwedus opened this issue 2 years ago • 6 comments

Description / Summary

In plain reStructuredText, labels can be placed in arbitrary locations and be referenced by the :ref: role.

In MyST markdown, I get a warning for figure_labelin the following example, but heading_label works:

see {ref}`figure_label`.

(heading_label)=
## A section with a lot of text and a figure

A lot of text.

(figure_label)=
![My figure title](my_fig.svg)

The warning message is WARNING: Failed to create a cross reference. A title or caption not found: figure_label.

Value / benefit

Especially references to figures and tables would be useful for technical writing.

Implementation details

This limitation seems kinda odd, especially since the corresponding reStructuredText syntax and reference role does not have the limitation. Maybe it is easy to remove the warning and add the functionality?

Tasks to complete

No response

arwedus avatar Sep 20 '21 14:09 arwedus

So I imagine what is happening here (not yet tested), is that a target is "block level" syntax wheras an image is an "inline level" syntax. So your text:

(heading_label)=
## A section with a lot of text and a figure

A lot of text.

(figure_label)=
![My figure title](my_fig.svg)

is parsed to something that

<target heading_label>
<heading>
<paragraph>
<target figure_label>
<paragraph>
   <image>

So you can see that the figure_label target will be applied to the paragraph not the image.

It should maybe work like RST, if you were to use a "block level" directive e.g.

(figure_label)=
```{image} my_fig.svg
:alt: My figure title
```

which would go to just:

<target figure_label>
<image>

chrisjsewell avatar Sep 20 '21 16:09 chrisjsewell

Hi @chrisjsewell , I tried your suggestion with a document that looks somewhat like this:

-   **Alternative 1:** The idea of using ...
     is depicted in figures {ref}`fig_a` and {ref}`fig_b`.

    (fig_a)=
    ```{image} images/a.svg
    :alt: A
    :width: 600px
    :align: center
    ```

    (fig_b)=
    ```{image} images/b.svg
    :alt: B
    :width: 600px
    :align: center
   ```

-  **Alternative  2:** Here the list continues.

It results in the same warning and no reference being created.

arwedus avatar Sep 21 '21 07:09 arwedus

But what about just normally, not within a list?

chrisjsewell avatar Sep 21 '21 07:09 chrisjsewell

That doesn't make a difference, I still get the warning and the link does not work.

arwedus avatar Sep 23 '21 10:09 arwedus

@chrisjsewell do you plan a fix/change for this issue sometime?

arwedus avatar Nov 29 '21 09:11 arwedus

The warning message is WARNING: Failed to create a cross reference. A title or caption not found: figure_label.

That warning is issued by Sphinx and would occur just the same in reST with a :ref:`figure_label` referring to an image directive, as the latter never defines a title/caption. (An "alt" title is not an actual title.) The reference then requires a link text, for example {ref}`that figure <figure_label>` in Markdown. Or you could refer to a figure directive that contains a caption.

john-hen avatar Dec 15 '21 12:12 john-hen