Skip to content

Instantly share code, notes, and snippets.

@moritzschaefer
Last active June 19, 2019 08:26
Show Gist options
  • Select an option

  • Save moritzschaefer/2c290cb92ebcab644819e1f87ff54adc to your computer and use it in GitHub Desktop.

Select an option

Save moritzschaefer/2c290cb92ebcab644819e1f87ff54adc to your computer and use it in GitHub Desktop.

Revisions

  1. Moritz revised this gist Jun 19, 2019. 1 changed file with 20 additions and 10 deletions.
    30 changes: 20 additions & 10 deletions format-tex.py
    Original file line number Diff line number Diff line change
    @@ -2,21 +2,31 @@

    import sys
    import re
    import argparse

    infile, outfile = sys.argv[1:3]

    replace_labels_re = re.compile(r'\\label\{fig:(.*?)}')
    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(infile, 'r') as inf:
    with open(args.infile, 'r') as inf:
    content = inf.read()

    content = re.sub(r'\[\[file:\\# Out\[[0-9]*?\]:(.*?)\]\]', r'\1', content, flags=re.MULTILINE | re.DOTALL)
    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
    for i, label_match in enumerate(re.finditer(r'\\label\{(fig:.*?)\}', content)):
    label = label_match.groups()[0]
    content = content.replace(f'\\label{{{label}}}', f'\emph{{Figure {i+1}}}')
    content = content.replace(f'\\ref{{{label}}}', f'{i+1}')
    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(outfile, 'w') as outf:
    with open(args.outfile, 'w') as outf:
    outf.write(content)

  2. Moritz revised this gist May 27, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion format-tex.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    with open(infile, 'r') as inf:
    content = inf.read()

    content = re.sub(r'\[\[file:\\# Out\[[0-9]*?\]:(.*)\]\]', r'\1', content, flags=re.MULTILINE | re.DOTALL)
    content = re.sub(r'\[\[file:\\# Out\[[0-9]*?\]:(.*?)\]\]', r'\1', content, flags=re.MULTILINE | re.DOTALL)
    # now rename the figures
    for i, label_match in enumerate(re.finditer(r'\\label\{(fig:.*?)\}', content)):
    label = label_match.groups()[0]
  3. Moritz created this gist May 24, 2019.
    22 changes: 22 additions & 0 deletions format-tex.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/usr/bin/env python

    import sys
    import re

    infile, outfile = sys.argv[1:3]

    replace_labels_re = re.compile(r'\\label\{fig:(.*?)}')

    with open(infile, 'r') as inf:
    content = inf.read()

    content = re.sub(r'\[\[file:\\# Out\[[0-9]*?\]:(.*)\]\]', r'\1', content, flags=re.MULTILINE | re.DOTALL)
    # now rename the figures
    for i, label_match in enumerate(re.finditer(r'\\label\{(fig:.*?)\}', content)):
    label = label_match.groups()[0]
    content = content.replace(f'\\label{{{label}}}', f'\emph{{Figure {i+1}}}')
    content = content.replace(f'\\ref{{{label}}}', f'{i+1}')

    with open(outfile, 'w') as outf:
    outf.write(content)