python - Remove LaTeX macro spanning multiple lines -



python - Remove LaTeX macro spanning multiple lines -

i have next piece of latex code , want remove occurences of \new{"text spanning multiple lines"}. "text spanning multiple line" needs preserved , "\new{" , somewhere in file "}" needs removed, within new brackets should left untouched. tabs , spaces , newlines need preserved. tried write python application not produce decent output. hard part place remove brackets (and can on next line).

input:

\chapter{a \new{very} little \new{chapter}} \begin{itemize} \item \new{bla} \item dusse \item mekker \end{itemize} \new{this new multiline \texttt{text} things \tbd{tbd} in between } end

output (expected):

\chapter{a little chapter} \begin{itemize} \item bla \item dusse \item mekker \end{itemize} new multiline \texttt{text} things \tbd{tbd} in between end

own solution in python works:

read line replace \new{ occurence marker (char 0xff) read c in line check if c marker, set marked = true, set marked_cnt nested brackets, read next char else check: c == '{', increment marked_cnt else check: c == '}' , marked == true, decrement marked_cnt if marked_cnt == -1, reset marked = false, marked_cnt = 0, read next char print "valid" char #!/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") line in fin: l = line.replace("\new{", marker) c in l: if c == marker: marked = true marked_cnt = 0 go on elif c == '{': marked_cnt += 1 elif ((c == '}') , (marked == true)): marked_cnt -= 1 if marked_cnt == -1: marked = false marked_cnt = 0 go on fout.write(c) fin.close() fout.close()

try using regular expressions:

import re myre = re.compile(r'\\new{\w+}') match in myre.findall(mystring): newstring = match.replace('\new{','') newstring = newstring.replace('}','') mystring.replace(match,newstring)

this, however, won't rid of multi-line problem. solve that, go through string directly, checking opening , closing of brackets:

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 , position<len(s): position +=1 print(len(s), position,s[position]) if s[position] == '}' , openbrackets == 0: s = s[:position]+s[position+1:] trailexist = false print("removed!", position) elif s[position] == '{': openbrackets += 1 print('openbrackets:',openbrackets) elif s[position] == '}' , openbrackets>0: openbrackets -= 1

python latex multiline

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -