删除跨多行的LaTeX宏
我有一段LaTeX代码,想要去掉所有的\NEW{"跨多行的文本"}。这里的"跨多行的文本"要保留,只需要去掉"\NEW{"和文件中某个地方的"}",而大括号里的内容要保持不变。制表符、空格和换行也要保留。我之前尝试写了一个Python程序,但输出效果不太理想。最难的部分是去掉大括号的地方(有可能在下一行)。
输入:
\chapter{A \NEW{very} small \NEW{chapter}}
\begin{itemize}
\item \NEW{Bla}
\item Dusse
\item Mekker
\end{itemize}
\NEW{This is new
multiline \texttt{text} with some things \TBD{TBD} in between
} The end
输出(预期):
\chapter{A very small chapter}
\begin{itemize}
\item Bla
\item Dusse
\item Mekker
\end{itemize}
This is new
multiline \texttt{text} with some things \TBD{TBD} in between
The end
我自己写的有效的Python解决方案:
- 读取一行
- 把\NEW{的出现替换成一个标记(字符0xff)
- 逐个读取行中的字符c
- 检查c是否是标记,如果是,就设置marked为True,并为嵌套的大括号设置marked_cnt,读取下一个字符
- 否则检查:如果c是'{',就增加marked_cnt
- 否则检查:如果c是'}'并且marked为True,就减少marked_cnt
- 如果marked_cnt等于-1,重置marked为False,marked_cnt为0,读取下一个字符
- 打印“有效”的字符
#!/usr/bin/env python2.7 import sys marker=chr(255) marked=False marked_cnt=0 fin = open("file.tex", "r") fout = open("file.tex.out", "w") for line in fin: l = line.replace("\NEW{", marker) for c in l: if c == marker: marked = True marked_cnt = 0 continue elif c == '{': marked_cnt += 1 elif ((c == '}') and (marked == True)): marked_cnt -= 1 if marked_cnt == -1: marked = False marked_cnt = 0 continue fout.write(c) fin.close() fout.close()
1 个回答
0
试试用正则表达式:
import re
myRe = re.compile(r'\\NEW{\w+}')
for match in myRe.findall(myString):
newstring = match.replace('\NEW{','')
newstring = newstring.replace('}','')
myString.replace(match,newstring)
不过,这样做还解决不了多行的问题。要解决这个问题,可以直接遍历字符串,检查括号的开和关:
while s.find('\\NEW{')>-1:
position = s.find('\\NEW{')
print(position, s[position:position+4])
s = s[0:position]+s[position+5:]
trailexist = True
openbrackets = 0
while trailexist and position<len(s):
position +=1
print(len(s), position,s[position])
if s[position] == '}' and openbrackets == 0:
s = s[:position]+s[position+1:]
trailexist = False
print("Removed!", position)
elif s[position] == '{':
openbrackets += 1
print('Openbrackets:',openbrackets)
elif s[position] == '}' and openbrackets>0:
openbrackets -= 1