jupyter-book icon indicating copy to clipboard operation
jupyter-book copied to clipboard

Enable line numbers to show on code cells.

Open jwayne2978 opened this issue 4 years ago • 6 comments

Is your feature request related to a problem? Please describe.

No

Describe the solution you'd like

When code cells are rendered, there should be a way to show line numbers. This could be done with tags. Narrating code requires precision with references to line numbers.

Describe alternatives you've considered

None.

Additional context

jwayne2978 avatar Mar 05 '21 07:03 jwayne2978

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

welcome[bot] avatar Mar 05 '21 07:03 welcome[bot]

Inspired by https://github.com/readthedocs/sphinx_rtd_theme/issues/1168 this worked for me in both HTML and pdflatex output:

```{code-block} python
---
name: code_linenumbers
linenos: True
caption: |
    This code has line numbers.
---
print("Hello")
print("world.")
```

Duplicate issue #1580

michaelosthege avatar Apr 25 '22 10:04 michaelosthege

This was added in https://github.com/executablebooks/MyST-NB/pull/380, available as nb_number_source_lines globally, and also per notebook or per code cell, see also https://myst-nb.readthedocs.io/en/latest/use/formatting_outputs.html#images-and-figures

chrisjsewell avatar Apr 25 '22 11:04 chrisjsewell

@chrisjsewell thanks for that context. Can you post/link an example of where the nb_number_source_lines setting should be set?

michaelosthege avatar Apr 25 '22 11:04 michaelosthege

heya, its not released to jupyter-book just yet (only just merged that PR last week), in sphinx its just in the conf.py

chrisjsewell avatar Apr 25 '22 11:04 chrisjsewell

Any updates pertaining to enabling line numbers to be shown on code blocks? Thanks!

gao-hongnan avatar Aug 24 '22 10:08 gao-hongnan

This feature would allow me to mention specific code line in my course.

pierrepo avatar Jun 27 '23 22:06 pierrepo

This feature would allow me to mention specific code line in my course.

@pierrepo See https://github.com/executablebooks/jupyter-book/issues/1254#issuecomment-1108417408. This worked just fine for my thesis

michaelosthege avatar Jun 27 '23 23:06 michaelosthege

Hello @michaelosthege

Thanks for your comment. I did not think it would work out of the box... and it did :exploding_head:

So I have now to replace all:

```python

by

```{code-block} python
---
linenos: True
---

in my course. Many thanks for your help @michaelosthege :pray:

@chrisjsewell I still believe that a global code line numbering option would be useful in Jupyter Book.

pierrepo avatar Jun 28 '23 06:06 pierrepo