129

How do I reference a cell in a IPython notebook markdown?

I know how to make a reference to an external link. But is there a way to assign an ID to a cell and then refer to the cell in the markdown?

Michael_Scharf
  • 33,154
  • 22
  • 74
  • 95

2 Answers2

167

Yes, there's way to do just that in IPython.

First, define the destination in the cell you want to link with a html anchor tag and give it an Id. For example:

<a id='another_cell'></a>

Note - When you run the above cell in markdown, it will become invisible. You can add some text above the anchor to identify the cell.

Second, create the internal hyperlink to the destination created above using Markdown syntax in another cell and run it:

[Another Cell](#another_cell)

Now, clicking on link should take you to the destination.

Amit
  • 19,780
  • 6
  • 46
  • 54
  • 7
    Could you elaborate your answer whether or not it is possible to reference to a certain code cell and if so, how? – Joma May 27 '15 at 08:07
  • 3
    @Joma It's easy if you're only interested in navigation. If the code cell is already preceded by a Markdown cell, then you need only place the anchor tag at the end of the preceding Markdown cell. If not, (e.g. you have two consecutive code cells and want an anchor for the second one) you could just create an "invisible" Markdown cell above your code cell, as Amit mentions, containing the anchor tag. – eenblam Sep 12 '15 at 23:10
  • How do you add a subsection? Like a, b, c under 1. – RodrikTheReader Jun 05 '18 at 11:56
  • 5
    this blog post has more useful information: http://sebastianraschka.com/Articles/2014_ipython_internal_links.html – exp1orer Jun 11 '18 at 22:12
  • 3
    Hmm, this appears not to be working on Google Colab – Bendemann Jan 16 '21 at 15:38
  • 1
    Is anyone able to do this on google colab? – bunNyBug Aug 07 '22 at 14:31
  • This is also not working well in vscode. Some links work but others dont for no reason. – Portfedh Sep 13 '22 at 23:07
68

If you want to link directly to a specific section of your notebook, it can be useful to use this code: [section title](#section-title). Note that, for the text in the parentheses, you should replace spaces and special characters with a hyphen.

As an example, consider this case in which we want to insert a link to the section 'Univariate + One step ahead':

Section that I want to link

To do so, we just need to add the code [Univariate + One step ahead](#Univariate-+-One-step-ahead):

Code to create the link

This way we avoid the introduction of an anchor tag, since the section title already acts as one.

Pedro Marcelino
  • 958
  • 7
  • 7
  • 8
    Good call on the replacing spaces with a hyphen! Definitely was making that mistake! – Greg Hilston Feb 05 '19 at 04:24
  • 3
    Good idea, but this approach will not produce the expected outcome if the hyperlink is a title itself, in which case clinking the hyperlink will jump to itself. Example: anchor is `## Univariate` (`##` = 2nd level title), if then one wants a hyperlink to that anchor by setting *e.g.*: `### [Univariate](#Univariate)`, clinking on it will bring the hyperlink itself to the top of the page, not the expected section. – calocedrus Feb 13 '19 at 02:09
  • This didn't work for me on azure... – Att Righ Nov 23 '21 at 14:29
  • @calocedrus is right. However, one can bypass such a problem if, instead of nesting the title itself between hyperlink tags, such as `## Univariate`, one just uses an unfilled hyperlink tag (``) before (or after) setting the title itself. After all, the point is to go to the title cell once the link is clicked, not if the title acts as the linked object itself. – AZarketa Feb 03 '22 at 17:17
  • Doesn't work in DataSpell – jtlz2 Mar 25 '22 at 13:13