简体   繁体   English

我可以在markdown / Latex中创建可链接的标签吗? (Jupyter笔记本)

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

I have a Jupyter notebook and I have cells containing markdown like this: 我有一个Jupyter笔记本,并且我的单元格包含如下标记:

\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. 除了\\href\\url\u003c/code> , <a href=等,它们都作为文本的一部分呈现,而不是作为可单击的超链接处理。

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. 原来,已经有一个答案涉及如何在Jupyter中发出\\usepackage语句。 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. 但是,现在我详细阅读了它,实际上它涵盖了如何在将笔记本转换为实际的乳胶文件的过程中向笔记本添加\\usepackage指令。 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 写一个同时包含乳胶(markdown,确实)和python单元的笔记本
    • add tags (that will eventually become links) as you desire to the latex 根据需要添加标签(最终将成为链接)
  • generate output by executing all of your python code 通过执行所有python代码生成输出
  • convert the final result to a static latex file, using the trick described in the above answers to add the hyperref package 使用以上答案中描述的技巧将最终结果转换为静态乳胶文件,以添加hyperref包

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. 最终结果将是一个.pdf ,其中包含Markdown单元格中的所有乳胶以及笔记本中python单元格中的所有源和输出。 Of course the obvious downside is that it would not be able to execute the python source directly. 当然,明显的缺点是它将无法直接执行python源。

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. 尽管我早先对此表示怀疑,但事实证明,您实际上可以将Markdown单元中的方程式链接到笔记本中的任何其他Markdown单元。 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 交换\\eqref\\tag

First, replace the \\tag commands in your equation with \\eqref : 首先,用\\eqref替换方程式中的\\tag命令:

\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. 请注意,如上例所示,您必须使用\\quad或其他有效的乳胶数学模式空白之一添加自己的间距。

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. \\eqref宏的内容应与相应的\\label的内容匹配,但是显示的链接文本将与标记的内容匹配。

Here's a link to a live notebook online with some example \\eqref links in the last cell. 这是在线活动笔记本的链接 ,最后一个单元格中有一些\\eqref链接示例

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 \\tag替换为html链接,并使用$...$代替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: 如果您只是使用美元符号定界符(即$<your-math-here>$ )调用常规的内联$<your-math-here>$ ,则无需在align环境中包装方程式,然后可以将每行末尾的\\tags替换为使用[]()语法的markdown html链接:

$\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 ). 您可能还会发现,使用内联mathmode渲染方程式的方式不如使用display mathmode渲染方程式的方式(如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. 从好的方面来说,笔记本中的html链接似乎受到更多的支持/更强大,并且您无需重新加载即可呈现它们。

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. 这是在线活动笔记本另一个链接 ,在最后一个单元格中有一些示例html方程链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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