sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.48k stars 2.11k forks source link

label system of math directive is confusable #5010

Open tk0miya opened 6 years ago

tk0miya commented 6 years ago

Problem

In reST world, usually, we have two ways to give a label to an arbitrary node which is generated by directive. The first way is hyperlink target. And another is :name: option. For example, we can give a label to figures like following:

.. _figure1:
.. figure:: sphinx-logo.png
.. figure:: sphinx-logo.png
   :name: figure2

Both are working fine. Off course, we can refer it using :ref: role.

But math directive is not. It also supports :name: option (it is an alias of :label: option). But :ref: role can't refer it.

In addition, the behavior of equation number is not same between :name: option and hyperlink targets. With the option, the math_block has equation number automatically. On the other hand, putting hyperlink targets before math directive, the math_block does not have equation number.

Procedure to reproduce the problem

.. math:: E = mc^2
   :name: equation1
.. _equation2:
.. math:: E = mc^2

Error logs / results

With :name: option, it is rendered as following:

\begin{equation}\label{equation:index:equation1}
\begin{split}E = mc^2\end{split}
\end{equation}

With a hyperlink target, it is rendered:

\phantomsection\label{\detokenize{index:equation2}}\begin{equation*}
\begin{split}E = mc^2\end{split}
\end{equation*}

Expected results

Both case are converted to same result.

Environment info

jfbu commented 6 years ago

The math directive may contain multiple equations. It would be nice to have a :labels: which would take comma separated arguments to assign labels to each one. But then it does not play well with the hyperlink target method. This is not only LaTeX issue, also HTML output.

tk0miya commented 6 years ago

Note: In HTML output, they are rendered as following:

<div class="math notranslate nohighlight" id="equation-equation1">
<span class="eqno">(1)<a class="headerlink" href="#equation-equation1" title="Permalink to this equation">¶</a></span>\[E = mc^2\]</div>
<div class="math notranslate nohighlight" id="equation2">
\[E = mc^2\]</div>

In HTML case, :ref: role can't refer equation having :name: option also.

tk0miya commented 6 years ago

About :labels: idea, I feel it is pretty good. But it might be hard in HTML output. I think LaTeX output is not so difficult. If you really want it, please file a new issue :-)

chrisjsewell commented 2 years ago

Just to note here, using the :name: option for math, then refering to it with :ref: is actually broken for output formats like latex, since the visitor does not use it. For example:

.. _mathtarget1:
.. math:: E = mc^2

.. math:: E = mc^2
   :name: mathtarget2

.. math:: E = mc^2
   :label: mathtarget3

:ref:`text <mathtarget1>` :ref:`text <mathtarget2>` :ref:`text <mathtarget3>`

will build with one warning:

WARNING: undefined label: mathtarget3

but the latex it produces will actually fail on PDF build, because the mathtarget2 reference does not have the equation: prefix:

\phantomsection\label{\detokenize{syntax/syntax:mathtarget1}}
\begin{equation*}
\begin{split}a=1\end{split}
\end{equation*}
\begin{equation}\label{equation:syntax/syntax:mathtarget2}
\begin{split}b=2\end{split}
\end{equation}
\begin{equation}\label{equation:syntax/syntax:mathtarget3}
\begin{split}b=2\end{split}
\end{equation}
\sphinxAtStartPar
{\hyperref[\detokenize{syntax/syntax:mathtarget1}]{\sphinxcrossref{\DUrole{std,std-ref}{text}}}} 
{\hyperref[\detokenize{syntax/syntax:mathtarget2}]{\sphinxcrossref{\DUrole{std,std-ref}{text}}}}  
\DUrole{xref,std,std-ref}{text}