university-setup/scripts/ieeeeqnarray.py

29 lines
1.1 KiB
Python

# A python script designed to parse the contents of an \[ \] in latex (given in the arguments), and make a nice IEEEeqnarray* with correct indent and columns from it.
import sys
indent=len(sys.argv[1])-4 # Indent got by the indent of the leading \[ of equation
topline = sys.argv[2].strip() # Get the content of the first line of the equation
level = 0 # Counts the 'level' of stacked braces
position = 0 # Will save the position of the found = in the top line
parsed_line = '' # Parsed top line
for x in topline:
if x in ['(','[','{']:
level+=1
parsed_line+=x
elif x in [')',']','}']:
level-=1
parsed_line+=x
elif level == 0 and x == '=':
position = len(parsed_line)
parsed_line+='& = &'
else:
parsed_line+=x
# Set correct indent and newline to the parsed line
parsed_line = ' '*(indent+4) + parsed_line[0:-1].strip() + ' \\\\\n'
# Print the output, adding \begin{}, the top line, a new line with '& = &' and \end{}
print(" "*indent +"\\begin{IEEEeqnarray*}{rCl}\n" + parsed_line + ' '*(position+indent+4) + "& = & \n" + " "*indent + "\\end{IEEEeqnarray*}")