How to Embed Code in Medium
Two easy methods for embedding code in your Medium article.
When writing an article about coding, writers often find it difficult to add code to the article. Code in normal text ruins the formatting and does not differentiate the text from the code. Some writers overcome this problem by uploading the screenshot of the code.
In this article I will explain two easy methods to embed code into your Medium article.
Method 1: Embed directly in medium
To embed code directly in medium, use the following shortcut key:
- Mac : Command+ Option+6
- Windows: Control + Alt + 6
- Linux: Control + Alt + 6
When you press these keys, a grey box as below will appear, inside which you can write your code.
<Enter your code here>
Check how the code formatting is retained . The code stands out with the help of typewriter font and highlighting.
#Title
plt.title(f"G 20 Countries : {abbr['gni_pc']} vs {abbr['life_ex']}") # x and y axis labels plt.xlabel(abbr['gni_pc']) plt.ylabel(abbr['life_ex']) #Country names
for i in range(hdi_df.shape[0]):
plt.text(df.gni_pc[i], y=df.life_ex[i], s=df.Country[i]))
Method 2: Embed using Github Gist
Github Gist is a much more appealing and readable option compared to directly embedding using Medium.
First, you have to sign up to Github gist and login to your account.
On the top left of Github gist page, you can find a ‘+’ sign, you can press on it to create a new gist.

You’ll be taken to the next page where you have to enter the following details:
i. gist description
ii. your code snippet’s title or filename
iii. your code snippet to be shared

You can save it as a public or secret gist.
If you want to publish it on medium, then you should save your gist as public.

Copy the link to your gist (url) or get the sharable link.
It can be embedded into Medium article by simply pasting it in a new line or using embed link option.

This is how your gist will then look on medium.
This code is written for an article on quadrant analysis.
The advantage of using Git Gist is that when the code is edited in Gist, the changes will be reflected in the Medium article and wherever the code is shared.
Video Format
If you prefer the video format of content, you can check out the following YouTube Video.
Become a Member
I hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other authors on variety of topics.
Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.