Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View README.md

LaTeX in IPython Notebook

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

Required:

  • ipython-dev
  • biblio-py
  • markdown

An example notebook is available here.

View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#$\\LaTeX$ in IPython Notebook\n",
"\n",
"With this extension you can use $\\LaTeX$ style references within IPython (Notebook) while still using markdown for formatting.\n",
"\n",
"Required:\n",
"\n",
"* ipython-dev\n",
"* biblio-py\n",
"* markdown\n",
"\n",
"First, you need to load the extension. Use `%load_ext` (or better `%reload_ext`):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%reload_ext ipyBibtex "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.'\n",
"\n",
"This needs to be done __before__ `%bibFile`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%etal 3 "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next you need to specify a bibliography file you want to use. There are two line magics available for this.\n",
"\n",
"* `%bibFile` will open the file and prepare the required dictionaries\n",
"* `%bibFileRobust` will do the same and print all keys that were found"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%bibFile path/to/file.bib"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally you can write your text with in a _code_ cell with `%%cite` cell magic."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cite\n",
"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.\n",
"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.\n",
"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.\n",
"Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.\n",
"\n",
"Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.\n",
"Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius, __\\cite{hayashi2000,engle1982}__.\n",
"Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum.\n",
"Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima.\n",
"Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum."
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<p>Lorem ipsum dolor sit amet <strong>(Hansen, 1982; Gallant and Tauchen, 1996; Creal, Koopman and Lucas, 2013)</strong>, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.\n",
"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.\n",
"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.\n",
"Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.</p>\n",
"<p>Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.\n",
"Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius, <strong>Hayashi (2000); Engle (1982)</strong>.\n",
"Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum.\n",
"Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima.\n",
"Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</p>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Lorem ipsum dolor sit amet __(Hansen, 1982; Gallant and Tauchen, 1996; Creal, Koopman and Lucas, 2013)__, 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.\n",
"\n",
"Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius, __Hayashi (2000); Engle (1982)__. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum."
]
}
],
"prompt_number": 4
}
],
"metadata": {}
}
]
}
View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
'''
What : Integration of LaTeX into IPython (extension)
Author : Marcin 'zmk' Zamojski
License : GPLv3
Maintained at : https://gist.github.com/z-m-k/6080008
IPython Notebook at : http://nbviewer.ipython.org/6080008/ipyBibtex.ipynb
With this extension you can use LATEX style references within IPython (Notebook) while still using markdown for formatting.
Required:
* ipython-dev
* biblio-py
* markdown
++++++++++++++
Usage
++++++++++++++
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]:
%%cite
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}__.
++++++++++++++
Changelog
++++++++++++++
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
 
@magics_class
class printTex(Magics):
''''''
 
def __init__(self, shell):
super(printTex, self).__init__(shell)
# self.bibliography=None
self.etal=3
self.cite={
't':{},
'p':{}
}
wrapText=re.compile('(?<!\n)\n(?!\n)|[ ]+')
self.wrapText=lambda x: wrapText.sub(' ', x)
self.citeRe=re.compile('(?P<all>\\cite(?P<mode>t|p)*\{(?P<cite>[\w\d\s,]*)\})')
# def cite
@line_magic
def bibFile(self, line, robust=False):
'''Opening bibliography file'''
bibliography=biblist.BibList()
bibliography.import_bibtex(line, normalize=False)
for key in bibliography.bib.keys():
curArt=bibliography.bib[key]
curAuth=zip(*curArt['author'])[1:3]
if len(curAuth[0])>1:
if len(curAuth[0])>self.etal:
curAuth=curAuth[0][0]+' et. al.'
else:
curAuth= ', '.join(curAuth[0][:-1])+' and '+curAuth[0][-1]
else:
curAuth=curAuth[0][0]
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:
publish_display_data('printTex',
{'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())))})
 
@line_magic
def bibFileRobust(self, line):
self.bibFile(line, robust=True)
 
@line_magic
def etal(self, line):
self.etal=int(line)
 
@cell_magic
def cite(self, line, cell):
if len(line)>0:
cell=line+'\n'+cell
for c in self.citeRe.finditer(cell):
refs=[self.cite['p'][ref.strip()] if c.group('mode')=='p' else self.cite['t'][ref.strip()]
for ref in c.group('cite').split(',')]
cell=cell.replace('\\'+c.group('all'), '; '.join(refs) if c.group('mode')!='p' else '('+'; '.join(refs)+')')
publish_display_data('printTex',
{
'text/html': markdown(cell),
'text/plain': self.wrapText(cell)
})
def load_ipython_extension(ip):
#register magic to hold state
printTexObj = printTex(ip)
ip.register_magics(printTexObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.