16

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')
```
3
  • Does not seem this is present in current release. Submit a feature request? Commented Dec 11, 2012 at 16:12
  • I am interested in that as well Commented Dec 17, 2012 at 15:07
  • 1
    And now it certainly works :) Commented Jan 9, 2018 at 4:54

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have to add explicit language tag, e.g. ```py ... ``` for syntax highlighting to kick in.
1

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.

1 Comment

Doesn't seem to highlight syntax any longer :(
1

In IPython 7.2.0 notebooks you can use:

Text `code` text

in a markdown cell to print highlighted code inline.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.