Skip to content

Instantly share code, notes, and snippets.

@moritzschaefer
Last active June 19, 2019 08:26
Show Gist options
  • Save moritzschaefer/2c290cb92ebcab644819e1f87ff54adc to your computer and use it in GitHub Desktop.
Save moritzschaefer/2c290cb92ebcab644819e1f87ff54adc to your computer and use it in GitHub Desktop.
Fix org babel ipython export related issues
#!/usr/bin/env python
import sys
import re
import argparse
parser = argparse.ArgumentParser(description='Postprocess/fix org mode tex output')
parser.add_argument('--dont-replace-fig-labels', action='store_true')
parser.add_argument('--dont-delete-file-output', action='store_true')
parser.add_argument('infile', type=str)
parser.add_argument('outfile', type=str)
args = parser.parse_args()
with open(args.infile, 'r') as inf:
content = inf.read()
if not args.dont_delete_file_output:
content = re.sub(r'\[\[file:\\# Out\[[0-9]*?\]:(.*?)\]\]',
r'\1',
content,
flags=re.MULTILINE | re.DOTALL)
# now rename the figures
if not args.dont_replace_fig_labels:
for i, label_match in enumerate(re.finditer(r'\\label\{(fig:.*?)\}', content)):
label = label_match.groups()[0]
content = content.replace(f'\\label{{{label}}}', f'Figure {i+1}: ')
content = content.replace(f'\\ref{{{label}}}', f'{i+1}')
with open(args.outfile, 'w') as outf:
outf.write(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment