Jupyter kernel to highlight syntax

Viewed 1992

I need to write lecture notes (about C++) and I want to use Jupyter Notebook.

Is there any simple kernel (or something else) to just highlight C++ syntax? I don't need to run code.

For now I use Markdown cells to write C++ code. I write code inside <pre> tags. There are two huge problems:

  • no syntax highlighting;
  • for example vector<int> is parsed as vector + <unknown tag> so the displayed result is vector. I have to workaround this by writing the following: vector&lt;int>. It's monotonous.

Thanks for any ideas.

1 Answers

You can do this in a Markdown cell type by specifying the language of a triple-backtick (```) code block. Change the cell type to Markdown, then make a code block like this:

```c++
while(true) {
  cout << "hello world forever" << endl;
}
```

Hit Ctrl+Enter and you should see the cell rendered with code highlights.

enter image description here

Jupyter includes code highlighting via the Pygments module, so you can specify any of the supported languages in the code block.

Related