Skip to content

Instantly share code, notes, and snippets.

@9999years
Created February 4, 2019 15:08
Show Gist options
  • Save 9999years/6ef93a47d0b636d2057dba327260302f to your computer and use it in GitHub Desktop.
Save 9999years/6ef93a47d0b636d2057dba327260302f to your computer and use it in GitHub Desktop.
something about the LaTeX listings package and OpenType ligatures
import sys
def esc(txt):
left = txt
right = txt
chrs = r'\~^'
for chr in chrs:
right = right.replace(chr, '\\char"' + format(ord(chr), 'X'))
left = left.replace('\\', '\\\\')
chrs = r'$&%#{}_'
for chr in chrs:
right = right.replace(chr, '\\' + chr)
left = left.replace(chr, '\\' + chr)
return left, right
def main2():
sys.stdout.reconfigure(encoding='utf-8')
with open('ligs.txt', encoding='utf-8') as f:
for l in sorted(map(str.strip, f.readlines()), key=len):
length = len(l)
l, r = esc(l)
print(f'{{{l}}}{{{{\\hbox{{{r}}}}}}}{length}')
def main():
sys.stdout.reconfigure(encoding='utf-8')
with open('ligs2.txt', encoding='utf-8') as f:
# sorted pairs
for seq, cp in sorted(map(str.split, f.readlines()),
key=lambda p: len(p[0])):
length = len(seq)
slen = str(length)
seq, _ = esc(seq)
cp = int(cp, 16)
print('{' + seq + '}'
+ '{\\makebox[' + slen + '\\pprochar][r]'
+ '{\\char"' + format(cp, 'X') + '}}'
+ slen)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment