Skip to content

Instantly share code, notes, and snippets.

@mahito1594
Last active May 16, 2019 16:24
Show Gist options
  • Save mahito1594/f2db8ef8dc01249de5d81adaab27f75a to your computer and use it in GitHub Desktop.
Save mahito1594/f2db8ef8dc01249de5d81adaab27f75a to your computer and use it in GitHub Desktop.
`.bib` 内のアクセント付き文字の unicode <-> LaTeX command 変換用スクリプト
#! /usr/bin/env python3
# Copyright (C) 2019 Mahito TANNO
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org>
"""
* Overview:
This is my personal Python script which convert LaTeX command to
unicode characters. Typically, I use this for BibTeX database.
* Requirements:
- Python3
- pylatexenc
You can install by `pip install pylatexenc`
* Usage:
`ltx2utf FILE.bib`.
"""
import os
import sys
import shutil
from pylatexenc.latex2text import LatexNodes2Text
bib_file = sys.argv[1]
backup_file = bib_file + '.bak'
if not os.path.isfile(bib_file):
print('Error: please input your file.')
sys.exit(1)
shutil.copy(bib_file, backup_file)
with open(bib_file, 'rt') as file:
latex = file.read()
text = LatexNodes2Text(keep_inline_math=True, strict_latex_spaces=True, keep_braced_groups=True).latex_to_text(latex)
with open(bib_file, 'wt') as file:
file.write(text)
#! /usr/bin/env python3
# Copyright (C) 2019 Mahito TANNO
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org>
"""
* Overview:
This is my personal Python script which covert unicode accented characters
to LaTeX commands. Typically, I use this for BibTeX databases.
* Requirements:
- Python3
- pylatexenc
You can install by `pip install pylatexenc`.
* Usage:
`utf2ltx FILE.bib`.
"""
import os
import sys
import fileinput
from pylatexenc.latexencode import utf8tolatex
bib_file = sys.argv[1]
if not os.path.isfile(bib_file):
print('Error: please input your file.')
sys.exit(1)
with fileinput.FileInput(bib_file, inplace=True, backup='.bak') as file:
for line in file:
line = utf8tolatex(line, non_ascii_only=True)
print(line, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment