#
# maple.text.reverse.nawk  
#
# Undo changes in text regions produced by Export to LaTeX in MapleV Release 6.01. 
# This script will allow one to include LaTeX commands in text areas of a Maple
# worksheet, file.mws, so that the commands will still be executable after 
# an Export to LaTeX.
#
# Author: Maury Rahe, version 10/31/00.
#
# 
# Caveat: Since Maple's Export to Latex breaks lines at strange places, there may be
# a *few* places where changes have to be made by hand. LaTeX will inform you if there
# are any of these, and you can deal with those exceptions by a text editor.
#
#
# Usage: On the command line, in the directory where the files are, type
# nawk -f maple.text.reverse.nawk file.tex > output.tex
# followed by . Maple's LaTeX changes in file.tex are rolled back in output.tex.
# Output.tex will then be used as input to the LaTeX program.
#
#
# We assume that we start in the header area, a non mapleinline text region.
#
# The values of x, y, and z indicate what area of the text we are in.
#
# x=1 in a text area (generally need to reverse changes, except inline math)
# x=0 in a Maple output area (never reverse changes)
# 
# Sometimes in a text area, we *don't* want to reverse the changes, since
# the text contains inline math formulas. 
#
# Maple distinguishes math formulas in text areas which are left as Maple commands 
# and Maple commands that are to be prettyprinted. We keep track of which kind
# of inline math formula we're in by setting y and z values.
# 
# Set y=0, z=0 outside of a Maple inline mathematics text area. 
# Set y=1 when the inline text command is left in Maple form.
# Set z=1 when the inline text command region is displayed in LaTeX prettyprint.
# 
# The y and z values are decremented after seeing }{% , the end of the formula.
#
BEGIN{x=1;y=0;z=0;w=0}    # we start in header region, which is text, nonformula, no split
#
# Maple's default LaTeX class is article. This class may need to be changed for inclusion 
# in a book. The deq_macro file allows one to specify certain fonts, for example, tty used
# for the monospaced typewriter output of Maple commands. One may also wish to set a 
# default textwidth different than Maple's. If any of these conditions apply, the user
# should adjust and uncomment the second line below.
#
# Replace \documentclass{article} with \documentclass{book}\input{deq_macro}\textwidth=6in
#
#/^\\documentclass\{article\}/{sub(/^\\documentclass\{article\}/,"\\documentclass\{book\}\\input\{deq_macr\}\\textwidth=6in")}
#
# Maple output is placed in three kinds of non-overlapping regions, whose 
# boundaries are each identified by a \begin{ } and a \end{ } command, 
# which is always found at the beginning of a line.
#
# Maplelatex region: (Maple mathematical output.)
#
/^\\begin\{maplelatex\}/{x=0}              # set no substitution flag, starting Maple output
/^\\end\{maplelatex\}/{x=1}	           # set substitution flag, leaving maple output
#
# Mapleinput region: (Active mathematical input.)
#
/^\\begin\{mapleinput\}/{x=0}              # set no substitution flag
/^\\end\{mapleinput\}/{x=1}	           # set substitution flag
#
# Mapletty region:  (Warnings, etc.)
#
/^\\begin\{maplettyout\}/{x=0}             # set no substitution flag
/^\\end\{maplettyout\}/{x=1}	           # set substitution flag
#
# Inline Maple commands in the text areas need to be bypassed. Set y=1 for
# {1d and z=1 for {2d.
#
# The Maple code is in lines beginning with \mapleinline{inert}{1d ... or
# \mapleinline{inert}{2... . 
# With the \mapleinline{inert}{2d ...  lines,         
# there is also a LaTeXed math formula immediately following.
#
# Mapleinline mathematical region:
#
/^\\mapleinline\{inert\}\{1/{y=1}          # set y=1 if Maple-form formulas 
/^\\mapleinline\{inert\}\{2/{z=1}	   # set z=1 if prettyprint formulas
#
# The Maple-form formulas are terminated by }{%. 
#
# If the formula is in prettyprint, the Maple form is included, followed by math mode, 
# which must also be echoed. The math mode is Maple generated, and ends with $}.
#
# Examples:
#
# Maple-form formula:
# \begin{mapleinput}
# \mapleinline{active}{1d}{z:=3;z; # An assignment.}{%
# }
# \end{mapleinput}
#
# prettyprint formula:
# \begin{maplelatex}
# \mapleinline{inert}{2d}{z := 3;}{%
# \[
# z := 3
# \]
# %
# }
# \end{maplelatex}
#
# 
# Otherwise, if we are in text but not in a bypassed inline formula, we check for       
# symbols to change.
#
# Bad splits in output:
# If a line ends in \TEXTsymbol{,\TEXTsymbol{\backslash,
# or \symbol{ due to bad line split, turn *on* w flag,
# then suppress the output of \TEXTsymbol, \TEXTsymbol{\backslash, or \symbol.
#
{if(x==1&&y==0&&z==0){sub(/\\TEXTsymbol[{]$/,"");w=1}}
{if(x==1&&y==0&&z==0){sub(/\\TEXTsymbol[{]\\backslash$/,"");w=1}}
{if(x==1&&y==0&&z==0){sub(/\\symbol[{]$/,"");w=1}}
#                  
# replace badly split symbols after \TEXTsymbol{, \TEXTsymbol{\backslash,
# or \symbol{, in line *after* the bad split.
#
{if(x==1&&y==0&&z==0&&w=1){sub(/^\\backslash[}]/,"\\");w=0}}      # replace split \
{if(x==1&&y==0&&z==0&&w=1){sub(/^[}]/,"\\");w=0}}                 # replace split \
{if(x==1&&y==0&&z==0&&w=1){sub(/^<[}]/,"<");w=0}}                 # replace split <
{if(x==1&&y==0&&z==0&&w=1){sub(/^>[}]/,">");w=0}}                 # replace split >
{if(x==1&&y==0&&z==0&&w=1){sub(/^94[}]/,"\^");w=0}}               # replace split ^
{if(x==1&&y==0&&z==0&&w=1){sub(/^126[}]/,"\~");w=0}}              # replace split ~
#
# replace normally split symbols
#
{if(x==1&&y==0&&z==0)gsub(/\\TEXTsymbol[{]\\backslash[}]/,"\\")}   # restore \
{if(x==1&&y==0&&z==0)gsub(/\\[{]/,"\{")}		           # restore {
{if(x==1&&y==0&&z==0)gsub(/\\[}]/,"\}")}		           # restore }
{if(x==1&&y==0&&z==0)gsub(/\\\$/,"\$")}                            # restore $
{if(x==1&&y==0&&z==0)gsub(/\\%/,"\%")}		                   # restore %
{if(x==1&&y==0&&z==0)gsub(/\\#/,"\#")}		                   # restore #
{if(x==1&&y==0&&z==0)gsub(/\\_/,"\_")}		                   # restore _
{if(x==1&&y==0&&z==0)gsub(/\\symbol[{]94[}]/,"\^")}                # restore ^
{if(x==1&&y==0&&z==0)gsub(/\\symbol[{]126[}]/,"\~")}	           # restore ~
{if(x==1&&y==0&&z==0)gsub(/\\TEXTsymbol[{]<[}]/,"<")}	           # restore <
{if(x==1&&y==0&&z==0)gsub(/\\TEXTsymbol[{]>[}]/,">")}	           # restore >
{if(x==1&&y==0&&z==0)gsub(/\\\&/,"\\&")}    	                   # restore &
{print $0}
#
# Decrement y and z as we leave Maple form formulas or prettyprint.
#
{if(match($0,/[}][{]\%/)>0&&y>0){y--}}         # decrement y at end of Maple form formulas
{if(match($0,/[}][{]\%/)>0&&z>0){z--}}         # decrement z at end of prettyprint
# End of script.