简体   繁体   中英

Can I make tags in markdown/Latex linkable? (Jupyter Notebook)

I have a Jupyter notebook and I have cells containing markdown like this:

\begin{align}
0 + S(a) &= S(0 + a) \tag{by definition of addition} \\
         &= S(a) \tag{by induction hypothesis}
\end{align}

I want certain tags to be clickable so they jump to the corresponding sections. For example I tried

\tag{[by definition of addition](#def_addition)}

In addition to \\href , \\url\u003c/code> , <a href= , etc, but they all get rendered as part of the text rather than processed as a clickable hyperlink.

Is this sort of thing not supported or is there a workaround?

Turns out there's already an answer covering how to issue a \\usepackage statement in Jupyter. But now that I read it in detail, it in fact covers how to add a \\usepackage directive to a notebook during the process of converting it into an actual proper latex file. Which is not really what you wanted, but it might be the best you can do.

So to use their solution, your workflow would be like this:

  • write a notebook containing both latex (markdown, really) and python cells
    • add tags (that will eventually become links) as you desire to the latex
  • generate output by executing all of your python code
  • convert the final result to a static latex file, using the trick described in the above answers to add the hyperref package

Your final result would be a .pdf that contains all of the latex from the markdown cells and all of the source and output from the python cells in your notebook. Of course the obvious downside is that it would not be able to execute the python source directly.

Guess what? In spite of my earlier skepticism, it turns out that you actually can link from equations in markdown cells to any other markdown cell in a notebook. I've come up with 2 ways of doing it. They both involve some tradeoffs, and they're both kind of dirty hacks, so you may still be better off with my other answer, but here goes:

Swap \\eqref for \\tag

First, replace the \\tag commands in your equation with \\eqref :

\begin{align}
0 + S(a) &= S(0 + a) \quad\quad\quad\quad \eqref{dadd} \\
         &= S(a) \quad\quad\quad\quad \eqref{ihop}
\end{align}

Note that, as in the example above, you'll have to add your own spacing with \\quad or one of the other valid latex mathmode whitespaces.

Once that's done, add a combination label/tag to any other markdown cell in the notebook:

$$\label{dadd}\tag{by definition of addition}$$

somewhere in the cell that holds the definition of addition and

$$\label{ihop}\tag{by induction hypothesis}$$

somewhere in the cell that holds the induction hypothesis.

Together, this will add links to your equations that look like tags. The contents of the \\eqref macro should match that of the corresponding \\label , but the link text that's displayed will match that of the tag.

Here's a link to a live notebook online with some example \\eqref links in the last cell.

Another caveat: in order to get the links to render you'll have to save your notebook and reload the browser running it, probably at least a few times.

Replace \\tag with html links, and use $...$ in place of align

Instead of wrapping your equations in the align environment, if you just invoke the regular inline mathmode using the dollar sign delimiters (ie $<your-math-here>$ ), you can then replace the \\tags at the end of each line with markdown html links using the []() syntax:

$\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad 0 + S(a) = S(0 + a) \qquad\qquad\qquad\qquad$ [(by definition of addition)](#dadd)
$\;\quad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad = S(a) \qquad\qquad\qquad\qquad\quad$ [(by induction hypothesis)](#ihop)

As you can see, the big drawback of this approach is that you have to do all of your own spacing, including the fiddly process of alignment. You may also find that the way equations are rendered using inline mathmode is less appealing than how they are rendered using display mathmode (as in an align ). On the plus side, the html links seem to be much more supported/robust in notebooks, and you don't need to reload to get them to render.

The syntax for the link endpoints is as so:

<a id='dadd'></a>

Here's another link to a live notebook online with some example html equation links in the last cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM