Syntax Highlighting in iPython Notebook Markdown Cell

六眼飞鱼酱① 提交于 2019-12-12 07:47:13

问题


Is there a way to get a Markdown cell in iPython Notebooks to highlight syntax in code blocks?

For instance, in GitHub, one can get the desired effect via the following.

```python
>>>print('hello')
```

回答1:


The GitHub Flavored Markdown-style of denoting code using the triple-backtick is now supported in IPython master branch on GitHub, and so will be included in the 1.0 release.

As Jakob noted, even prior to this, you could use regular markdown for code, in which you just need to indent your code by four spaces, and this continues to be a valid way of displaying code in your IPython notebook.




回答2:


using IPython 0.13.1 syntax highlighting is as easy as (in a markdown cell):

some text

    def foo():
        print 'bar'
        return 0

some text

Just, use a blank line before and indent the code (see example notebooks shipped with Ipython). This works for Python and some other languages.




回答3:


In IPython 7.2.0 notebooks you can use:

Text `code` text

in a markdown cell to print highlighted code inline.



来源:https://stackoverflow.com/questions/13794698/syntax-highlighting-in-ipython-notebook-markdown-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!