Created
March 27, 2014 16:59
-
-
Save juhasch/9812482 to your computer and use it in GitHub Desktop.
Strip spaces after first $ for math equations. Required to produce single-$ math equations when running IPython nbconvert --to=latex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.nbconvert.preprocessors import * | |
import re | |
class StripDollarSpacesPreprocessor(Preprocessor): | |
"""Remove spaces after leading '$' in math equations. | |
Make sure '$ a=10 $' is converted to '$a=10 $' and not '\$ a=10 \$' | |
when running nbconvert and pandoc | |
""" | |
def replfunc(self,match): | |
""" Match '$<space>' and replace by single '$' """ | |
x = '\$(\s+)' | |
regex2 = re.compile(x) | |
a= match.group(0) | |
return regex2.sub('$',a) | |
def preprocess_cell(self, cell, resources, index): | |
"""Find all '$' delimited equations and strip space after starting '$' | |
""" | |
stringmatch = r'([\$\'])(?:(?=(\\?))\2.)*?\1' | |
regex = re.compile(stringmatch) | |
if 'source' in cell and cell.cell_type == "markdown": | |
cell.source = regex.sub(self.replfunc,cell.source) | |
return cell, resources |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment