36

In Sphinx, if I have the following heading declaration:

.. _somestuff:

``this is code``, this is not!
==============================

It renders, like this:

    this is code, this is not!

Which is good, but, if I use the reference, e.g:

Have a look at :ref:`somestuff`

It loses the code formatting and renders like:

    Have a look at this is code, this is not!

instead of:

    Have a look at this is code, this is not!


Is it possible to retain the code formatting in the reference? And how would I go about it?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
tjm
  • 7,500
  • 2
  • 32
  • 53
  • 5
    I don't think this is possible right now. I'm pretty sure the `:ref:` role just pulls the link, and ignores any other rst formatting. You might try reporting this to the Sphinx developers as a bug/feature request. – Kevin Horn Dec 07 '12 at 15:43
  • Here is a workaround: https://stackoverflow.com/a/51666008/407651 – mzjn Aug 03 '18 at 08:18

1 Answers1

2

If you have a look at :ref: documentation in its official web site about inline markups:

:ref:

To support cross-referencing to arbitrary locations in any document, the standard reST labels are used. For this to work label names must be unique throughout the entire documentation ...

I think (as @Kevin Horn) it's no possible right now, because it's only used to create links (without rst formatting) with other sections in your project. Then if you write something like this:

.. _somestuff:

``this is code``, this is not!
==============================

.. _another_somestuff:

this is another code!
========================

If I link with these sections:

Have a look at :ref:`somestuff`
Have a look at :ref:`another_somestuff`
Have a look at :ref:`this link <somestuff>`

The result is:


Have a look at this is code, this is not!

Have a look at this is another code!

Have a look at this link


The style is the same in all of them .

Note: the italic/bold words symbolize links

fenix688
  • 2,435
  • 2
  • 17
  • 23