Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2021
What would you like to do?

#LaTeX in IPython Notebook

With this extension you can use LaTeX-style references within IPython (Notebook) while still using markdown for formatting.


  • ipython-dev
  • biblio-py
  • markdown

An example notebook is available here.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
What : Integration of LaTeX into IPython (extension)
Author : Marcin 'zmk' Zamojski
License : GPLv3
Maintained at :
IPython Notebook at :
With this extension you can use LATEX style references within IPython (Notebook) while still using markdown for formatting.
* ipython-dev
* biblio-py
* markdown
First, you need to load the extension. Use %load_ext (or better %reload_ext):
In [1]:
%reload_ext ipyBibtex
You can specify the max number of co-authors in the output before substituting with 'et. al.,' using %etal. The default value upon loading the extension is 3, i.e. articles with 4+ authors will have 'et. al.'
This needs to be done before %bibFile
In [2]:
%etal 3
Next you need to specify a bibliography file you want to use. There are two line magics available for this.
* %bibFile will open the file and prepare the required dictionaries
* %bibFileRobust will do the same and print all keys that were found
In [3]:
%bibFile path/to/file.bib
Finally you can write your text with in a code cell with %%cite cell magic.
In [4]:
Lorem ipsum dolor sit amet __\citep{hansen1982,gallanttauchen1996,crealkoopmanlucas2013}__, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.
Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.
Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius, __\cite{hayashi2000,engle1982}__.
2013-07-15 # v0.1.0 # Load bib files and provide basic substitution for \cite, \citet, and \citep
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
from IPython.core.displaypub import publish_display_data
from markdown import markdown
from yapbib import biblist
import re
class printTex(Magics):
def __init__(self, shell):
super(printTex, self).__init__(shell)
# self.bibliography=None
wrapText=re.compile('(?<!\n)\n(?!\n)|[ ]+')
self.wrapText=lambda x: wrapText.sub(' ', x)
# def cite
def bibFile(self, line, robust=False):
'''Opening bibliography file'''
bibliography.import_bibtex(line, normalize=False)
for key in bibliography.bib.keys():
if len(curAuth[0])>1:
if len(curAuth[0])>self.etal:
curAuth=curAuth[0][0]+' et. al.'
curAuth= ', '.join(curAuth[0][:-1])+' and '+curAuth[0][-1]
self.cite['t'][bibliography.bib[key]['_code']]='{0} ({1})'.format(curAuth, curArt['year'])
self.cite['p'][bibliography.bib[key]['_code']]='{0}, {1}'.format(curAuth, curArt['year'])
if robust:
{'text/plain': '{0} loaded, there are {1} keys ({2})'.format(line, len(self.cite['t'].keys()), ', '.join(self.cite['t'].keys())),
'text/html': markdown('_{0}_ loaded<br/>There are __{1}__ keys:<br/>{2}'.format(line, len(self.cite['t'].keys()), ', '.join(self.cite['t'].keys())))})
def bibFileRobust(self, line):
self.bibFile(line, robust=True)
def etal(self, line):
def cite(self, line, cell):
if len(line)>0:
for c in self.citeRe.finditer(cell):
refs=[self.cite['p'][ref.strip()] if'mode')=='p' else self.cite['t'][ref.strip()]
for ref in'cite').split(',')]
cell=cell.replace('\\''all'), '; '.join(refs) if'mode')!='p' else '('+'; '.join(refs)+')')
'text/html': markdown(cell),
'text/plain': self.wrapText(cell)
def load_ipython_extension(ip):
#register magic to hold state
printTexObj = printTex(ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment