34

New to Jupyter.

Want to embed code for illustration without execution in a Markdown cell.

http://jupyter-notebook.readthedocs.org/en/latest/examples/Notebook/rstversions/Working%20With%20Markdown%20Cells.html

This link basically says one may do it but does not say how to do it. It says for example to make the text a heading, you use the pound sign #. But it does not say how to embed the code but just showed it is doable.

Tried to search online and find the same info as in the link provided.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
StayLearning
  • 601
  • 2
  • 7
  • 18
  • 5
    Like in other Markdown, you can either indent by four spaces, or surround the block with triple backticks. – Thomas K Feb 25 '16 at 17:04
  • As @ThomasK demonstrates, this can be accomplished via CSS. Markdown is a writing format, not a publishing format. Things like margins are out of scope for Markdown. – Waylan Feb 25 '16 at 18:11
  • You can download the notebook from [here](http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Working%20With%20Markdown%20Cells.ipynb#Embedded-code). Start a jupyter notebook server and edit the marldown cells you are interested in to see how is coded to be rendered that way ;-) – kikocorreoso Feb 26 '16 at 19:40

6 Answers6

54

You can contain the code in three backslashes backticks. If you specify the language in the beginning it will also color code as usual.

e.g.

```python
from pyomo.environ import *
model = Concretemodel()
```
mxnoqwerty
  • 641
  • 5
  • 5
22

I am a new to Jupyter Notebook too, must say that their documentation is awful as they say what we can do instead of HOW to do it. I have opened already made '.ipynb' notebook and figured it out, I will list a couple of them from unexplained documentation:

  1. Make text ITALIC: *Italic*
  2. Make text BOLD: **Bold**
  3. List item as a bullet: dash and space -
  4. List item as a number: Simple as number and dot 1.
  5. Indenting text: Greater than and space >
  6. Inline code span: Back quotation mark " ` "
  7. Block of code: Triple back quotation marks " ``` "
  8. Link a section: [Title of Section](#title-of-section)
  9. Hyperlink: [Text](URL)

I hope everyone has found their solution.

Uki.
  • 251
  • 2
  • 6
14

One way is to use code block. Insert your code in markdown cell between code tag.

For example:
<code> insert your code </code>

Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
PPK
  • 307
  • 2
  • 8
1

Using Jupyter Lab, and coding in r, the way to enter code blocks in a markdown cell is easy and has good and predictable results. Just follow the same steps each time.

Press Esc key, type m for markdown cell, press Enter key. The cursor is now in the markdown cell waiting for instructions.

Press the Tab key once. Most often the tab key is defaulted to four spaces. Press the Tickmark key 3 times

Press the Enter button once. The cursor will descend 1 line and remain within the tab

Type your code or paste a code block. If code is pasted, it will most likely need to become aligned below the tickmarks. Completely select all the code, then press the Tab key to move the selected code. This will align the code below the tickmarks.

Press the Enter button once. This should place the cursor directly below the start of the code.

Type the Tickmark key 3 times.

Press the Control + Enter keys to exit the markdown cell.

Gray
  • 1,164
  • 1
  • 9
  • 23
0

For inline code blocks in Jupter Notebook use the left quote character (`) the character to the left of the number [1] and above [Tab] on most keyboards.

Malman
  • 39
  • 1
  • 7
-1

The language will be detected automatically, if possible. Or you can specify it on the first line with 3 colons and the language name.

    :::python 
    import abc

For futher information, read https://sourceforge.net/p/jupiter/wiki/markdown_syntax/ (: