【问题标题】:How do you change the style of pybtex references in sphinx如何更改 sphinx 中 pybtex 引用的样式
【发布时间】:2019-05-08 05:39:38
【问题描述】:

我正在尝试使用sphinxcontrib.bibtex 在某些文档中包含引用/参考。它说你可以set the style 使用:

.. bibliography: refs.bib
   :style: plain

它带有以下样式:alphaplain,它们创建的参考标签如下所示:

  • alpha: [ZieglerBenderSchreiber+14][TMT14][PCY+16a]
  • plain: [1], [2], [3]

我发现alpha 不一致,但我认为plain 的数字缺乏上下文。要自定义格式,它提供了以下示例:

from pybtex.style.formatting.unsrt import Style as UnsrtStyle
from pybtex.style.template import toplevel # ... and anything else needed
from pybtex.plugin import register_plugin

class MyStyle(UnsrtStyle):

    def format_XXX(self, e):
        template = toplevel [
            # etc.
        ]
        return template.format_data(e)

register_plugin('pybtex.style.formatting', 'mystyle', MyStyle)

sphinx 失败了:

sphinx-build -M html . out
Running Sphinx v1.8.3

Configuration error:
There is a syntax error in your configuration file: bad input (conf.py, line 92)
Did you change the syntax from 2.x to 3.x?

它似乎在后台使用pybtex,因此请遵循该文档中的示例:

from pybtex.style.formatting import BaseStyle
from pybtex.richtext import Text, Tag

class MyStyle(BaseStyle):
    def format_article(self, entry):
        return Text('Article ', Tag('em', entry.fields['title']))

from pybtex.plugin import register_plugin
register_plugin('pybtex.style.formatting', 'mystyle', MyStyle)

狮身人面像失败了:

Exception occurred:
  File "/home/josh/codes/docs/conf.py", line 88, in format_article
    return Text('Article ', Tag('em', entry.fields['title']))
AttributeError: 'dict' object has no attribute 'fields'
The full traceback has been saved in /tmp/sphinx-err-dftwlzvr.log, if you want to report the issue to the developers.

...我认为这只是自定义参考书目条目文本而不是标签文本。如何自定义标签文本?我更喜欢以下之一:

  1. 只是括号数字 ([1]),如 plain,但带有上标标签
  2. 类似于[ref.12] 类似于eq.12fig.12
  3. 类似于alpha,但字符数一致

【问题讨论】:

    标签: python python-sphinx restructuredtext pybtex


    【解决方案1】:

    我不想回答我自己的问题,但这就是我的想法。我在conf.py 中添加了以下内容,它使用 bibtex 键作为标签文本:

    from pybtex.style.formatting.unsrt import Style as UnsrtStyle
    from pybtex.style.labels.alpha import LabelStyle as AlphaLabelStyle
    
    class KeyLabelStyle(AlphaLabelStyle):
      def format_label(self, entry):
        label = entry.key
        return label
    
    class CustomStyle(UnsrtStyle):
      default_sorting_style = 'author_year_title'
    
      def __init__(self, *args, **kwargs):
        super(CustomStyle, self).__init__(*args, **kwargs)
        self.label_style = KeyLabelStyle()
        self.format_labels = self.label_style.format_labels
    
    from pybtex.plugin import register_plugin
    register_plugin('pybtex.style.formatting', 'custom', CustomStyle)
    

    在 RST 中激活:

    .. bibliography:: refs.bib
       :style: custom
    

    这不是一个完整的答案,但是因为它并没有真正生成自定义标签……而是覆盖了现有样式的一些内部结构。如此糟糕的几乎答案......我希望它可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多