inlinepython-sphinxrestructuredtextrole

How can I use the only directive inline by using role in python-sphinx?


In python-sphinx there is the only directive, which can be used to conditionally influence the document according to its output. For instance text appears in html or latex only.

It is use like this:

.. only:: not latex

   Here there is some Text, that does not appear in latex output.

.. only:: html

   Here there is some Text, that only appears in html output.

How can I use the role directive in the right way to use the only-class inline, let's say like this:

Here there is some Text, that :only-notlatex:`does not appear in latex`
:only-html:`only appears in html`.

I saw something similar for the raw directive. Is this also possible for the only directive? I tried:


.. role:: only-html(only)
   :format: html

.. role:: only-notlatex(only)
   :format: not latex 

But this does not work.


Solution

  • Directives work on blocks of text (whole paragraphs); roles are for inline text (within paragraphs).

    You mentioned raw, and there is indeed both a directive and a role with that name, for "raw data pass-through".

    But there is no built-in role that is the inline equivalent of the only directive. You will have to create your own custom role for this purpose. I can't provide any detailed instructions, but here is an article that can help you get started: http://doughellmann.com/2010/05/09/defining-custom-roles-in-sphinx.html.