numref for container

58 views
Skip to first unread message

Myne

unread,
Jan 31, 2020, 11:36:49 PM1/31/20
to sphinx-users
Hi,

I want to make a numref to a container.


.. container::
   
:name: mycontainer


     spam eggs




Bar
-----


Let's reference :numref:`mycontainer` .


I want it to appear as "Let's reference Cont. 1" . In my conf.py I put 


numfig_format
= {'container': 'Cont. %s'}



But that doesn't seem to work, I get 

WARNING: undefined label: mycontainer

I am wondering how to make numref work on containers and other types.

Komiya Takeshi

unread,
Feb 1, 2020, 6:22:52 AM2/1/20
to sphinx...@googlegroups.com
Hi,

"numref" only does referring by number as it named. To refer something
by number, you need to assign numbers to each object before
referencing. But Sphinx does not have a feature to assign numbers to
containers. So you need to write some extensions to do that.

Personally, I can't imagine how containers get numbered. How is it displayed?

Thanks,
Takeshi KOMIYA

2020年2月1日(土) 13:36 Myne <myne...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/f6167e37-ef86-416a-9355-7138bfa82345%40googlegroups.com.

Myne

unread,
Feb 1, 2020, 6:37:40 PM2/1/20
to sphinx-users
Hi,

I tried to make an extension which will number and reference the containers but did not succeed.

How can I fix it?

Thanks.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx...@googlegroups.com.

Komiya Takeshi

unread,
Feb 2, 2020, 8:46:50 AM2/2/20
to sphinx...@googlegroups.com
Sounds good. I just saw your extension and I found some problem.

1. It assigned the value of `name` to `container_node.names`.
Correctly, it should be stored into `container_node['names']`.
2. To enable the name of hyperlink target, you need to call
`self.state.document.note_implicit_target(container_node)`.
3. At present, Sphinx does not support assigning numbers to container
node. So you should inherit another node (ex. Element) instead.

With I rewrite your extension by above rules, I got a number of
container_node via `:numref:` role.
Please try it.

Thanks,
Takeshi KOMIYA

2020年2月2日(日) 8:37 Myne <myne...@gmail.com>:
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/7dfe936b-8b9c-4b73-8fc4-88229d1ed703%40googlegroups.com.

Myne

unread,
Feb 6, 2020, 12:16:08 AM2/6/20
to sphinx-users
Hi Takeshi KOMIYA,

I tried those steps here 


but I get an error


Traceback (most recent call last):
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/cmd/build.py", line 276, in build_main
    app
.build(args.force_all, filenames)
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/application.py", line 353, in build
   
self.builder.build_update()
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 299, in build_update
    len
(to_build))
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 311, in build
    updated_docnames
= set(self.read())
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 418, in read
   
self._read_serial(docnames)
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 439, in _read_serial
   
self.read_doc(docname)
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 479, in read_doc
    doctree
= read_doc(self.app, self.env, self.env.doc2path(docname))
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/io.py", line 350, in read_doc
    pub
.publish()
 
File "/home/user/Documents/docutils/docutils/docutils/core.py", line 219, in publish
   
self.apply_transforms()
 
File "/home/user/Documents/docutils/docutils/docutils/core.py", line 200, in apply_transforms
   
self.document.transformer.apply_transforms()
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/transforms/__init__.py", line 86, in apply_transforms
   
super().apply_transforms()
 
File "/home/user/Documents/docutils/docutils/docutils/transforms/__init__.py", line 171, in apply_transforms
    transform
.apply(**kwargs)
 
File "/home/user/.cache/pypoetry/virtualenvs/sphinx-demo-4JCgFyEV-py3.7/lib/python3.7/site-packages/sphinx/transforms/__init__.py", line 177, in apply
   
self.document.note_implicit_target(node)
 
File "/home/user/Documents/docutils/docutils/docutils/nodes.py", line 1489, in note_implicit_target
   
self.set_name_id_map(target, id, msgnode, explicit=None)
 
File "/home/user/Documents/docutils/docutils/docutils/nodes.py", line 1436, in set_name_id_map
   
self.set_duplicate_name_id(node, id, name, msgnode, explicit)
 
File "/home/user/Documents/docutils/docutils/docutils/nodes.py", line 1475, in set_duplicate_name_id
    dupname
(node, name)
 
File "/home/user/Documents/docutils/docutils/docutils/nodes.py", line 2290, in dupname
    node
['names'].remove(name)
ValueError: list.remove(x): x not in list


Did I miss something?

Komiya Takeshi

unread,
Feb 6, 2020, 10:48:52 AM2/6/20
to sphinx...@googlegroups.com
Sorry, my last comment was incorrect.

>2. To enable the name of hyperlink target, you need to call
>`self.state.document.note_implicit_target(container_node)`.

Please use `note_explicit_target()` instead.
You can see the difference between implicit and explicit targets here:
https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#implicit-hyperlink-targets

In addition, please use `lambda node: node[0].astext(),` for
add_enumerable_node() instead.
With current Sphinx, node['names'] was removed on transformation of
doctree. (This would be fixed in future release)

Thanks,
Takeshi KOMIYA

2020年2月6日(木) 14:16 Myne <myne...@gmail.com>:
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/e90bf62d-420e-44ad-90e4-65652add714d%40googlegroups.com.

Myne

unread,
Feb 7, 2020, 12:09:06 AM2/7/20
to sphinx-users
That works for me. Thank you for your help.


Reply all
Reply to author
Forward
Message has been deleted
0 new messages