2

I thought to cross reference I have to enter the starting format as

:ref:`Link Text <CrossReferenceLabel>`

Then use it later on by writing

.. _CrossReferenceLabel:

Like the following.

   +-----------+-----------------------------------+-------+
   | Number    | Animal                            |       |
   +===========+===================================+=======+
   | 1         | :ref:`Dog <DogLabel>`             |       |
   +-----------+-----------------------------------+-------+
   | 2         | Cat                               |       |
   +-----------+-----------------------------------+-------+


   +-----------+-----------------------------------+-------+
   | Dog       | Services                          |       |
   |           | .. _DogLabel:                     |       |
   +===========+===================================+=======+
   | 1         | Vet                               |       |
   +-----------+-----------------------------------+-------+
   | 2         | Toy Shop                          |       |
   +-----------+-----------------------------------+-------+

The link is created for the Link Text, however the destination doesn't seem to have the cross reference.

Iancovici
  • 5,574
  • 7
  • 39
  • 57
  • See [this answer](https://stackoverflow.com/a/54759554) and [this answer](https://stackoverflow.com/a/54813604). I think you need to add some blank lines around the reference target itself (I'm not entirely sure right now). – bad_coder May 22 '21 at 15:54

1 Answers1

4

You are so close. I would place the label above the Dog table because that is what it labels.

+-----------+-----------------------------------+-------+
| Number    | Animal                            |       |
+===========+===================================+=======+
| 1         | :ref:`Dog <DogLabel>`             |       |
+-----------+-----------------------------------+-------+
| 2         | Cat                               |       |
+-----------+-----------------------------------+-------+

.. _DogLabel:

+-----------+-----------------------------------+-------+
| Dog       | Services                          |       |
+===========+===================================+=======+
| 1         | Vet                               |       |
+-----------+-----------------------------------+-------+
| 2         | Toy Shop                          |       |
+-----------+-----------------------------------+-------+

The third column is not necessary in this example, so you could remove it.

Also grid style tables are the most difficult syntax to edit out of the four different table styles. See Tables documentation for alternatives.

Additionally if you want to reference something within a table cell, you must follow reStructuredText rules about whitespace after a label. For example.

+-----------+-----------------------------------+-------+
| Number    | Animal                            |       |
+===========+===================================+=======+
| 1         | :ref:`Dog <DogLabel>`             |       |
+-----------+-----------------------------------+-------+
| 2         | Cat                               |       |
+-----------+-----------------------------------+-------+

+-----------+-----------------------------------+-------+
| Dog       | .. _DogLabel:                     |       |
|           |                                   |       |
|           | Services                          |       |
+===========+===================================+=======+
| 1         | Vet                               |       |
+-----------+-----------------------------------+-------+
| 2         | Toy Shop                          |       |
+-----------+-----------------------------------+-------+

To me it makes more sense to label the table than a cell in the table.

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57