运行python脚本将betacode替换为希腊字母LaTeX

2024-09-28 19:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把现有的.tex-File中的betacode转换成普通的希腊字母。在

例如:我想替换:

\bcode{lo/gos}

使用简单:

^{pr2}$

所有其他的字形也是如此。幸运的是,似乎有一个python脚本可以做到这一点。但是,由于完全没有经验,我根本不知道如何运行它。在

以下是python sript的代码:

^{3}$

这里有一个小的.tex文件,它应该可以工作。在

\documentclass[12pt]{scrbook}
\usepackage[polutonikogreek, ngerman]{babel}
\usepackage[ngerman]{betababel}
\usepackage{fontspec}
%\defaultfontfeatures{Ligatures=TeX}
%\newfontfeature{Microtype}{protrusion=default;expansion=default;}

\begin{document}
    \bcode{lo/gos}
\end{document}

如果脚本不起作用:是否可以将代码Makro中的所有字符串转换为regex之类的内容?例如“o/”到ό等等?在这里选择的武器是什么?在


Tags: 代码脚本defaultlo经验documentfiletex
1条回答
网友
1楼 · 发布于 2024-09-28 19:27:41

我有安装python吗?

在shell提示符处尝试python -V。您的代码是python2代码,因此您将使用python2版本。在

enter image description here

我需要安装Python

如果您不需要复杂的环境(对于这个问题也不需要),最直接的方法就是转到python.org。别忘了你需要python2。在

运行程序

一般来说,它将简单到:

python beta2unicode.py myfile.tex-file

要获取输出:

^{pr2}$

剧本行吗?

差不多了。您需要将脚本末尾的代码替换为以下代码:

import sys

t = beta2unicodeTrie()

import re
BCODE = re.compile(r'\\bcode{[^}]*}')

for line in open(sys.argv[1]):
    matches = BCODE.search(line)
    for match in BCODE.findall(line):
        bcode = match[7:-1]
        a, b = t.convert(bcode.upper())
        if b:
            raise IOError("failed conversion '%s' in '%s'" % (b, line))
        converted = a.encode("utf-8")
        line = line.replace(match, converted)

    print(line.rstrip())

结果

\documentclass[12pt]{scrbook}
\usepackage[polutonikogreek, ngerman]{babel}
\usepackage[ngerman]{betababel}
\usepackage{fontspec}
%\defaultfontfeatures{Ligatures=TeX}
%\newfontfeature{Microtype}{protrusion=default;expansion=default;}

\begin{document}
    λόγοσ
\end{document}

相关问题 更多 >