Referencing figures with numbers in Sphinx and reStructuredText

前端 未结 5 1311
挽巷
挽巷 2020-12-14 06:43

When writing RST that will be processed with Sphinx, I can\'t get Sphinx LaTeX output to use figure numbers when referencing figures. For instance, this code:



        
5条回答
  •  攒了一身酷
    2020-12-14 06:57

    One can use raw latex code, inline. For the example above, a role for raw latex code is first defined and than used to refer to the figure with the \ref{} latex command, and to set a label to the figure with the \label{} latex command.

    The following should work:

    .. role:: raw-latex(raw)
         :format: latex
    
    The lemmings are attacking, as can be seen in :ref:`figlem`
    on figure :raw-latex:`\ref{pic:lem}`.
    
    .. _figlem:
    
    .. figure:: _static/lemming_invasion.* 
    
       They're coming! :raw-latex:`\label{pic:lem}`
    

    Note that the \label{} command will appear inside the caption in the tex file, but it is still acceptable, at least by pdflatex. Also note that there should be at least one space before :raw-latex.

提交回复
热议问题