#!/bin/sh
#
#  This is a part of ChiTeX.
#  This script is created by 
#    Hung-Yih Chen ()  (yih@math.ncu.edu.tw)
#               (Dept. of Mathematics, National Central Univ.)
#           Last updated  Jan. 20, 2000
# 
#                All rights preserved   
#



gawkloc=`which gawk|grep ":"`
if test "$gawkloc" = ""; then
   gawkloc=`which gawk|grep "no gawk"`
fi
if test "$gawkloc" != ""; then
   gawkbin="awk"
else
   gawkbin="gawk"
fi

#gawkbin="gawk"

searchtexmf="y"
if test "$searchtexmf" = "y"; then
echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
$gawkbin  '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `


fi



export TEXMF

if test ! -s $1.tex
then
 if test ! -s $1; then 
   filename=`find $TEXMF/tex -follow -name $1 -print`
   if test "$filename" = ""; then
      filename=`find $TEXMF/tex/ -follow -name $1.tex -print`
      if test "$filename" = ""; then
         exit
      fi
   fi
 else
   filename=$1
 fi
else 
 filename=$1.tex
fi

outfilename=`echo "$filename"|sed 's#/#c#g'`
outfilename=".tmp00/$outfilename"


  bname=`basename $filename`                    

#  dirname=`
#  ls $filename| \
#  $gawkbin -v bname=$bname '
#  BEGIN{}
#  {basenamepos=index($0,bname)
#   dirname=substr($0,1,basenamepos-1)
#   periopos=index(dirname,".")
#   print dirname
#  }
#  END{}' `                                       

  filea=`echo $bname|cut -d. -f 1` 
#  fileb=`echo $bname |cut -d. -f 2`
#  file=$dirname$filea


stexf=`
$gawkbin -v filename=$filename '
BEGIN{}
{if (index($0,filename) > 0)
  {print "yes"}
 else
  {print ""}
}
END{}' texfile@`


#filebb=`echo $bname |sed -n '/\./p'`


#  if test "$filebb" = ""
#  then
#      if test ! -s $file.tex
#        then null="1"
#      else fileyes="2" 
#      fi
#  else
#      if test $fileb != "tex"
#      then
#          fileyes="3"
#      else
#         fileyes="2"
#      fi
#  fi




#includetemp=`sed -n '/\\include/p' $filename`

includetemp=`egrep "\\include" $filename`


if test "$includetemp" != ""; then
   chitranu $1
fi



rm -f *temp@ 
rm -f maintemp*@ maintmpb@

  echoprompt=`echo -n "."`
  if test "$echoprompt" = "."; then
     echomode="-n ."
  else
     echoprompt=`echo  ".\c"`
     if test "$echoprompt" = "."; then
        echomode=" .\c"
     else 
        echomode="-e .\c"
     fi
  fi

echo $echomode

   if test -s  $filename.tmp@; then 
      mv  $filename.tmp@  maintemp@
   else
      cp $filename maintemp@
   fi


#if test -s filecontents@; then 
#   /bin/rm -f filecontents@
#fi

if test -s "*.tmp@@"; then rm -f *.tmp@@; fi
if test -s "ask@"; then rm -f ask@; fi



# begin to manage \begin{filecontents} ...\end{filecontents}
# 


#if test "$filecontentsfind" = "y"; then
filecontents=`$gawkbin '
BEGIN{endprint="0"}
{
 if (endprint != "2")
 {percent=index($0,"%")
#  inline=substr($0,percent+1,length($0)-percent)
  if (percent > 0)
     {inline=substr($0,1,percent-1)}
  else
     {inline=$0} 
  if (documentclass == 0)
     {documentclass=index(inline,"\134documentclass")}
  if (documentclass > 0)
     {exit}
  filecontents=index(inline,"\134begin{filecontents}")
  endfilecontents=index(inline,"\134end{filecontents}")
  
  rinlinebg=0
  if (filecontents > 0)
     {havefilecontents="1"
      rbrlbra=index(inline,"\134begin{filecontents}{")
      rbrlbrb=index(inline,"\134begin{filecontents} {") 
      if (rbrlbra > 0)
         {rinlinebg=22}
      else
         {if (rbrlbrb > 0)
             {rinlinebg=23}
         }
      if (rinlinebg > 0)
         {rinline=substr(inline,rinlinebg,length(inline))
          rinlinerbr=index(rinline,"}")
          filename=substr(rinline,1,rinlinerbr-1)
          system("rm -f " filename".tmp@@")
          kk=1
          }
     }


  if (filename != "")
     {if (endfilecontents == 0)
         {if (kk > 1)
             {print $0 >> filename".tmp@@"}
          else
             {kk=kk+1}
         }
      else
         {
          filename=""
          }
     }
  if (documentclass > 0)
     {endprint="1";exit}
  if (endprint == "0") 
     {
     }
  else
     {if (endprint == "1") 
        {endprint="2"}
     }
 }  
}
END{
  if (havefilecontents == "1")
#     {print "filecontents" > "filecontents@"}
      {print "yes"}
}' maintemp@  `


if test "$filecontents" = "yes"; then
ls -1 *.tmp@@ > tmpfile@@ 2>/dev/null


if test ! -s tmpfile@@; then 
   exit
fi


# ask overwrite old files
#if test -s filecontents@; then
$gawkbin '
BEGIN{FS=".tmp@@"}
{
 print "message=\42" "1\42" > "ask@"
 print "while test \42$message\42 = \42" "1\42" > "ask@"
 print "do " > "ask@"
 print "if test -s " $1 "; then " > "ask@"
 print "differ=`diff " $1 " " $1".tmp@@`" > "ask@"
 print "if test \42$differ\42 != \42\42; then" > "ask@"
 print "clear " > "ask@"
 print "echo \42 " " \42 " > "ask@"
 print "echo \42    File " $1 " exist \42" > "ask@"
 print "echo \42 \42 " > "ask@"
 print "echo \42    1. Make new file \134\42" $1 "\134\42 and overwrite the old \
one, while \42" > "ask@"
 print "echo \42       the old one \134\42" $1 "\134\42 will be \
backup as \134\42" $1 ".bak\134\42 \42" > "ask@"

 print "echo \42    2. Keep the old one. \42" > "ask@"
 print "echo \42    \42" > "ask@"
 print "echo -n \42    Enter '1' or '2' to choose one of the above: \42" > "ask@"
 print "read inkey" > "ask@"
 print "if test \42$inkey\42 = \42" "1\42; then" > "ask@"
 print "   mv  " $1 " " $1".bak" > "ask@"
 print "   mv -f " $1 ".tmp@@ " $1 > "ask@"
 print "   message=\42" "0\42 " > "ask@"
 print "fi" > "ask@"
 print "if test \42$inkey\42 = \42" "2\42; then" > "ask@"
 print "   message=\42" "0\42 " > "ask@"
 print "fi" > "ask@"
 print "else" > "ask@"
 print "  message=\42" "0\42" > "ask@"
 print "fi" > "ask@"
 print "else" > "ask@"
 print "  mv -f " $1 ".tmp@@ " $1 > "ask@"
 print "  message=\42" "0\42" > "ask@"
 print "fi" > "ask@"
 print "done "  > "ask@"
}
END{} ' tmpfile@@

chmod +x ask@
./ask@

fi
# end for managing  \begin{filecontents} ...\end{filecontents}



if test -s setaddpercent@; then
   chmod +x setaddpercent@
   setaddpercent@   
#   source setaddpercent@   
fi

if test "$chbold" = ""; then
 if test -s chbold@; then
   chbold=`echo "y"`
   export chbold
 fi
fi


#nochprotect="y"


gawk -v nochprotect=$nochprotect '
BEGIN{
oldinline=""
inbracelbrno=0
oldfpos=0
verbatim=0
##################
#   while (getline labeline < "ch@labelfile" >0)
#      {labelper=index(labeline,"%")
#       if (labelper > 0) 
#        {
#         labelat=index(labeline,"bib@")
#         if (labelat > 0)
#         {#++counta
#          counta=substr(labeline,labelat+4,labelper-labelat-4)
#          chlabel[counta]=substr(labeline,labelper+1,length(labeline)-labelper)
#          bitemstrorg[counta]=chlabel[counta]
#          enlabel[counta]=substr(labeline,1,lablelper-1)
#          bitemstrtmp[counta]=counta
#         }
#        }
#      }
#   count=counta
#   bitemcount=count
#   linecount=bitemcount
#   bgbitemcount=bitemcount+1
#   ++bitemcount
#############################
}
{  inline=$0
   subinline=$0
   inlinea=$0; percent=0
   nextrtn=0
   outline=""


#   if (docclass == 0)
#      {docclass=index(inline,"\\documentclass")
#       if (docclass > 0)
#          {print "\\makeatletter%"
#           print "\\let\\ch@verinsline\\relax%"
#           print "\\makeatother%"
#          }
#      }

#   verbatimpos=index(subinline,"\\begin{verbatim")
   percent=index(inline,"%")
   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
       }

   if (percent > 0)
      {inline=substr(subinline,1,percent-1)}

   if (havebatimpos==0)
      {verbatimpos=index(inline,"\\begin{verbatim")}

 if (verbatimpos > 0)
  {havebatimpos=1
     if (havebatimpos==1)
        {endverbatim=index(subinline,"\\end{verbatim")}
     if (endverbatim > 0)
        {havebatimpos=0; verbpos=index(inline,"\\verb")} 

     if (endverbatim > 1 && verbpos > endverbatim)  # should endverbatim
        {                                           # and \verb not on same line
         lline=substr($0,1,endverbatim-1)
         rline=substr($0,endverbatim,length($0)-endverbatim+1)
         rbr=index(rline,"}")
         rrline=substr(rline,rbr+1,length(rline)-rbr)
         inline=lline "%\n" substr(rline,1,rbr) "\n\\addpercent" rrline
         havebatimpos=0
        }
     else
       {inline=$0
        }

   subinline=inline
   verbatimpos=index(subinline,"\\begin{verbatim")


 oldstep="y"       
 if (oldstep=="y")
  {

   if (verbatimpos==0)
#      {outline=$0; inlinea=$0; nextrtn=1}
      {outline=inline; nextrtn=1}

 if (nextrtn == 0)
  {percent=index(inline,"%")
   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
       }

   if (percent<verbatimpos)
#      {outline=$0; nextrtn=1
      {outline=inline; nextrtn=1
#       print $0;next
      }

   if (nextrtn == 0)
   {
# to skip the right side of % not between
#                                   "\begin{verbatim" and "\end{verbatim"
#   subline=$0; line=""; haveverbatim=0
   subline=inline; line=""; haveverbatim=0
   while (subline != "")
   {verb=index(subline,"\\begin{verbatim")
    if (verb==0)
     {line=line subline; break} 
    else
     {haveverbatim=1
      percent=index(subinline,"%")
      if (percent==0)
        {line=line subline; break}
      else
        {verba=index(subline,"\\begin{verbatim*")
         if (verba>0)
            {verb=verba;verbcharno=16}
         else
            {verbcharno=15}

         if (percent<verb)
           {line=line substr(subline,1,percent-1); break}
         else
           {
            rline=substr(subline,verb+verbcharno+1,length(subline)-verbcharno)
            endverb=index(rline,"\\end{verbatim")
            endverba=index(rline,"\\end{verbatim*")
            if (endverba>0)
               {endverbno=15}
            else
               {endverbno=14} 

            if (endverb==0)
#              {line=$0;finish=1;break}
              {line=inline;finish=1;break}

            endverb=endverb+endverno+verb+verbcharno-1
            line=line substr(subline,1,endverb)
            subline= \
         substr(subline,endverb+1,length(subline)-endverb)
            subinline= \
         substr(subinline,endverb+1,length(subinline)-endverb)
           }
        }
     }
#   } 
  } # end of if (oldstep=="y")
 
#  subline=line
#   haveverb=0
#   inlinea=subline

   inlinea=line


#  if (percenta == 1)
  {
   verbatimpos=index(inlinea,"\\begin{verbatim}")
   endverbatimpos=index(inlinea,"\\end{verbatim}")
   verbatimposa=index(inlinea,"\\begin{verbatim*}")
   endverbatimposa=index(inlinea,"\\end{verbatim*}")

#print inlinea
#print "endverbatimpos=" endverbatimpos


   if (verbatimpos==0)
      {verbatimpos=index(inline,"\\begin{verbatim}")}
   if (endverbatimpos==0)
      {endverbatimpos=index(inline,"\\end{verbatim}")
       if (endverbatimpos>0)
          {leftendverbatim=substr(inline,1,endverbatimpos-1)}
      }
   if (verbatimposa==0)
      {verbatimposa=index(inline,"\\begin{verbatim*}")}
   if (endverbatimposa==0)
      {endverbatimposa=index(inline,"\\end{verbatim*}")
       if (endverbatimposa>0)      
          {leftendverbatima=substr(inline,1,endverbatimposa-1)}
      }

#   if (verbatimpos > 0 && endverbatimpos > 0)
   if (endverbatimpos > 0 && leftendverbatim!="")
{gsub("\\\\end{verbatim}","\n\\end{verbatim}\n\\ch@verinsline\n\\addpercent ",inlinea)}

#   if (verbatimposa > 0 && endverbatimposa > 0)
   if (endverbatimposa > 0 && leftendverbatima!="")
{gsub("\\\\end{verbatim\\*}","\n\\end{verbatim*}\n\\ch@verinsline\n\\addpercent ",inlinea)}

  }
   if (percent == 1)
#      {inlinea=$0}
      {inlinea=inline}

   outline=inlinea
#  print inlinea
  } # end of nextrtn=1
 } # end of nextrtn=1
}  # end of if (oldstep=="y")
}
else
 {inline=$0}

   outline=inline

########  End of 1st paragrpagh ########   

#   inline=$0
#   inlineorg=$0


   inline=outline
   inlineorg=outline

   outlinetwo=""
   outlinetwopre=""

   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   subinline=inline
   percent=index(inline,"%")
   perpos=percent
   newtheorem="n"

   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 1)
           {inline=substr(inline,1,percent-1)}
        else
           if (percent == 1)
              {inline=""}
       }


#   if (perfound == 1)
#      {inline=substr(inline,1,perpos-1)}

    if (inline == "")
       {oldfpos=0}


# Find \chboldface,...
 if (chbold == "")
  {chboldface=index(inline,"\\chboldface")
   choutline=index(inline,"\\choutline")
   outline=index(inline,"\\outline")
   if (chboldface !=0 || choutline != 0 || outline != 0)
      {print "yes" > "chbold@";chbold="y"}
  }

# Find \verbatim

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")


  endd=0
  fpos=0

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbatimpos > 0)
    {verbatimmark=verbatimmark+1
     if (firstverb=="")             # For nested verbatimlike cmd
         {firstverb="verbatim"}     #  to set firstverb="..." with ...
     if (firstverb=="verbatim")     #  the first appeared verbatim  
        {bgverbatimmark=1}          #  and set firstverb="" when
    }                               #  end of the first appeared verbatim
 if (endverbatimpos > 0)            # the complicated mechnism arised from 
    {verbatimmark=verbatimmark-1    #  automatic
    if (firstverb=="verbatim")      #   \addpercent  which
         {firstverb=""}             #   must not be in verbatim nested
    }                               #   loop and other is for macro \input
                                    #   in or not in "alltt" environment
 if (verbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark+1
     if (firstverb=="")
         {firstverb="verbatimtab"}
     if (firstverb=="verbatimtab")
        {bgverbatimmark=1}

    }
 if (endverbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark-1
     if (firstverb=="verbatimtab")
         {firstverb=""}
    }

 if (chtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark+1
     if (firstverb=="")
         {firstverb="chtabverbatim"}
     if (firstverb=="chtabverbatim")
        {bgverbatimmark=1}

     }
 if (endchtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark-1
     if (firstverb=="chtabverbatim")
         {firstverb=""}
    }

 if (boxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark+1
     if (firstverb=="")
         {firstverb="boxedverbatim"}
     if (firstverb=="boxedverbatim")
        {bgverbatimmark=1}
     else
        {bgverbatimmark=0}

    }
 if (endboxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark-1
     if (firstverb=="boxedverbatim")
         {firstverb=""}
    }

 if (listingpos > 0)
    {listingmark=listingmark+1
     if (firstverb=="")
         {firstverb="listing"}
     if (firstverb=="listing")
        {bgverbatimmark=1}
     else
        {bgverbatimmark=0}
    }
 if (endlistingpos > 0)
    {listingmark=listingmark-1
     if (firstverb=="listing")
         {firstverb=""}
     else
        {bgverbatimmark=0}
    }

 if (allttpos > 0)
    {allttmark=allttmark+1
     if (firstverb=="")
         {firstverb="alltt"}
     if (firstverb=="alltt")
        {bgverbatimmark=1}
     else
        {bgverbatimmark=0}

    }
 if (endallttpos > 0)
    {allttmark=allttmark-1
     if (firstverb=="alltt")
         {firstverb=""}
    }

# if (verbatimmark+boxedverbatimmark+listingmark+allttmark+verbatimtabmark+chtabverbatimmark==1)
#    {bgverbatimmark=1}
# else
#    {bgverbatimmark=0}

 if (verbatimmark<0||boxedverbatimmark<0||listingmark<0||\
       allttmark<0||verbatimtabmark<0||chtabverbatimmark<0)
    {edverbatimmark=-1}
 else
    {edverbatimmark=1}

# if (firstverb!="")
#    {bgverbatimmark=1}


 if (verbpos>0||verbatimpos > 0||boxedverbatimpos > 0||listingpos > 0 \
     || verbatimwritepos > 0||verbatimtabpos > 0|| chtabverbatimpos >0)
    {verbatim=1; verbatimnocmd=1}
 if (endverbatimpos > 0||endboxedverbatimpos > 0||endlistingpos > 0 \
     ||endverbatimwritepos > 0||endverbatimtabpos > 0||endchtabverbatimpos >0)
    {if (firstverb=="")
        {verbatim=0;verbatimnocmd=0}
    }

 if (allttpos > 0|| verbatimcmdpos > 0)
    {verbatim=1; verbatimnocmd=0}
 if (endallttpos > 0|| endverbatimcmdpos > 0)
    {verbatim=0;verbatimnocmd=0}

 if (verbatimnocmd == 1)
    {gsub("\\input","\einput",inlineorg)
    }

 if (verbatim == 0 && firstverb=="")
# if (verbatim == 0 && edverbatimmark==1)
# if (verbatim == 0)
 {
  if (doclss == 0)
    {doclss=index(inline, "documentclass")
     amsbook=index(inline,"{amsbook}")}
  
  if (baselineskiploc == 0)
     {baselineskiploc=index(inline,"\\baselineskip")
      if (baselineskiploc > 0)
         {outlinetwopre="\\def\\ch@baselineskip{n}\n"}
#          print "\\def\\ch@baselineskip{n}"
         }
      else
         {outlinetwopre=""
     }
  if (index(inline,"\\newtheorem") > 0)
     {newtheorem="y"
      print inline > "newtheorem@"
     }

  if (newtheorem == "y" && (doclss > 0 && amsbook > 0))
           {outlinetwo=outlinetwopre "%" inlineorg
#            print "%" inlineorg
            }

 if (nochprotect == "")
  {
  j=1
  for (j; j<=2; j++)
  {
   chappos=index(inline,"\\chapter{"); chapposb=index(inline,"\\chapter {")
   secpos=index(inline,"\\section{"); secposb=index(inline,"\\section {")

   subsecpos=index(inline,"\\subsection{");subsecposb= \
      index(inline,"\\subsection {")
   subsubsecpos=index(inline,"\\subsubsection{");subsubsecposb= \
         index(inline,"\\subsubsection {")

   paragraphpos=index(inline,"\\paragraph{")
   paragraphposb=index(inline,"\\paragraph {")
   subparagraphpos=index(inline,"\\subparagraph{")
   subparagraphposb=index(inline,"\\subparagraph {")

   fpos=0
   if (chappos > 0)
     {lbr[1]=chappos+8; fpos=1}
   else
     {if (chapposb > 0)
        {lbr[1]=chappos+9; fpos=1}
      else
        {if (secpos > 0)
          {lbr[1]=secpos+8; fpos=2}
         else
          {if (secposb > 0)
            {lbr[1]=secpos+9; fpos=2}
           else
             {if (subsecpos > 0)
               {lbr[1]=subsecpos+11; fpos=3}
              else
              {if (subsecposb > 0)
                {lbr[1]=subsecposb+12; fpos=3}
               else
                {if (subsubsecpos > 0)
                  {lbr[1]=subsubsecpos+14; fpos=4}
                 else
                   {if (subsubsecposb > 0)
                     {lbr[1]=subsubsecposb+15; fpos=4}
                    else                                                 #
                     {if (paragraphpos > 0)                              #
                         {lbr[1]=paragraphpos+10; fpos=5}                #
                      else                                               #
                         {if (paragraphposb > 0)                         #
                             {lbr[1]=paragraphposb+11; fpos=5}           #
                          else                                           #
                             {if (subparagraphpos > 0)                   #
                                {lbr[1]=subparagraphpos+13; fpos=6}      #
                              else                                       #
                                {if (subparagraphposb > 0)               #
                                    {lbr[1]=subparagraphposb+14; fpos=6} #
                                }  #11                                   #
                             }   #10                                     #
                         }   #9                                          #
                     }   #8                                              #
                   }
                }
              }
             }
          }
        }
     }

   
     if (j == 1)
      {if (oldinline !="")
        {if ((oldfpos != fpos && fpos !=0) || inline == "")
            {outlinetwo=outlinetwopre oldinline; break
#             print oldinline; break
             }
         else
            {inline=oldinline inline}
        }

      }



  }

    oldinline=""
    lbrpos=lbr[1]; lbrno=1

    rbrno=0
    if (fpos > 0)
       {linline=substr(inline,1,lbr[1])
        rinline=substr(inline,lbr[1]+1,length(inline)-lbr[1])

        if (rinline !="")   # if rinline="" let it unchanged
        {
         sec=0
         rbrexist=0
         lenn=length(rinline)
         i=1

         for (i; i<=lenn; i++)
             {ch[i]=substr(rinline,i,1)}

         rbrexist=0; rbrno=0 
         i=1
         for (i; i<=lenn; i++) 
         {if (ch[i] > "\240" && ch[i] < "\372")
              {sec=1
               i=i+1
              }             
          else
              {sec=0
               if (ch[i] == "}" && ch[i-1] != "\\")
                  {
                   rbrexist=1
                   rbrpos=i
                   rbrno=1
                   break
                  }
             }
         }
         

         if (rbrexist == 1)    # if exist rbr find lbrno
          {lblock=substr(rinline,1,rbrpos-1)
           if (lenn == rbrpos)
             {rblock="";endd=1}
           else
             {rblock=substr(rinline,rbrpos+1,lenn-rbrpos)}


           if (lblock != "")   # if lblock !=""
           {lenlblock=length(lblock)
            i=1
            for (i; i<=lenlblock; i++) 
            {ch[i]=substr(lblock,i,1)
             if (ch[i] > "\240" && ch[i] < "\372")
                {i=i+1}             
             else
                {if (ch[i] == "{" && ch[i-1] != "\\")
                    {++lbrno}
                }
            }
           }
           else
           {endd=1}             #if lblock="" lbrno=1 never increased


           if (lbrno > rbrno)        
              {   #find rbr "}" in rblock if rblock !=""
               lastrbrpos=0
               if (rblock != "")
                {lenn=length(rblock)
                 i=1
                 for (i; i<=lenn; i++)
                   {ch[i]=substr(rblock,i,1)}

                 bgrbrpos=1; i=1
                 for (i; i<=lenn; i++) 
                 {lbrnewfound=1
                   {if (ch[i] > "\240" && ch[i] < "\372")
                     {i++}             
                    else
                     {#secc=0;
                      newblock=""; lbrnewfound=1
                      if (ch[i] == "}" && ch[i-1] != "\\")
                         {newblock=substr(rblock,bgrbrpos,i-1)
                          lennew=length(newblock)
                          lbrnewfound=0; bgrbrpos=i
                          j=1
                          for (j; j<=lennew; j++)
                          {
                           chnew[j]=substr(newblock,j,1)
                            {if (chnew[j] > "\240" && chnew[j] < "\372")
                                {j++}
                             else
                                {if (chnew[j] == "{" && chnew[j-1] != "\\")
                                  {lbrnewfound=1; break}
                                }
                            }
                          }
                          if (lbrnewfound == 0)
                             {++rbrno;
                              if (lbrno == rbrno)
                                 {lastrbrpos=i;break}
                             }
                         }
                     }
                   }
                 }




                }
              }        #if resulted lbrno=rbrno lastrbrpos>0
           else                     #where lbrno=rbrno=1
              {}

           if (lbrno <= rbrno)
              {if (rblock != "")
                {lenn=length(rblock)
                 righttorbr=substr(rblock,lastrbrpos,lenn-lastrbrpos+1)
                  inbrace=substr(lblock,1,rbrpos-1) "}" 
                 inbrace=inbrace substr(rblock,1,lastrbrpos-1)
                }
               else   #where rblock="" must lbrno=rbrno=1
                {inbrace=substr(lblock,1,rbrpos-1) "}"; righttorbr=""}

               len=length(inbrace)
               inbracestr=""
               i=1
               for (i; i<=len; i++)
                   {ch[i]=substr(inbrace,i,1)}
               i=1
               for (i; i<=len; i++)
               {if (ch[i] > "\240" && ch[i] < "\372")
                   {inbracestr=inbracestr ch[i] ch[i+1]; i++}
                else
                   {if (ch[i] == "\\")
                       {inbracestr=inbracestr "\\protect" ch[i]} 
#                       {inbracestr=inbracestr ch[i]} 
                    else 
                       {inbracestr=inbracestr ch[i]}
                   }
               }

               inline=linline inbracestr righttorbr
               endd=1
              }
           else
              {if (percent == 0)
                  {oldinline=inline " "}
               else 
                  {oldinline=inline}
               endd=1
              }
          }
         else
          {
           {if (percent == 0)         #where rbr not exist, lbrno=1 rbrno=0
               {oldinline=inline " "} #move inline to oldinline, to
            else                      #append to inline of next record
               {oldinline=inline}
            endd=1                    
           }                            
          }

        }
       else                       #where rinline=""
        {
         {if (percent == 0)
             {oldinline=inline " "}
          else 
             {oldinline=inline}
          endd=1
         }
        }
       }
    else
       {endd=1}   # where fpos=0
   }
  }
    if (fpos == 0)
       {if (newtheorem == "y" && (doclss > 0 && amsbook > 0))
##           {print "%" inlineorg}
           {  }
        else
           {outlinetwo=outlinetwopre inlineorg
#            print inlineorg
           }
        }
    else
       {if (oldinline == "")
           {outlinetwo=outlinetwopre inline
#            print inline
           }
       }

# print outlinetwo
#  }
#END{}  maintemp@ 
####### End of 2nd paragrapgh ###########


   inline=outlinetwo
   inlineorg=outlinetwo
   outline=""
   outlinepre=""
   outlineb=""


#if (nochprotect == "y")
#   {outline=outlinetwo
##    print outlinetwo; next
#    }


#   inline=$0
#   inlineorg=$0

   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {inline=substr(inline,1,percent-1);perfound=1}
       }



   if (perfound == 1)
      {#inline=substr(inline,1,perpos-1)
       perstr="%"}
   else
      {perstr=""}

#   verbpos=index(inline,"\\verb")  
#   verbatimpos=index(inline,"\\begin{verbatim")
#   endverbatimpos=index(inline,"\\end{verbatim")
#   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
#   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
#   listingpos=index(inline,"\\begin{listing")
#   endlistingpos=index(inline,"\\end{listing")
#   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
#   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
#   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
#   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
#   chtabverbatimpos=index(inline,"\\chtabverbatim")
#   endchtabverbatimpos=index(inline,"\\endchtabverbatim")
#
#   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
#   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")
#
#   allttpos=index(inline,"\\begin{alltt}")
#   endallttpos=index(inline,"\\end{alltt}")
#  
#   chprotect=index(inline,"\\chprotect")
#   unchprotect=index(inline,"\\unchprotect")

# if (chprotect > 0)
#    {verbatim=0}
# if (unchprotect > 0)
#    {verbatim=1}


 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0\
     ||verbatimtabpos>0||boxedverbatimpos>0||listingpos>0 \
     ||verbatimwritepos>0||chtabverbatimpos>0)
    {verbatim=1;bgverbatim=1;endverbatim=0}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
   {#if (firstverb=="")
      {verbatim=0;bgverbatim=0;endverbatim=1}
   }

#print "inline=" inline
#print "fisrtverb=" fisrtverb

# if (bgverbatim == 1 && verbpos==0 && fisrtverb=="")
# if (bgverbatim == 1 && verbpos==0)
 if (bgverbatimmark == 1 && bgverbatim == 1 && verbpos==0)
    {outlinepre="\\unaddpercent"
#     printf "\\unaddpercent"
#print "inline=" inline
#print "fisrtverb=" fisrtverb

     }
 else
    {outlinepre=""}

 if (bgverbatim == 1)
    {bgverbatim=0}
    
#print "firstverb=" firstverb
#print "verbatim=" verbatim
#getline < "-"


 if (verbatim == 0 && firstverb=="")
# if (verbatim == 0 && edverbatimmark==1)
# if (verbatim == 0)
    {
     citepos=index(inline,"\\cite")
     if (citepos == 0)
#      {if (verbmatch == 0 && edverbatimmark==0 && endverbatim == 1)
      {if (endverbatim == 1)
#      {if (edverbatimmark == 1)
        {#printf inline perstr
#         print inline perstr
#         print "\\addpercent"; endverbatim=0
#         }
         outline=outlinepre inline perstr "\n"
         outline=outline "\\addpercent"
         endverbatim=0
        }
       else
        {
         if (verbatimmark<0)
            {verbatimmark=0}
         if (boxedverbatimmark<0)
            {boxedverbatimmark=0}
         if (listingmark<0)
            {listingmark=0}
         if (allttmark<0)
            {allttmark=0}
         if (verbatimtabmark<0)
            {verbatimtabmark=0}
         if (chtabverbatimmark<0)
            {chtabverbatimmark=0}

#          outline=outlinepre inline perstr  ?????????
          outline=inline perstr
#         print inline perstr
        }
      }
     else
      {morecite=1
       rinline=inline
       firstcite=1
       while (morecite == 1)
       {
        linline=substr(rinline,1,citepos+4)
        rinline=substr(rinline,citepos+5,length(rinline)-citepos-4)
        citepos=index(rinline,"\\cite")
        if (citepos > 0)
         {lrinline=substr(rinline,1,citepos-1)
          if (firstcite == 1)
             {
#              outline=outlinepre linline lrinline "%" 
              outline=linline lrinline "%"
#              print linline lrinline "%" 
              firstcite=0
              }
          else
             {
#              outline=outlinepre "\\cite" lrinline "%"
              outlineb="\\cite" lrinline "%"
#              outline=outline "\n\\cite" lrinline "%"        # ??????
#              print "\\cite" lrinline "%"
             }
         }
        else
         {morecite=0
          if (firstcite == 1)
             {outline=outlinepre linline rinline perstr
#              print linline rinline perstr
             }
##?             {print linline rinline}
          else
             {#outline=outlinepre "\\cite" rinline perstr
              outlineb=outline "\\cite" rinline perstr
#             {outline=outline "\n\\cite" rinline perstr
#              print "\\cite" rinline perstr
             }
##?             {print "\\cite" rinline}
         }
       }
      }
    }
 else
    {
#     verbatimmark=0;boxedverbatimmark=0;listingmark=0
#     allttmark=0;verbatimtabmark=0;chtabverbatimmark=0

     outline=outlinepre inlineorg

#     print inlineorg
    }
#}
#END{}  maintemp@ | \

###### End of 3rd paragraph ############

#$gawkbin -v nochprotect=$nochprotect 
#BEGIN{
#}
#{
#if (nochprotect == "y")
#   {print $0; next}

#   inline=$0
#   inlineorg=$0

#   inline=outline
#   inlineorg=outline


jjj=1
for (jjj;jjj<=2;jjj++)  
{if (jjj == 1)
    {inline=outline
     inlineorg=outline
    }
   else
    {if (outlineb != "")
       {inline=outlineb
        inlineorg=outlineb
       }
     else
       {break}
    }
     
   outline=""
   outlinepre=""
   outlineb=""   

   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
            #inline=substr(inline,1,percent-1);perfound=1 
           }
       }


   if (perfound == 1)
      {
#       if (perpos != length(inline))
       if (percent != length(inline))
          {inline=substr(inline,1,perpos-1)}
      }

#   verbpos=index(inline,"\\verb")  
#   verbatimpos=index(inline,"\\begin{verbatim")
#   endverbatimpos=index(inline,"\\end{verbatim")
#   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
#   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
#   listingpos=index(inline,"\\begin{listing")
#   endlistingpos=index(inline,"\\end{listing")
#   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
#   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
#   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
#   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
#   chtabverbatimpos=index(inline,"\\chtabverbatim")
#   endchtabverbatimpos=index(inline,"\\endchtabverbatim")
#
#   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
#   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")
#
#   allttpos=index(inline,"\\begin{alltt}")
#   endallttpos=index(inline,"\\end{alltt}")
#
#   chprotect=index(inline,"\\chprotect")
#   unchprotect=index(inline,"\\unchprotect")

# if (chprotect > 0)
#    {verbatim=0}
# if (unchprotect > 0)
#    {verbatim=1}

 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0\
     ||verbatimtabpos > 0 ||boxedverbatimpos > 0||listingpos > 0\
     || verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}

 if (verbatim == 0 && firstverb=="")
    {
     citepos=index(inline,"\\nocite")
     if (citepos == 0)
      {#outline=inline
       print inline
      }
     else
      {
       morecite=1
       rinline=inline
       firstcite=1
       while (morecite == 1)
       {
        linline=substr(rinline,1,citepos+6)
        rinline=substr(rinline,citepos+7,length(rinline)-citepos-6)
        citepos=index(rinline,"\\nocite")
        if (citepos > 0)
         {lrinline=substr(rinline,1,citepos-1)
          if (firstcite == 1)
             {#outline=linline lrinline "%"
              print linline lrinline "%"
              firstcite=0
              }
          else
             {#outlineb="\\nocite" lrinline "%"
#              outline="\\nocite" lrinline "%"
              print "\\nocite" lrinline "%"
             }
         }
        else
         {morecite=0
          if (firstcite == 1)
             {#outline=linline rinline
              print linline rinline
             }
          else
             {#outlineb="\\nocite" rinline
#              outline="\\nocite" rinline
              print "\\nocite" rinline
             }
         }
       }
      }
    }
 else
    {#outline=inlineorg
     print inlineorg
    }
#print outline
}
###### End of 4th paragraph ############
}
END{} ' maintemp@ | \
gawk '
BEGIN{
   while (getline labeline < "ch@labelfile" >0)
      {labelper=index(labeline,"%")
       if (labelper > 0) 
        {
         labelat=index(labeline,"bib@")
         if (labelat > 0)
         {#++counta
          counta=substr(labeline,labelat+4,labelper-labelat-4)
          chlabel[counta]=substr(labeline,labelper+1,length(labeline)-labelper)
          bitemstrorg[counta]=chlabel[counta]
          enlabel[counta]=substr(labeline,1,lablelper-1)
          bitemstrtmp[counta]=counta
         }
        }
      }
   count=counta
   bitemcount=count
   linecount=bitemcount
   bgbitemcount=bitemcount+1
   ++bitemcount
}
{

#if (nochprotect == "y")
#   {print $0; next}

   inline=$0
   inlineorg=$0

#   inline=outline
#   inlineorg=outline

   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
#            inline=substr(inline,1,percent-1);perfound=1
           }
       }


   if (perfound == 1)
      {if (percent != length(inline))
         {inline=substr(inline,1,perpos-1)}
      }

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbatimpos > 0)
    {verbatimmark=verbatimmark+1}
 if (endverbatimpos > 0)
    {verbatimmark=verbatimmark-1}

 if (verbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark+1}
 if (endverbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark-1}

 if (chtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark+1}
 if (endchtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark-1}

 if (boxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark+1}
 if (endboxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark-1}

 if (listingpos > 0)
    {listingmark=listingmark+1}
 if (endlistingpos > 0)
    {listingmark=listingmark-1}

 if (allttpos > 0)
    {allttmark=allttmark+1}
 if (endallttpos > 0)
    {allttmark=allttmark-1}

 if (verbatimmark+boxedverbatimmark+listingmark+allttmark+verbatimtabmark+chtabverbatimmark==1)
    {bgverbatimmark=1}
 else
    {bgverbatimmark=0}

 if (verbatimmark<0||boxedverbatimmark<0||listingmark<0||\
       allttmark<0||verbatimtabmark<0||chtabverbatimmark<0)
    {edverbatimmark=-1}
 else
    {edverbatimmark=1}


 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}

 if (verbatim == 0 && edverbatimmark==1)
# if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  bitempos=1
  while (inl != "" && bitempos != 0)
  {
   len=length(inl)
   bitemposa=0
   bitemposb=0
   bitempos=0
   bitemrbpos=0

   bitemposa=index(inl,"\\bibitem{") 
   bitemposb=index(inl,"\\bibitem {")
   bitemposc=index(inl,"\\bibitem[") 

   if (bitemposc > 0)
      {#ltbitem=substr(inl,1,bitemposc-1)
       fsubbitem=substr(inl,bitemposc+9,length(inl)-bitemposc-8)
       roptlbr=index(fsubbitem,"]{")
       if (roptlbr > 0)
          {
           bitemposc=bitemposc+8+roptlbr+1
          }
       else
          {bitemposc=0}
      }

   if (bitemposc > 0)
      {bitempos=bitemposc                                                                           
       bitembgpos=bitemposc+1
      }       
   else
      {if (bitemposa > 0 )                                                       
        {bitempos=bitemposa                                                                           
         bitembgpos=bitemposa+9
        }                                                                      
       else                                                               
        {if (bitemposb > 0 )                                               
         {bitempos=bitemposb
          bitembgpos=bitemposb+10
         }
        }
      }


   if (bitempos > 0)                                                                                
      {
       subbitem=substr(inl,bitembgpos,len-bitembgpos+1)                                          
       ltbitem=substr(inl,1,bitembgpos-1)
       bitemrbpos=0
       subbitemstr=subbitem
       rbrexist=0

          while (subbitemstr !="" && rbrexist == 0)
           {
            bitemrbtmp=index(subbitemstr,"}")
            if (bitemrbtmp > 0)
              {
               prerbr=substr(subbitemstr,1,bitemrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;bitemrbpos=bitemrbpos+bitemrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;bitemrbpos=bitemrbpos+bitemrbtmp}
                 else
                    {subbitemstr= \
             substr(subbitemstr,bitemrbtmp+1,length(subbitemstr)-bitemrbtmp)
                     bitemrbpos=bitemrbpos+bitemrbtmp
                    }
              }
            else
              {
               rbrexist=0; subbitemstr=""
              }              
           }
          

       if (bitemrbpos > 0)
          {linecount=linecount + "1"
           bitemstr=substr(subbitem,1,bitemrbpos-1)                                                 
           lenn=length(bitemstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(bitemstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               bitemcount=bitemcount + 1 
               i=1; havebitemstr=0
               if (count == 0)
                  {havebitemstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (bitemstrorg[i] == bitemstr)
                           {havebitemstr=1;matchcount=i;break}
                       }
                  } 
               if (havebitemstr == 0)
                  {count=count+1
                   matchcount=count
                   bitemstrorg[count]=bitemstr
                   lencount=length(linecount)
                   bitemstrtmp[count]=bitemcount
                  }

               inlineextra[count]="ch@bib@" bitemstrtmp[matchcount] "%" bitemstr
               inlinenew=inlinenew ltbitem "ch@bib@" \
                  bitemstrtmp[matchcount] "}"
               inlineupdate=1
             }
            else
             {inlinenew=inlinenew ltbitem "\\bibitem{" bitemstr "}"}  # update
            
           rhbitem=substr(subbitem,bitemrbpos+1,length(subbitem)-bitemrbpos)                        #               
           inl=rhbitem
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhbitem}
  else
     {inl=inline}

 k=1
 for (k; k<=2; k++)
 {if (k == 2)
     {inl=inline}
  inlinenewb=""
  citepos=1
  while (inl != "" && citepos != 0)
  {len=length(inl)
   citeposa=0
   citeposb=0
   citeposc=0
   citepos=0
   citerbpos=0
   
   if (k == 1)
    {citeposa=index(inl,"\\cite{")                                                                   #
     citeposb=index(inl,"\\cite {")                                                                  #
     citeposc=index(inl,"\\cite[")
     addno=0
    }
   else
    {citeposa=index(inl,"\\nocite{")                                                                   #
     citeposb=index(inl,"\\nocite {")
     addno=2
    }
    

   if (citeposc > 0)
      {
       fsubcite=substr(inl,citeposc+6,length(inl)-citeposc-5)
       roptlbr=index(fsubcite,"]{")
       if (roptlbr > 0)
          {
           citeposc=citeposc+5+roptlbr+1
          }
       else
          {citeposc=0}
      }

   if (citeposc > 0)
      {citepos=citeposc                                                                           
       citebgpos=citeposc+1
      }       
   else
      {
       if (citeposa > 0 )                                                                              #
          {citepos=citeposa                                                                           #
           citebgpos=citeposa+6+addno
          }                                                                      #
       else                                                                                             #
          {if (citeposb > 0 )                                                                          #
           {citepos=citeposb
            citebgpos=citeposb+7+addno}                                                                     #
          }                                                                                             #
      }
 
   if (citepos > 0)                                                                                #
      {if (citepos > 1)                                                                            #
          {ltcite=substr(inl,1,citepos-1)}                                                     #
       else                                                                                         #
          {ltcite=""}                                                                              #

       subcite=substr(inl,citebgpos,len-citebgpos+1)                                          #
       ltcite=substr(inl,1,citebgpos-1)

       citerbpos=0
       subcitestr=subcite
       rbrexist=0

          while (subcitestr !="" && rbrexist == 0)
           {
            citerbtmp=index(subcitestr,"}")
            if (citerbtmp > 0)
              {
               prerbr=substr(subcitestr,1,citerbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;citerbpos=citerbpos+citerbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;citerbpos=citerbpos+citerbtmp}
                 else
                    {subcitestr= \
             substr(subcitestr,citerbtmp+1,length(subcitestr)-citerbtmp)
                     citerbpos=citerbpos+citerbtmp
                    }
              }
            else
              {
               rbrexist=0; subcitestr=""
              }              
           }
          
          
       if (citerbpos > 0)                                                                          #
          {citestr=substr(subcite,1,citerbpos-1)                                                 #
           rhcite=substr(subcite,citerbpos+1,length(subcite)-citerbpos)
           comma=1; subbcitestr=citestr
           jj=0
           while (subbcitestr != "" && comma != 0)
           {comma=index(subbcitestr,",")
            ++jj
            if (comma == 0)
               {citecomp[jj]=subbcitestr}
            else
               {citecomp[jj]=substr(subbcitestr,1,comma-1)
                subbcitestr=\
                  substr(subbcitestr,comma+1,length(subbcitestr)-comma)
               }
           }

           j=1
           for (j; j<=jj; j++)
           {i=1
            for (i; i<=count; i++)
               {chinesematch=0
                if (citecomp[j] == bitemstrorg[i])
                   {
                    citestrorg=bitemstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (j == 1)
              {commastr=""}
           else
              {commastr=","}
           
           if (chinesematch == 1)
              {#inlinenewb=inlinenewb ltcite "\\cite{ch@bib@" citestrorg "}"
               #inlinenewb=inlinenewb ltcite "ch@bib@" citestrorg "}"
               inlinenewb=inlinenewb commastr "ch@bib@" citestrorg
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(citecomp[j])
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(citecomp[j],i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                bitemcount=bitemcount+1
                lencount=length(linecount)
                bitemstrtmp[count]=bitemcount
                bitemstrorg[count]=citecomp[j]
                inlineextra[count]="ch@bib@" bitemcount "%" citecomp[j]
                inlinenewb=inlinenewb commastr "ch@bib@" bitemcount

                inlineupdate=3+addno
               }
              else
               {inlinenewb=inlinenewb commastr citecomp[j]}
#               {inlinenewb=inlinenewb ltcite "\cite{" citecomp[j] "}"}
              }
            inl=rhcite
            if (j == jj)
               {inlinenewb=ltcite inlinenewb "}"}
           } #new for (j; j<=jj; j++) 
         }                                                                                        #
      }                                                                                             #
  }

  if (k == 1)
  {if (inlineupdate == 1)
      {inline=inlinenew rhbitem}
   else
      {if (inlineupdate == 3)
          {inline=inlinenewb rhcite}
      }
  }
  else 
      {if (inlineupdate == 5 )
          {inline=inlinenewb rhcite}
      }
 }  # End of for (k; k<=2; k++)

################################################

       if (inlineupdate == 0)
          {print inlineorg}
       else
          {print inline}
 }
 else
  {print inlineorg}
}
END{  
      i=bgbitemcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@labelfile"
      }
#       print  bitemcount > "ch@exportb"
###### End of 5th paragraph ############
}' | \
gawk '
BEGIN{
#  if (nochprotect == "y")
#     {print "yes" > nochprotect}

   while (getline labeline < "ch@labelfile" >0)
      {labelper=index(labeline,"%")
       if (labelper > 0) 
        {
         labelat=index(labeline,"lb@")
         if (labelat > 0)
         {#++countb
          countb=substr(labeline,labelat+3,labelper-labelat-3)
          chlabel[countb]=substr(labeline,labelper+1,length(labeline)-labelper)
          labelstrorg[countb]=chlabel[countb]
          enlabel[countb]=substr(labeline,1,lablelper-1)
          labelstrtmp[countb]=countb
         }
        }
      }
   count=countb
   labelcount=count
   linecount=labelcount
   bglabelcount=labelcount+1
   ++labelcount
   fnum=0
   sww=0
############
#print "##??!!firstline"
if (chitranno == 0)
   {
    addperc=1; beginpercprev=1}
latin=0
outlinelast=""
inlineold=""
endtab=1; endverb=1
##############

}
{
#if (nochprotect == "y")
#   {#print "yes" > "nochprotect"
#    print $0; next}

   if (NR == 1)  
      {inline="##??!!firstline\n" $0}
   else
      {inline=$0}

   outline=""
   outlinepre=""  

   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
#            inline=substr(inline,1,percent-1);perfound=1
           }
       }


   if (percenttmp > 0)
       {
        if (substr(subinline,percent-1,1) == "\\")
           {per=1; subinline=substr(inline,percent+1,length(inline)-percent)
            perfound=0
            while (per !=0 && subinline !="" && perfound ==0)  
            {per=index(subinline,"%")
             if (per > 0)
                { perpos=perpos+per
                  if (substr(subinline,percent-1,1) == "\\")
                     {subinline=substr(subinline,per+1,length(subinline)-per)
                      percent=per
                     }
                  else
                     {perfound=1; break}
                }
             else
                {perfound=0}
            }
           }
        else
           {perfound=1}
       }


   if (perfound == 1)
#      {if (perpos != length(inline))
      {if (percent != length(inline))
         {inline=substr(inline,1,perpos-1)}}

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbatimpos > 0)
    {verbatimmark=verbatimmark+1}
 if (endverbatimpos > 0)
    {verbatimmark=verbatimmark-1}

 if (verbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark+1}
 if (endverbatimtabpos > 0)
    {verbatimtabmark=verbatimtabmark-1}

 if (chtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark+1}
 if (endchtabverbatimpos > 0)
    {chtabverbatimmark=chtabverbatimmark-1}

 if (boxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark+1}
 if (endboxedverbatimpos > 0)
    {boxedverbatimmark=boxedverbatimmark-1}

 if (listingpos > 0)
    {listingmark=listingmark+1}
 if (endlistingpos > 0)
    {listingmark=listingmark-1}

 if (allttpos > 0)
    {allttmark=allttmark+1}
 if (endallttpos > 0)
    {allttmark=allttmark-1}

 if (verbatimmark+boxedverbatimmark+listingmark+allttmark+verbatimtabmark+chtabverbatimmark==1)
    {bgverbatimmark=1}
 else
    {bgverbatimmark=0}

 if (verbatimmark<0||boxedverbatimmark<0||listingmark<0||\
       allttmark<0||verbatimtabmark<0||chtabverbatimmark<0)
    {edverbatimmark=-1}
 else
    {edverbatimmark=1}


 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}

 if (verbatim == 0 && edverbatimmark==1)
# if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  labelpos=1
  while (inl != "" && labelpos != 0)
  {
   len=length(inl)
   labelposa=0
   labelposb=0
   labelpos=0
   labelrbpos=0

   labelposa=index(inl,"\\label{")                                                               
   labelposb=index(inl,"\\label {")                                                              
   
   if (labelposa > 0 )                                                       
      {labelpos=labelposa                                                                           
       labelbgpos=labelposa+7
      }                                                                      
   else                                                               
      {if (labelposb > 0 )                                               
       {labelpos=labelposb
        labelbgpos=labelposb+8}                                                                     
      }                                                                                             

   if (labelpos > 0)                                                                                
      {
       if (labelpos > 1)                                                                            
          {ltlabel=substr(inl,1,labelpos-1)}                                                     
       else                                                                                         
          {ltlabel=""}                                                                              
       sublabel=substr(inl,labelbgpos,len-labelbgpos+1)                                          
       ltlabel=substr(inl,1,labelpos-1)
#       labelrbpos=index(sublabel,"}")
       labelrbpos=0
       sublabelstr=sublabel
       rbrexist=0

          while (sublabelstr !="" && rbrexist == 0)
           {
            labelrbtmp=index(sublabelstr,"}")
            if (labelrbtmp > 0)
              {
               prerbr=substr(sublabelstr,1,labelrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;labelrbpos=labelrbpos+labelrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;labelrbpos=labelrbpos+labelrbtmp}
                 else
                    {sublabelstr= \
             substr(sublabelstr,labelrbtmp+1,length(sublabelstr)-labelrbtmp)
                     labelrbpos=labelrbpos+labelrbtmp
                    }
              }
            else
              {
               rbrexist=0; sublabelstr=""
              }              
           }
          

       if (labelrbpos > 0)
          {linecount=linecount + "1"
           labelstr=substr(sublabel,1,labelrbpos-1)                                                 
           lenn=length(labelstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(labelstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               labelcount=labelcount + 1 
               i=1; havelabelstr=0
               if (count == 0)
                  {havelabelstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (labelstrorg[i] == labelstr)
                           {havelabelstr=1;matchcount=i;break}
                       }
                  } 
               if (havelabelstr == 0)
                  {count=count+1
                   matchcount=count
                   labelstrorg[count]=labelstr
                   lencount=length(linecount)
                   labelstrtmp[count]=labelcount
                  }

               inlineextra[count]="ch@lb@" labelstrtmp[matchcount] "%" labelstr
#               inlinenew=inlinenew ltlabel "\\protect\\label{ch@lb@" \
               inlinenew=inlinenew ltlabel "\\label{ch@lb@" \
                  labelstrtmp[matchcount] "}"

               inlineupdate=1
             }
            else
             {inlinenew=inlinenew ltlabel "\\label{" labelstr "}"}
           rhlabel=substr(sublabel,labelrbpos+1,length(sublabel)-labelrbpos)                        #               
           inl=rhlabel
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhlabel}

 k=1
 for (k; k<=2; k++)
 {if (k ==2)
     {inl=inline}
  inlinenewb=""
  refpos=1
  while (inl != "" && refpos != 0)
  {len=length(inl)
   refposa=0
   refposb=0
   refpos=0
   refrbpos=0

   if (k == 1)
    {refposa=index(inl,"\\ref{");preaddstr="\\ref{"
     refposb=index(inl,"\\ref {");addno=0}
   else
    {refposa=index(inl,"\\pageref{");preaddstr="\\pageref{"                                                     #
     refposb=index(inl,"\\pageref {");addno=4}

   if (refposa > 0 )                                                                              #
      {refpos=refposa
       refbgpos=refposa+5+addno
      }                                                                      #
   else                                                                                             #
      {if (refposb > 0 )                                                                          #
       {refpos=refposb
        refbgpos=refposb+6+addno}
      }                                                                                             #


   if (refpos > 0)                                                                                #
      {if (refpos > 1)                                                                            #
          {ltref=substr(inl,1,refpos-1)}                                                     #
       else                                                                                         #
          {ltref=""}                                                                              #

       subref=substr(inl,refbgpos,len-refbgpos+1)                                          #
       ltref=substr(inl,1,refpos-1)                                                          #

       refrbpos=0
       subrefstr=subref
       rbrexist=0

          while (subrefstr !="" && rbrexist == 0)
           {
            refrbtmp=index(subrefstr,"}")
            if (refrbtmp > 0)
              {
               prerbr=substr(subrefstr,1,refrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;refrbpos=refrbpos+refrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;refrbpos=refrbpos+refrbtmp}
                 else
                    {subrefstr= \
             substr(subrefstr,refrbtmp+1,length(subrefstr)-refrbtmp)
                     refrbpos=refrbpos+refrbtmp
                    }
              }
            else
              {
               rbrexist=0; subrefstr=""
              }              
           }
          
          
#       refrbpos=index(subref,"}")

       if (refrbpos > 0)                                                                          #
          {refstr=substr(subref,1,refrbpos-1)                                                 #
           rhref=substr(subref,refrbpos+1,length(subref)-refrbpos)                        #
           i=1
           chinesematch=0
           for (i; i<=count; i++)
               {
                if (refstr == labelstrorg[i])
                   {
                    refstrorg=labelstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (chinesematch == 1)
#              {inlinenewb=inlinenewb ltref "\\ref{ch@lb@" refstrorg "}"
              {inlinenewb=inlinenewb ltref preaddstr "ch@lb@" refstrorg "}"
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(refstr)
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(refstr,i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                labelcount=labelcount+1
                lencount=length(linecount)
                labelstrtmp[count]=labelcount
                labelstrorg[count]=refstr
                inlineextra[count]="ch@lb@" labelcount "%" refstr
#                inlinenewb=inlinenewb ltref "\\ref{ch@lb@" \
                inlinenewb=inlinenewb ltref preaddstr "ch@lb@" \
                     labelcount "}"
                inlineupdate=3+addno
               }
              else
               {inlinenewb=inlinenewb ltref "\\ref{" refstr "}"}
              }
           inl=rhref
          }                                                                                        #
      }                                                                                             #
  }

 if (k ==1)
  {if (inlineupdate == 1)
    {inline=inlinenew rhlabel} 
   else
    {if (inlineupdate == 3)
        {inline=inlinenewb rhref}
    }
  }
 else
  {if (inlineupdate == 7)
    {inline=inlinenewb rhref}
  }

 }

################################################

#   item=index(inline,"\\item[")
   item=0
   if (item > 0)
      {if (item > 1)
        {lsubline= substr(inline,1,item-1)}
       else
        {lsubline=""}
       subline=substr(inline,item+6,length(inline)-item-5)
       sec=0
       found=0
       sublen=length(subline)
       i=1; big5=0
       for (i; i<=sublen; i++) 
        {fch=substr(subline,i,1)
         if (fch > "\240" && fch < "\372")
            {sec=1; big5=1
             i=i+1
            }             
         else
            {sec=0
             if (fch == "]")
                {found=1
                 content=substr(subline,1,i-1)
                 rsubline=substr(subline,i+1,sublen-i)
                 break
                }
            }
        }
      if (found == 1)
         {print "\\def\\chtempstr{" content "}"  # > "tmpp@"
          print lsubline "\\item[\\chtempstr]" rsubline # > "tmpp@"
         }
      }
     else
      {
       if (sectionline == 0 && inlineupdate == 0)
          {
#           outline=$0
           print $0 #  > "tmpp@"
          }
       else
          {
#           outline=inline
           print inline # > "tmpp@"
          }
      }
 }
 else
  {
#   outline=$0
   print $0  # > "tmpp@"
  }
}

END{  
      i=bglabelcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@labelfile"
      }
##### End of 6th paragraph ############
}' | \
gawk  -v chitranno=$chitranno \
   -v addperc=$addperc -v beginpercprev=$beginpercprev '
BEGIN{
print "##??!!firstline"
if (chitranno == 0)
   {
    addperc=1; beginpercprev=1}
latin=0
outlinelast=""
inlineold=""
endtab=1; endverb=1
}
{

# sww=0
# outline=""  
  {
    crloc=index($0,"\r")
#    crloc=index(outline,"\r")
   if (crloc > 0)
      {inline=substr($0,1,crloc-1) substr($0,crloc+1,length($0)-crloc)}
#      {inline=substr(outline,1,crloc-1) substr(outline,crloc+1,length(outline)-crloc)}
   else 
      inline=$0
#      inline=outline
   fi
  }

  outline=""
  outlinetwo=""
  outlinetwopre=""

  leftinlinenew=""
  rightinlinenew=""
  inlinenew=inline
  percent=index(inline,"%")
  {if (percent > 0)
     {if (percent != length(inline))
         {inline=substr(inline,1,percent-1)}
     }
  fi}       
#
  {if (addperc == 0)
     { beginperc=index(inline,"\\addpercent")
       if (beginperc > 0)
          {beginpercprev=1}
     }      
   fi
  }
  {if (latin == 0) 
     {
       beginlatin=index(inline,"\\latin")
      if (beginlatinprev >0)
         {latin=1}
      if (beginlatin > 0)
         {beginlatinprev=1
          latin=1
          endlatinb=index(inlinenew,"\\endlatin")
          leftinlinenew=substr(inlinenew,1,beginlatin-1)
          {if (endlatinb > 0)   
             {rightinlinenew=substr(inlinenew,endlatinb,12) "}"
              rightinlinenew=rightinlinenew substr(inlinenew,endlatinb+12,length(inlinenew)-endlatinb+11)
              inlinenew="{" substr(inlinenew,beginlatin,endlatinb-beginlatin)
              latinendlatin=1
             }
           else
              inlinenew="{" substr(inlinenew,beginlatin,length(inlinenew)-beginlatin+1)
          }
         }
     }
   fi
  }
  {if (beginpercprev > 0)
      {unaddperc=index(inline,"\\unaddpercent")
       if (unaddperc > 0)
          {beginpercprev=0}
       fi
      }
  }

  {if (beginlatinprev > 0)
      {endlatin=index(inline,"\\endlatin")
       if (endlatin > 0)
          {beginlatinprev=0
#           latin=0
          {if (latinendlatin != 1)
              {
         rightinlinenew= substr(inlinenew,endlatin,12) "}"
 rightinlinenew=rightinlinenew substr(inlinenew,endlatin+12,length(inlinenew)-endlatin-11)
              leftinlinenew=""
              inlinenew=substr(inlinenew,1,endlatin-1)
              }              
          }
              
          }
       fi
      }
  }
  {if (beginpercprev > 0 || beginlatinprev > 0)
      {
       if (endtab > 0)
          {begintab=index(inline,"\\chtabverbatim")}
       if (begintab > 0)
          {endtab=index(inline,"\\endchtabverbatim")}
       if (endverb > 0)
          {beginverb=index(inline,"\\begin{verbatim")}
       if (endverbcmd > 0)
          {beginverbcmd=index(inline,"\\begin{verbatimcmd}")}

       if (endvalltt > 0)
          {beginalltt=index(inline,"\\begin{alltt}")}
       if (beginverb > 0)       
          {endverb=index(inline,"\\end{verbatim")}
       if (beginverbcmd > 0)       
          {endverbcmd=index(inline,"\\end{verbatimcmd}")}

       if (beginalltt > 0)       
           {endalltt=index(inline,"\\end{alltt}")}

       if (begintab > 0 || beginverb > 0 || beginalltt > 0)
         {
           if (begintab > 0) 
             {if (endtab > 0)
                 {begintab=0
                  endverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

          if (beginverb > 0) 
             {
              if (endverb > 0)
                 {beginverb=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }
          if (beginverbcmd > 0) 
             {
              if (endverbcmd > 0)
                 {beginverbcmd=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

          if (beginalltt > 0) 
             {
              if (endalltt > 0)
                 {beginalltt=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

         }
       else
             {if (beginpercprev > 0)
                  {addperc=1}
              if (beginlatinprev > 0)  
                  {latin=1
              fi}                
             }
      }
   else 
    {addperc=0
     latin=0
    }
  }
  {if (latin > 0)
   {str=""
   kl=0
   len=length(inlinenew)
   sec=0
   kk=1
   for (k=1; k<=len;k++)
   {if (sec == 0)
        {
         ch=substr(inlinenew,kk,1)
         kk=k+1
         if (ch > "\240" && ch < "\372")
            {sec=1
             chpost=substr(inlinenew,kk,1)
        if (chpost >= "a" && chpost <= "}" && chpost != "|")
        {
        {if (chpost == "{")                          
          chpost="\233"
        else
        {if (chpost == "|")                          
          chpost="\234"
        else
        {if (chpost == "~")                          
          chpost="\200"
        else
        {if (chpost == "}")
           chpost="\235"
         else
        {if (chpost == "\133")
            chpost="\236"
         else
        {if (chpost == "\134")
            chpost="\237"
#            chpost="\200"
         else
        {if (chpost == "\135")
             chpost="\238"
          else
          {if (chpost == "a") 
             chpost="\201"
           else
           {if (chpost == "b") 
              chpost="\202"
            else
            {if (chpost == "c") 
               chpost="\203"
             else
             {if (chpost == "d") 
                chpost="\204"
              else
              { if (chpost == "e") 
                  chpost="\205"
               else
               { if (chpost == "f") 
                  chpost="\206"
                else
                { if (chpost == "g") 
                   chpost="\207"
                 else
                 { if (chpost == "h") 
                    chpost="\210"
                  else
                  { if (chpost == "i") 
                      chpost="\211"
                   else
                   { if (chpost == "j") 
                      chpost="\212"
                    else
                    { if (chpost == "k") 
                       chpost="\213"
                    else
                    { if (chpost == "l") 
                        chpost="\214"
                     else
                     { if (chpost == "m") 
                        chpost="\215"
                     else
                     { if (chpost == "n") 
                         chpost="\216"
                      else
                      { if (chpost == "o") 
                         chpost="\217"
                       else
                       { if (chpost == "p") 
                          chpost="\220"
                        else
                        { if (chpost == "q") 
                           chpost="\221"
                         else
                         { if (chpost == "r") 
                            chpost="\222"
                          else
                          { if (chpost == "s") 
                             chpost="\223"
                           else
                           { if (chpost == "t") 
                              chpost="\224"
                            else
                            { if (chpost == "u") 
                               chpost="\225"
                             else
                             { if (chpost="v") 
                                chpost="\226"
                              else
                              { if (chpost == "w") 
                                 chpost="\227"
                               else
                               {  if (chpost == "x") 
                                   chpost="\230"
                                 else
                                 { if (chpost == "y") 
                                    chpost="\231"
                                  else
                                  { if (chpost == "z") 
                                     chpost="\232"
        }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
#        }}}}}}}}}}}}}}}}}}}}}}}}}}}}}
   }
             kk=kk+1
             str=str ch chpost}
         else
            {str=str ch}   
        }
    else
       {sec=0}
    }
     outline=str
   }
  }   

  {if (addperc > 0)
    {
     leninlinenew=length(inlinenew)
     if (substr(inlinenew,leninlinenew,1) == "%")
        {if (leninlinenew > 2)
           {if (substr(inlinenew,leninlinenew-2,1) > "\240")
               {inlinenew=substr(inlinenew,1,leninlinenew-1)}
           }
        }
#     outline=inlinenew "
"

     if (latin == 0)
      {outline=inlinenew "
"}
     else
      {rightinlinenew=rightinlinenew "
"}
    }
   else
    {if (latin == 0)
       {outline=inlinenew}
    }
  }

  {outlinelast=inlineold outlinelast
   if (outlinelast != "")
   {lastchar=substr(outlinelast,length(outlinelast),1)
    if (lastchar != "
")
      {outlinelast=inlineold outlinelast  
#       outlinetwo=outlinelast
       print outlinelast
       inlineold=""}
    else
       {firstchar=substr(outline,1,1)
        if (firstchar > "\240" && firstchar < "\372")
           {lenoutline=length(outlinelast) 
            kk=lenoutline-1
            i=1
            sec=0
            while (kk > i-1)
                {ch=substr(outlinelast,i,1)
                 i=i+1
                 if (ch > "\240" && ch < "\372")
                    {sec=1
                     i=i+1
                     }
                 else
                     {sec=0}
                 }

            if (sec == 1 && lenoutline >2)
               {lchar=substr(outlinelast,lenoutline-2,2)
##                print substr(outlinelast,1,lenoutline-1) "%"
                inlineold=substr(outlinelast,1,lenoutline-1)
                }
            else
               {
#                outlinetwo=substr(outlinelast,1,lenoutline-1)
                print substr(outlinelast,1,lenoutline-1)
                inlineold="" }
           }
        else
           {outlinelast=substr(outlinelast,1,length(outlinelast)-1)
#            outlinetwo=outlinelast
            print outlinelast
            inlineold="" }
       }
   }
   else
   {
#    outlinetwo=outlinelast
    print outlinelast
   }
   outlinelast=leftinlinenew outline rightinlinenew  
  }

  if (beginlatin > 0) 
     {beginlatin=0
      latinendlatin=0
      }
  if (endlatin > 0)
     {endlatin=0
      latin=0
     }
  
}
END{
########
#      i=bglabelcount
#      for (i; i<=count; i++)
#      {print inlineextra[i] >> "ch@labelfile"}
########
print outlinelast
print "#!/bin/sh" > "setaddpercent@"
print "addperc=" addperc > "setaddpercent@"
print "export addperc" > "setaddpercent@"
print "beginpercprev=" beginpercprev > "setaddpercent@"
print "export beginpercprev" > "setaddpercent@"
print "chitranno=1" > "setaddpercent@"
print "export chitranno" > "setaddpercent@"
}'| \
gawk '
BEGIN{
 fnum=0
 sww=0 
 nn=0
}

{
  {

   outline=""
   outlinepre=""
   outlinetwo=$0

#    gsub("
","",$0)
#    gsub("
","",outlinetwo)
##   nn=nn+1           !no more use
#   inlinea=$0
   inlinea=outlinetwo
   gsub(" ","",inlinea)
   if (inlinea != "") 
      {firstnonnull="y"}
   if (inlinea == "" && firstnonnull == "") 
      {outlinepre=""}
   else
#      {print $0}
      {
        outline=outlinetwo
#        outline=$0
      }

   if (sww == 0)
   {
#    l1=index($1,"\\\documentclass")
    l1=index(outlinetwo,"\\\documentclass")

    if (l1 > 0) 
    {
     if (l1 == 1) 
      {sww=1
#       l11=index($1,"%")
       l11=index(outlinetwo,"%")
       if (l11 > 0) 
#          inline=substr($1,1,l11-1)
          {inline=substr(outlinetwo,1,l11-1)}
       else
#          inline=$1
          {inline=outlinetwo}
       fi
       l12=index(inline,"}")
       inline=substr(inline,1,l12)
       l122=index(inline,"{")      
       inlinefname=substr(inline,l122+1,l12-l122-1)
       if (inlinefname == "article")
#          print "\\\usepackage{carticle}"
          outline=outline "\n\\\usepackage{carticle}"
       else
        if (inlinefname == "report")
#          print "\\\usepackage{creport}"
          outline=outline "\n\\\usepackage{creport}"
        else
          if (inlinefname == "book")
#           print "\\\usepackage{cbook}"
           outline=outline "\n\\\usepackage{cbook}"
          else
           if (inlinefname == "letter")
#            print "\\\usepackage{cletter}"
            outline=outline "\n\\\usepackage{cletter}"
           else
            if (inlinefname == "slides")
#             print "\\\usepackage{cslides}"
             outline=outline "\n\\\usepackage{cslides}"
            else
              if (inlinefname == "amsart")
#               print "\\\usepackage{camsart}"
               outline=outline "\n\\\usepackage{camsart}"
              else
               if (inlinefname == "amsbook")
#                print "\\\usepackage{camsbook}"
                outline=outline "\n\\\usepackage{camsbook}"
               else
                  if (inlinefname == "amsmath")
#                    print "\\\usepackage{camsart}"
                    outline=outline "\n\\\usepackage{camsart}"
                  fi
               fi
              fi
            fi
           fi
          fi
        fi
       fi
      }
     fi
    }
    fi
   }
   else
   {
   if (l3 == 0)
#      {l3=index($1,"\134begin{document}")}
      {l3=index(outlinetwo,"\134begin{document}")}

   if (l3 == 0)
   {
#    l2=index($1,"\\\usepackage")
    l2=index(outlinetwo,"\\\usepackage")
    if (l2 > 0) 
    {
     if (l2 == 1) 
      {
#       l21=index($1,"%")
       l21=index(outlinetwo,"%")
       if (l21 > 0) 
#          inline=substr($1,1,l21-1)
          inline=substr(outlinetwo,1,l21-1)
       else
#          inline=$1
          inline=outlinetwo
       fi
       l22=index(inline,"}")
       inline=substr(inline,1,l22)
       l222=index(inline,"{")      
       inlinefname=substr(inline,l222+1,l22-l222-1)
       if (inlinefname == "amstex")
#          print "\\\usepackage{camstex}"
          outline=outline "\n\\\usepackage{camstex}"
       else
          {if (inlinefname == "quotchap")
#             print "\\\usepackage{cengchhd}"
             outline=outline "\n\\\usepackage{cengchhd}"
          fi}
       fi
      }
     fi
    }
   } 
   fi
   }
   fi 
  }     
#}
#END{} | \
###### End of 8th paragraph ############
#$gawkbin 
#BEGIN{}
#{
# inline=$0
  inline=outline
  outlinetwo=""

 percent=index(inline,"%")
 if (percent > 0)
  {if (percent == 0)
     {inline=""}
   else
     {inline=substr(inline,1,percent-1)}
  } 
 if (l3 == 0)
    {l3=index(inline,"begin{document}")
     l33=index(inline,"\134begin {document}")
     }
 if (doclss == 0)
    {doclss=index(inline, "documentclass")
     amsbook=index(inline,"{amsbook}")}
 if ((l3 > 0 || l33 >0) && (doclss >0 && amsbook > 0))
    {while (getline tmpline < "newtheorem@" >0)   
           {
            outlinetwo=tmpline "\n"
#            print tmpline
            print outlinetwo
           }
    }
# print $0
# print outline
   outlinetwo=outlinetwo outline
 print outlinetwo
}
END{
#######
#      i=bglabelcount
#      for (i; i<=count; i++)
#      {print inlineextra[i] >> "ch@labelfile"}
#######
#print outlinelast
#print "#!/bin/sh" > "setaddpercent@"
#print "addperc=" addperc > "setaddpercent@"
#print "export addperc" > "setaddpercent@"
#print "beginpercprev=" beginpercprev > "setaddpercent@"
#print "export beginpercprev" > "setaddpercent@"
#print "chitranno=1" > "setaddpercent@"
#print "export chitranno" > "setaddpercent@"
}' | \
$gawkbin '
BEGIN{firstnonnull=""
#getline recnum < "recnum@"
}
{
   if (NR == 1)
      {firstnonnull=""
       getline recnum < "recnum@"
      }

   outlinetwo=$0
   if (docclass == 0)
#      {docclass=index($0,"\\documentclass")
      {docclass=index(outlinetwo,"\\documentclass")
       if (docclass > 0)
          {print "\\makeatletter%" > "maintempp@"
           print "\\let\\ch@verinsline\\relax%" > "maintempp@"
           print "\\makeatother%" > "maintempp@"
          }
      }

 FS="%" 
 fnum=1
 inperc=0
# ++reccurrent
 wholeline=""
# outlinetwo=$0
# {if (index($0,"\134input") != 0)
 {if (index(outlinetwo,"\134input") != 0)
   {
    nf=split(outlinetwo,arr)
#    nf=split($0,arr)

    while (fnum <=nf)
    {#print "fnum=" fnum " nf="  nf
    if (substr(arr[fnum],length(arr[fnum]),1) != "\134"||inperc ==1)
      {  inperc=1
         fnumsec=fnum+1
         inputpos = index(arr[fnumsec],"\134input")
         if (inputpos != 0)
            {if (inputpos == 1) 
               substrsec = ""
             else
	       substrsec = substr(arr[fnumsec],1,inputpos-1)
             fi
             if (fnum == 1)
                {wholeline=wholeline arr[fnum] "%" substrsec "\134@@!#?@enput" \
                 substr(arr[fnumsec],inputpos+6,length(arr[fnumsec])-inputpos-5)}
             else
                {wholeline=wholeline "%" substrsec "\134@@!#?@enput" \
                substr(arr[fnumsec],inputpos+6,length(arr[fnumsec])-inputpos-5)}
            }
         else
            {if (nf == 1)
#                wholeline=$0
                 wholeline=outlinetwo
             else
               {if (fnum == 1)
                  wholeline=wholeline arr[fnum] "%" arr[fnumsec]
                else
                  if (fnum <nf)
                    wholeline=wholeline "%" arr[fnumsec]
                  fi
    	        fi
               }
             fi
            }
	 fi
      }
    else
#   wholeline=$0
      {wholeline=outlinetwo}
    fi

    fnum=fnum+1
    }  # while (fnum <=nf)
   }   # if (index(outlinetwo,"\134input") != 0)
  else
#   wholeline=$0
   {wholeline=outlinetwo}
  fi
 }
 
#  firstmarkpos=index($0,"##??!!firstline")
  firstmarkpos=index(outlinetwo,"##??!!firstline")
##  if (firstmarkpos == 1 || (reccurrent == recnum && ($0 == "" || $0 == "\r")))
  if (firstmarkpos == 1)
      { }
  else
     {if (firstnonnull == "")
         {inlinea=wholeline
          gsub(" ","",inlinea)
          crpos=index(inlinea,"\r")
          if (inlinea == "" || inlinea == "\r")
             { }
          else
            {firstnonnull="y"
            print wholeline > "maintempp@"}
         }
      else
        {print wholeline > "maintempp@"}
     }

  }
END{
#######
#      i=bglabelcount
#      for (i; i<=count; i++)
#      {print inlineextra[i] >> "ch@labelfile"}
########
##print outlinelast
#print outlinelast >> "maintempp@"
#print "#!/bin/sh" > "setaddpercent@"
#print "addperc=" addperc > "setaddpercent@"
#print "export addperc" > "setaddpercent@"
#print "beginpercprev=" beginpercprev > "setaddpercent@"
#print "export beginpercprev" > "setaddpercent@"
#print "chitranno=1" > "setaddpercent@"
#print "export chitranno" > "setaddpercent@"
} '



if test -s maintempp@; then
   mv -f  maintempp@ maintemp@
fi

  sed -n '/\\input/p' maintemp@  > klinetemp@


if test -s trancode@; then rm -f trancode@; fi
if test -s maintemp2@;then rm -f maintemp2@; fi

if test ! -s klinetemp@; then 
  if test "$stexf" != ""; then
     chicode -u maintemp@|chicodelist|\
     sed 's#einput#input#g' > $filea.tmp00
  else
     chicode -u maintemp@|chicodelist| \
     sed 's#einput#input#g'  > $outfilename
  fi
  exit

#if test ! -s klinetemp@
#then 
##  if test -s stexf@; then
#  if test "$stexf" != ""; then
#     chicode -u maintemp@ > $filea.tmp00
#  else
#     chicode -u maintemp@ > $outfilename
#     sed 's#einput#input#g' $outfilename > maintemp@
#     mv maintemp@ $outfilename
#  fi
#  exit

else

#tr "%" "\235" < klinetemp@|sed "s/^/%/" |tr "%" "\012"|tr "\235" "%" > klinebtemp@

tr "%" "\235" < klinetemp@|sed "s/^/%/" |tr "%" "\012"|tr "\235" "%"| \
$gawkbin '
BEGIN{}
{
 FS="\\\134input" 
 fnum=2 
 while (fnum <=NF)
     {print $fnum 
     fnum = fnum + 1
      }
 }
END{}' | \
$gawkbin '
BEGIN {}
{ 
la=index($0,"\{")
firstch = substr($0,1,1)

if (firstch != " " && firstch != "\{")
       {null = 1}
fi

 if (la == 0)
   {sefldch = substr($1,1,1)
    if (sefldfch == "\\" || sefldfch == "%")
        {null = 1}
    else
     { secach = index($1,"\\")
       secbch = index($1,"%")
       if (secach + secbch >0)
        {if (secach + secbch ==2)       
           {if (secach >= secbch)
               {secach = secbch}
            fi
           }
         else 
            {if (secach == 0 )
               {secach = secbch}
             fi
            }
         fi
         srepl = substr($0,1,index($0,$1)+secach-2)
         sfirst = substr($1,1,secach-1)
        }
       else 
          {srepl = substr($0,1,index($0,$1)+length($1)-1)
           sfirst = $1
           }
       fi
     }
    fi
   }

 else

  {   
     sixch = substr($0,1,1)
     if (sixch == "\{")
         {null = 1}
     else 
       { if (sixch == " ")
           {if (substr($1,1,1) == "\{")
               {null = 1}
            else
               {null = 1}
            fi
           }
         else 
            {null = 1}
         fi
       }
      lb=index($0,"\}")
      sfirst=substr($0,la+1,lb-la-1)
      srepl=substr($0,1,lb)
  }
 fi

    slash=index(sfirst,"/")
sfilename=sfirst
#
 if (sfirst == "chitex" || sfirst == "vanilla.sty" \
     || sfirst == "amstex" || sfirst == "chitexl" || sfirst == ""\
     || sfirst == "amstex.tex" || sfirst == "tcilatex.tex"\
     || sfirst == "tcilatex" || sfirst == "ctciltx" || sfirst == "pictex"\
     || sfirst == "ctciltx.tex" || sfirst == "colordvi"\
     || sfirst == "colordvi.tex" \
     || slash > 0 && length(sfirst) == slash)
    {null = 1}
 else
 {
      slash=1
	 lslash=0
      ssfirst=sfirst
      while (slash>0)
      {slash=index(sfirst,"/")
       if (slash > 0)
         {sfirst=substr(sfirst,slash+1,length(sfirst))
          lslash=lsalsh+slash}
       fi
      }

      lc=index(sfirst,".")
      {if (lc > 0)
         {sprev=substr(ssfirst,1,lslash+lc-1)
          ssprev=substr(sfirst,1,lc-1) 
          spost=substr(sfirst,lc+1,length(sfirst)-lc)    
         }
      fi
      }

   outfilename=sfilename
   slashnum=1
   while (slashnum > 0)
   {slashnum=index(outfilename,"/")
    if (slashnum > 0)
     {outfilename= \
     substr(outfilename,1,slashnum-1)"c"substr(outfilename,slashnum+1,length(outfilename)) 
     }
   }
  outfilename=".tmp00/" outfilename

   if (lc == 0)
      {
      {print "#!/bin/sh" > "replacetemp@"} 
      {print "if test ! -s " ssfirst".tex"  > "replacetemp@"}
     {print "then " > "replacetemp@"}
     {print "     null=1 " > "replacetemp@"}
     {print "else" > "replacetemp@"}

#     {print "  sed  \"s#\\\\\134input" srepl "#\\\\\134input " \
#      outfilename"#g\"  maintemp@|cat > maintemp@" > "replacetemp@"}
     {print "  sed  \"s#\\\\\134input" srepl "#\\\\\134input\{" \
      outfilename "\}#g\"  maintemp@|cat > maintemp@" > "replacetemp@"}

#      outfilename"#g\"  maintemp@ > maintemp2@" > "replacetemp@"}
#     {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}

     {print "fi" > "replacetemp@"}
      if (kkk == 0)
         {print "#!/bin/sh"  > "nexttemp@"
          print " "  > "nexttemp@"; kkk=1}
     {print "if test -s " ssfirst".tex; then"  > "nexttemp@"}
#     {print "     null=1 " > "nexttemp@"}
#     {print "else" > "nexttemp@"}
     {print "     chitran " ssfirst >"nexttemp@"}
     {print "fi " > "nexttemp@"}
 
     }
   else
     {
      {print "#!/bin/sh" > "replacetemp@"} 
      {print "if test ! -s " ssfirst  > "replacetemp@"}
      {print "then " > "replacetemp@"}
      {print "     null=1 " > "replacetemp@"}
      {print "else" > "replacetemp@"}

      {if (spost == "tex") 
       {

        {print "sed  \"s#\\\\\134input" srepl "#\\\\\134input\{"\
         outfilename "\}#g\"  maintemp@|cat > maintemp@" >"replacetemp@"}
#         outfilename"#g\"  maintemp@ > maintemp2@" >"replacetemp@"}
#
#        {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}
        }
       else
        {

        {print "  sed -n \"/" ssprev".tmp00/p\" maintemp@ >" sfirst".@@@" > "replacetemp@"}
        {print "sed -e \"s#\\\\\134input" srepl "#\\\\\134input\{"\
         outfilename "\}#g\"  maintemp@|cat > maintemp@" >"replacetemp@"}
#         outfilename"#g\"  maintemp@ > maintemp2@" >"replacetemp@"}
#        {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}
        }
       }
     {print "fi" > "replacetemp@"}
      if (kkk == 0)
         {print "#!/bin/sh"  > "nexttemp@"
          print " "  > "nexttemp@";kkk=1}
     {print "if test -s " ssfirst " ;then"> "nexttemp@"}
#     {print "     null=1 " > "nexttemp@"}
#     {print "else" > "nexttemp@"}
     {print "     chitran " ssfirst >"nexttemp@"}
     {print "fi " > "nexttemp@"}


     }
   fi
  }
}

END{}' 

#ksegtemp@

#   fi


     if test ! -s replacetemp@
     then 
       null="1"
     else
       chmod +x replacetemp@
       ./replacetemp@
     fi


#sed 's/\\@@!#?@enput/\\input/g' maintemp@ > maintempnext@
#sed 's#\\einput#\\input#g' maintempnext@ > maintemp@

sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
sed 's#\\einput#\\input#g'  > maintempnext@


#mv maintempnext@ maintemp@


  if test "$stexf" != ""; then
     chicode -u maintempnext@|chicodelist| \
     sed 's#einput#input#g' > $filea.tmp00
  else
     chicode -u maintempnext@|chicodelist| \
     sed 's#einput#input#g'  > $outfilename
  fi


  if test ! -s nexttemp@
   then null="1"
  else 
    chmod +x nexttemp@
    ./nexttemp@
  fi


  rm -f *.@@@
fi

rm -f tmp@

chitranno=1
export chitranno

exit
     
$gawkbin -v nochprotect=$nochprotect -v chbold=$chbold '
BEGIN{
}
{  inline=$0
   subinline=$0
   inlinea=$0; percent=0

   if (docclass == 0)
      {docclass=index(inline,"\\documentclass")
       if (docclass > 0)
          {print "\\makeatletter%"
           print "\\let\\ch@verinsline\\relax%"
           print "\\makeatother%"
          }
      }

   verbatimpos=index(subinline,"\\begin{verbatim")

   if (verbatimpos==0)
      {print $0;next}

   percent=index(inline,"%")
   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
       }

   if (percent<verbatimpos)
      {print $0; next}

# to skip the right side of % not between
#                                   "\begin{verbatim" and "\end{verbatim"
   subline=$0; line=""; haveverbatim=0
   while (subline != "")
   {verb=index(subline,"\\begin{verbatim")
    if (verb==0)
     {line=line subline; break} 
    else
     {haveverbatim=1
      percent=index(subinline,"%")
      if (percent==0)
        {line=line subline; break}
      else
        {verba=index(subline,"\\begin{verbatim*")
         if (verba>0)
            {verb=verba;verbcharno=16}
         else
            {verbcharno=15}

         if (percent<verb)
           {line=line substr(subline,1,percent-1); break}
         else
           {
            rline=substr(subline,verb+verbcharno+1,length(subline)-verbcharno)
            endverb=index(rline,"\\end{verbatim")
            endverba=index(rline,"\\end{verbatim*")
            if (endverba>0)
               {endverbno=15}
            else
               {endverbno=14} 

            if (endverb==0)
              {line=$0;finish=1;break}

            endverb=endverb+endverno+verb+verbcharno-1
            line=line substr(subline,1,endverb)
            subline= \
         substr(subline,endverb+1,length(subline)-endverb)
            subinline= \
         substr(subinline,endverb+1,length(subinline)-endverb)
           }
        }
     }
   }

#  subline=line
#   haveverb=0
#   inlinea=subline

   inlinea=line


#  if (percenta == 1)
  {
   verbatimpos=index(inlinea,"\\begin{verbatim}")
   endverbatimpos=index(inlinea,"\\end{verbatim}")
   verbatimposa=index(inlinea,"\\begin{verbatim*}")
   endverbatimposa=index(inlinea,"\\end{verbatim*}")

   if (verbatimpos==0)
      {verbatimpos=index(inline,"\\begin{verbatim}")}
   if (endverbatimpos==0)
      {endverbatimpos=index(inline,"\\end{verbatim}")}
   if (verbatimposa==0)
      {verbatimposa=index(inline,"\\begin{verbatim*}")}
   if (endverbatimposa==0)
      {endverbatimposa=index(inline,"\\end{verbatim*}")}

   if (verbatimpos > 0 && endverbatimpos > 0)
     {gsub("\\\\end{verbatim}","\n\\end{verbatim}\n\\ch@verinsline ",inlinea)}

   if (verbatimposa > 0 && endverbatimposa > 0)
    {gsub("\\\\end{verbatim\\*}","\n\\end{verbatim*}\n\\ch@verinsline ",inlinea)}

  }
   if (percent == 1)
      {inlinea=$0}

   print inlinea
   
}
END{}' maintemp@ | \
$gawkbin -v nochprotect=$nochprotect -v chbold=$chbold '
BEGIN{
oldinline=""
inbracelbrno=0
oldfpos=0
verbatim=0
}
{
   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   subinline=inline
   percent=index(inline,"%")
   perpos=percent
   newtheorem="n"

   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 1)
           {inline=substr(inline,1,percent-1)}
        else
           if (percent == 1)
              {inline=""}
       }


#   if (perfound == 1)
#      {inline=substr(inline,1,perpos-1)}

    if (inline == "")
       {oldfpos=0}


# Find \chboldface,...
 if (chbold == "")
  {chboldface=index(inline,"\\chboldface")
   choutline=index(inline,"\\choutline")
   outline=index(inline,"\\outline")
   if (chboldface !=0 || choutline != 0 || outline != 0)
      {print "yes" > "chbold@";chbold="y"}
  }

# Find \verbatim

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")


  endd=0
  fpos=0

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}


 if (verbpos>0||verbatimpos > 0||boxedverbatimpos > 0||listingpos > 0 \
     || verbatimwritepos > 0||verbatimtabpos > 0|| chtabverbatimpos >0)
    {verbatim=1; verbatimnocmd=1}
 if (endverbatimpos > 0||endboxedverbatimpos > 0||endlistingpos > 0 \
     ||endverbatimwritepos > 0||endverbatimtabpos > 0||endchtabverbatimpos >0)
    {verbatim=0;verbatimnocmd=0}

 if (alttpos > 0|| verbatimcmdpos > 0)
    {verbatim=1; verbatimnocmd=0}
 if (endallttpos > 0|| endverbatimcmdpos > 0)
    {verbatim=0;verbatimnocmd=0}

 if (verbatimnocmd == 1)
    {gsub("\\input","\einput",inlineorg)
    }

 if (verbatim == 0)
 {
  if (doclss == 0)
    {doclss=index(inline, "documentclass")
     amsbook=index(inline,"{amsbook}")}
  
  if (baselineskiploc == 0)
     {baselineskiploc=index(inline,"\\baselineskip")
      if (baselineskiploc > 0)
         {print "\\def\\ch@baselineskip{n}"}
     }
  if (index(inline,"\\newtheorem") > 0)
     {newtheorem="y"
      print inline > "newtheorem@"
     }

  if (newtheorem == "y" && (doclss > 0 && amsbook > 0))
           {print "%" inlineorg}

 if (nochprotect == "")
  {
  j=1
  for (j; j<=2; j++)
  {
   chappos=index(inline,"\\chapter{"); chapposb=index(inline,"\\chapter {")
   secpos=index(inline,"\\section{"); secposb=index(inline,"\\section {")

   subsecpos=index(inline,"\\subsection{");subsecposb= \
      index(inline,"\\subsection {")
   subsubsecpos=index(inline,"\\subsubsection{");subsubsecposb= \
         index(inline,"\\subsubsection {")

   paragraphpos=index(inline,"\\paragraph{")
   paragraphposb=index(inline,"\\paragraph {")
   subparagraphpos=index(inline,"\\subparagraph{")
   subparagraphposb=index(inline,"\\subparagraph {")

   fpos=0
   if (chappos > 0)
     {lbr[1]=chappos+8; fpos=1}
   else
     {if (chapposb > 0)
        {lbr[1]=chappos+9; fpos=1}
      else
        {if (secpos > 0)
          {lbr[1]=secpos+8; fpos=2}
         else
          {if (secposb > 0)
            {lbr[1]=secpos+9; fpos=2}
           else
             {if (subsecpos > 0)
               {lbr[1]=subsecpos+11; fpos=3}
              else
              {if (subsecposb > 0)
                {lbr[1]=subsecposb+12; fpos=3}
               else
                {if (subsubsecpos > 0)
                  {lbr[1]=subsubsecpos+14; fpos=4}
                 else
                   {if (subsubsecposb > 0)
                     {lbr[1]=subsubsecposb+15; fpos=4}
                    else                                                 #
                     {if (paragraphpos > 0)                              #
                         {lbr[1]=paragraphpos+10; fpos=5}                #
                      else                                               #
                         {if (paragraphposb > 0)                         #
                             {lbr[1]=paragraphposb+11; fpos=5}           #
                          else                                           #
                             {if (subparagraphpos > 0)                   #
                                {lbr[1]=subparagraphpos+13; fpos=6}      #
                              else                                       #
                                {if (subparagraphposb > 0)               #
                                    {lbr[1]=subparagraphposb+14; fpos=6} #
                                }  #11                                   #
                             }   #10                                     #
                         }   #9                                          #
                     }   #8                                              #
                   }
                }
              }
             }
          }
        }
     }

   
     if (j == 1)
      {if (oldinline !="")
        {if ((oldfpos != fpos && fpos !=0) || inline == "")
            {print oldinline; break}
         else
            {inline=oldinline inline}
        }

      }



  }

    oldinline=""
    lbrpos=lbr[1]; lbrno=1

    rbrno=0
    if (fpos > 0)
       {linline=substr(inline,1,lbr[1])
        rinline=substr(inline,lbr[1]+1,length(inline)-lbr[1])

        if (rinline !="")   # if rinline="" let it unchanged
        {
         sec=0
         rbrexist=0
         lenn=length(rinline)
         i=1

         for (i; i<=lenn; i++)
             {ch[i]=substr(rinline,i,1)}

         rbrexist=0; rbrno=0 
         i=1
         for (i; i<=lenn; i++) 
         {if (ch[i] > "\240" && ch[i] < "\372")
              {sec=1
               i=i+1
              }             
          else
              {sec=0
               if (ch[i] == "}" && ch[i-1] != "\\")
                  {
                   rbrexist=1
                   rbrpos=i
                   rbrno=1
                   break
                  }
             }
         }
         

         if (rbrexist == 1)    # if exist rbr find lbrno
          {lblock=substr(rinline,1,rbrpos-1)
           if (lenn == rbrpos)
             {rblock="";endd=1}
           else
             {rblock=substr(rinline,rbrpos+1,lenn-rbrpos)}


           if (lblock != "")   # if lblock !=""
           {lenlblock=length(lblock)
            i=1
            for (i; i<=lenlblock; i++) 
            {ch[i]=substr(lblock,i,1)
             if (ch[i] > "\240" && ch[i] < "\372")
                {i=i+1}             
             else
                {if (ch[i] == "{" && ch[i-1] != "\\")
                    {++lbrno}
                }
            }
           }
           else
           {endd=1}             #if lblock="" lbrno=1 never increased


           if (lbrno > rbrno)        
              {   #find rbr "}" in rblock if rblock !=""
               lastrbrpos=0
               if (rblock != "")
                {lenn=length(rblock)
                 i=1
                 for (i; i<=lenn; i++)
                   {ch[i]=substr(rblock,i,1)}

                 bgrbrpos=1; i=1
                 for (i; i<=lenn; i++) 
                 {lbrnewfound=1
                   {if (ch[i] > "\240" && ch[i] < "\372")
                     {i++}             
                    else
                     {#secc=0;
                      newblock=""; lbrnewfound=1
                      if (ch[i] == "}" && ch[i-1] != "\\")
                         {newblock=substr(rblock,bgrbrpos,i-1)
                          lennew=length(newblock)
                          lbrnewfound=0; bgrbrpos=i
                          j=1
                          for (j; j<=lennew; j++)
                          {
                           chnew[j]=substr(newblock,j,1)
                            {if (chnew[j] > "\240" && chnew[j] < "\372")
                                {j++}
                             else
                                {if (chnew[j] == "{" && chnew[j-1] != "\\")
                                  {lbrnewfound=1; break}
                                }
                            }
                          }
                          if (lbrnewfound == 0)
                             {++rbrno;
                              if (lbrno == rbrno)
                                 {lastrbrpos=i;break}
                             }
                         }
                     }
                   }
                 }




                }
              }        #if resulted lbrno=rbrno lastrbrpos>0
           else                     #where lbrno=rbrno=1
              {}

           if (lbrno <= rbrno)
              {if (rblock != "")
                {lenn=length(rblock)
                 righttorbr=substr(rblock,lastrbrpos,lenn-lastrbrpos+1)
                  inbrace=substr(lblock,1,rbrpos-1) "}" 
                 inbrace=inbrace substr(rblock,1,lastrbrpos-1)
                }
               else   #where rblock="" must lbrno=rbrno=1
                {inbrace=substr(lblock,1,rbrpos-1) "}"; righttorbr=""}

               len=length(inbrace)
               inbracestr=""
               i=1
               for (i; i<=len; i++)
                   {ch[i]=substr(inbrace,i,1)}
               i=1
               for (i; i<=len; i++)
               {if (ch[i] > "\240" && ch[i] < "\372")
                   {inbracestr=inbracestr ch[i] ch[i+1]; i++}
                else
                   {if (ch[i] == "\\")
                       {inbracestr=inbracestr "\\protect" ch[i]} 
#                       {inbracestr=inbracestr ch[i]} 
                    else 
                       {inbracestr=inbracestr ch[i]}
                   }
               }

               inline=linline inbracestr righttorbr
               endd=1
              }
           else
              {if (percent == 0)
                  {oldinline=inline " "}
               else 
                  {oldinline=inline}
               endd=1
              }
          }
         else
          {
           {if (percent == 0)         #where rbr not exist, lbrno=1 rbrno=0
               {oldinline=inline " "} #move inline to oldinline, to
            else                      #append to inline of next record
               {oldinline=inline}
            endd=1                    
           }                            
          }

        }
       else                       #where rinline=""
        {
         {if (percent == 0)
             {oldinline=inline " "}
          else 
             {oldinline=inline}
          endd=1
         }
        }
       }
    else
       {endd=1}   # where fpos=0
   }
  }
    if (fpos == 0)
       {if (newtheorem == "y" && (doclss > 0 && amsbook > 0))
#           {print "%" inlineorg}
           {  }
        else
           {print inlineorg}
        }
    else
       {if (oldinline == "")
           {print inline}
       }
  
}
END{} ' | \
$gawkbin -v nochprotect=$nochprotect '
BEGIN{
#if (nochprotect == "y")
#   {}
}
{
if (nochprotect == "y")
   {print $0; next}

   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {inline=substr(inline,1,percent-1);perfound=1}
       }



   if (perfound == 1)
      {#inline=substr(inline,1,perpos-1)
       perstr="%"}
   else
      {perstr=""}
   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")
   
   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

# if (verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \

 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0\
     ||verbatimtabpos>0||boxedverbatimpos>0||listingpos>0 \
     ||verbatimwritepos>0||chtabverbatimpos>0)
    {verbatim=1;bgverbatim=1;endverbatim=0}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0;bgverbatim=0;endverbatim=1}

 if (bgverbatim == 1 && verbpos==0)
    {printf "\\unaddpercent"}
 if (bgverbatim == 1)
    {bgverbatim=0}
    

 if (verbatim == 0)
    {
     citepos=index(inline,"\\cite")
     if (citepos == 0)
      {if (endverbatim == 1)
        {#printf inline perstr
         print inline perstr
         print "\\addpercent"; endverbatim=0}
       else
        {print inline perstr}
      }
     else
      {morecite=1
       rinline=inline
       firstcite=1
       while (morecite == 1)
       {
        linline=substr(rinline,1,citepos+4)
        rinline=substr(rinline,citepos+5,length(rinline)-citepos-4)
        citepos=index(rinline,"\\cite")
        if (citepos > 0)
         {lrinline=substr(rinline,1,citepos-1)
          if (firstcite == 1)
             {print linline lrinline "%" 
              firstcite=0
              }
          else
             {print "\\cite" lrinline "%"}
         }
        else
         {morecite=0
          if (firstcite == 1)
             {print linline rinline perstr}
#?             {print linline rinline}
          else
             {print "\\cite" rinline perstr}
#?             {print "\\cite" rinline}
         }
       }
      }
    }
 else
    {print inlineorg}

}
END{} ' | \
$gawkbin -v nochprotect=$nochprotect '
BEGIN{
#if (nochprotect == "y")
#   {exit}
}
{
if (nochprotect == "y")
   {print $0; next}

   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
            #inline=substr(inline,1,percent-1);perfound=1 
           }
       }


   if (perfound == 1)
      {
#       if (perpos != length(inline))
       if (percent != length(inline))
          {inline=substr(inline,1,perpos-1)}
      }

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}

 if (verbatim == 0)
    {
     citepos=index(inline,"\\nocite")
     if (citepos == 0)
      {print inline}
     else
      {
       morecite=1
       rinline=inline
       firstcite=1
       while (morecite == 1)
       {
        linline=substr(rinline,1,citepos+6)
        rinline=substr(rinline,citepos+7,length(rinline)-citepos-6)
        citepos=index(rinline,"\\nocite")
        if (citepos > 0)
         {lrinline=substr(rinline,1,citepos-1)
          if (firstcite == 1)
             {print linline lrinline "%"
              firstcite=0
              }
          else
             {print "\\nocite" lrinline "%"}
         }
        else
         {morecite=0
          if (firstcite == 1)
             {print linline rinline}
          else
             {print "\\nocite" rinline}
         }
       }
      }
    }
 else
    {print inlineorg
    }

}
END{} ' | \
$gawkbin  -v nochprotect=$nochprotect ' 
BEGIN{
   while (getline labeline < "ch@labelfile" >0)
      {labelper=index(labeline,"%")
       if (labelper > 0) 
        {
         labelat=index(labeline,"bib@")
         if (labelat > 0)
         {#++counta
          counta=substr(labeline,labelat+4,labelper-labelat-4)
          chlabel[counta]=substr(labeline,labelper+1,length(labeline)-labelper)
          bitemstrorg[counta]=chlabel[counta]
          enlabel[counta]=substr(labeline,1,lablelper-1)
          bitemstrtmp[counta]=counta
         }
        }
      }
   count=counta
   bitemcount=count
   linecount=bitemcount
   bgbitemcount=bitemcount+1
   ++bitemcount
}
{

if (nochprotect == "y")
   {print $0; next}

   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
#            inline=substr(inline,1,percent-1);perfound=1
           }
       }


   if (perfound == 1)
      {if (percent != length(inline))
         {inline=substr(inline,1,perpos-1)}
      }

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}


 if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  bitempos=1
  while (inl != "" && bitempos != 0)
  {
   len=length(inl)
   bitemposa=0
   bitemposb=0
   bitempos=0
   bitemrbpos=0

   bitemposa=index(inl,"\\bibitem{") 
   bitemposb=index(inl,"\\bibitem {")
   bitemposc=index(inl,"\\bibitem[") 

   if (bitemposc > 0)
      {#ltbitem=substr(inl,1,bitemposc-1)
       fsubbitem=substr(inl,bitemposc+9,length(inl)-bitemposc-8)
       roptlbr=index(fsubbitem,"]{")
       if (roptlbr > 0)
          {
           bitemposc=bitemposc+8+roptlbr+1
          }
       else
          {bitemposc=0}
      }

   if (bitemposc > 0)
      {bitempos=bitemposc                                                                           
       bitembgpos=bitemposc+1
      }       
   else
      {if (bitemposa > 0 )                                                       
        {bitempos=bitemposa                                                                           
         bitembgpos=bitemposa+9
        }                                                                      
       else                                                               
        {if (bitemposb > 0 )                                               
         {bitempos=bitemposb
          bitembgpos=bitemposb+10
         }
        }
      }


   if (bitempos > 0)                                                                                
      {
       subbitem=substr(inl,bitembgpos,len-bitembgpos+1)                                          
       ltbitem=substr(inl,1,bitembgpos-1)
       bitemrbpos=0
       subbitemstr=subbitem
       rbrexist=0

          while (subbitemstr !="" && rbrexist == 0)
           {
            bitemrbtmp=index(subbitemstr,"}")
            if (bitemrbtmp > 0)
              {
               prerbr=substr(subbitemstr,1,bitemrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;bitemrbpos=bitemrbpos+bitemrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;bitemrbpos=bitemrbpos+bitemrbtmp}
                 else
                    {subbitemstr= \
             substr(subbitemstr,bitemrbtmp+1,length(subbitemstr)-bitemrbtmp)
                     bitemrbpos=bitemrbpos+bitemrbtmp
                    }
              }
            else
              {
               rbrexist=0; subbitemstr=""
              }              
           }
          

       if (bitemrbpos > 0)
          {linecount=linecount + "1"
           bitemstr=substr(subbitem,1,bitemrbpos-1)                                                 
           lenn=length(bitemstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(bitemstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               bitemcount=bitemcount + 1 
               i=1; havebitemstr=0
               if (count == 0)
                  {havebitemstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (bitemstrorg[i] == bitemstr)
                           {havebitemstr=1;matchcount=i;break}
                       }
                  } 
               if (havebitemstr == 0)
                  {count=count+1
                   matchcount=count
                   bitemstrorg[count]=bitemstr
                   lencount=length(linecount)
                   bitemstrtmp[count]=bitemcount
                  }

               inlineextra[count]="ch@bib@" bitemstrtmp[matchcount] "%" bitemstr
               inlinenew=inlinenew ltbitem "ch@bib@" \
                  bitemstrtmp[matchcount] "}"
               inlineupdate=1
             }
            else
             {inlinenew=inlinenew ltbitem "\\bibitem{" bitemstr "}"}  # update
            
           rhbitem=substr(subbitem,bitemrbpos+1,length(subbitem)-bitemrbpos)                        #               
           inl=rhbitem
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhbitem}
  else
     {inl=inline}

 k=1
 for (k; k<=2; k++)
 {if (k == 2)
     {inl=inline}
  inlinenewb=""
  citepos=1
  while (inl != "" && citepos != 0)
  {len=length(inl)
   citeposa=0
   citeposb=0
   citeposc=0
   citepos=0
   citerbpos=0
   
   if (k == 1)
    {citeposa=index(inl,"\\cite{")                                                                   #
     citeposb=index(inl,"\\cite {")                                                                  #
     citeposc=index(inl,"\\cite[")
     addno=0
    }
   else
    {citeposa=index(inl,"\\nocite{")                                                                   #
     citeposb=index(inl,"\\nocite {")
     addno=2
    }
    

   if (citeposc > 0)
      {
       fsubcite=substr(inl,citeposc+6,length(inl)-citeposc-5)
       roptlbr=index(fsubcite,"]{")
       if (roptlbr > 0)
          {
           citeposc=citeposc+5+roptlbr+1
          }
       else
          {citeposc=0}
      }

   if (citeposc > 0)
      {citepos=citeposc                                                                           
       citebgpos=citeposc+1
      }       
   else
      {
       if (citeposa > 0 )                                                                              #
          {citepos=citeposa                                                                           #
           citebgpos=citeposa+6+addno
          }                                                                      #
       else                                                                                             #
          {if (citeposb > 0 )                                                                          #
           {citepos=citeposb
            citebgpos=citeposb+7+addno}                                                                     #
          }                                                                                             #
      }
 
   if (citepos > 0)                                                                                #
      {if (citepos > 1)                                                                            #
          {ltcite=substr(inl,1,citepos-1)}                                                     #
       else                                                                                         #
          {ltcite=""}                                                                              #

       subcite=substr(inl,citebgpos,len-citebgpos+1)                                          #
       ltcite=substr(inl,1,citebgpos-1)

       citerbpos=0
       subcitestr=subcite
       rbrexist=0

          while (subcitestr !="" && rbrexist == 0)
           {
            citerbtmp=index(subcitestr,"}")
            if (citerbtmp > 0)
              {
               prerbr=substr(subcitestr,1,citerbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;citerbpos=citerbpos+citerbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;citerbpos=citerbpos+citerbtmp}
                 else
                    {subcitestr= \
             substr(subcitestr,citerbtmp+1,length(subcitestr)-citerbtmp)
                     citerbpos=citerbpos+citerbtmp
                    }
              }
            else
              {
               rbrexist=0; subcitestr=""
              }              
           }
          
          
       if (citerbpos > 0)                                                                          #
          {citestr=substr(subcite,1,citerbpos-1)                                                 #
           rhcite=substr(subcite,citerbpos+1,length(subcite)-citerbpos)
           comma=1; subbcitestr=citestr
           jj=0
           while (subbcitestr != "" && comma != 0)
           {comma=index(subbcitestr,",")
            ++jj
            if (comma == 0)
               {citecomp[jj]=subbcitestr}
            else
               {citecomp[jj]=substr(subbcitestr,1,comma-1)
                subbcitestr=\
                  substr(subbcitestr,comma+1,length(subbcitestr)-comma)
               }
           }

           j=1
           for (j; j<=jj; j++)
           {i=1
            for (i; i<=count; i++)
               {chinesematch=0
                if (citecomp[j] == bitemstrorg[i])
                   {
                    citestrorg=bitemstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (j == 1)
              {commastr=""}
           else
              {commastr=","}
           
           if (chinesematch == 1)
              {#inlinenewb=inlinenewb ltcite "\\cite{ch@bib@" citestrorg "}"
               #inlinenewb=inlinenewb ltcite "ch@bib@" citestrorg "}"
               inlinenewb=inlinenewb commastr "ch@bib@" citestrorg
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(citecomp[j])
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(citecomp[j],i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                bitemcount=bitemcount+1
                lencount=length(linecount)
                bitemstrtmp[count]=bitemcount
                bitemstrorg[count]=citecomp[j]
                inlineextra[count]="ch@bib@" bitemcount "%" citecomp[j]
                inlinenewb=inlinenewb commastr "ch@bib@" bitemcount

                inlineupdate=3+addno
               }
              else
               {inlinenewb=inlinenewb commastr citecomp[j]}
#               {inlinenewb=inlinenewb ltcite "\cite{" citecomp[j] "}"}
              }
            inl=rhcite
            if (j == jj)
               {inlinenewb=ltcite inlinenewb "}"}
           } #new for (j; j<=jj; j++) 
         }                                                                                        #
      }                                                                                             #
  }

  if (k == 1)
  {if (inlineupdate == 1)
      {inline=inlinenew rhbitem}
   else
      {if (inlineupdate == 3)
          {inline=inlinenewb rhcite}
      }
  }
  else 
      {if (inlineupdate == 5 )
          {inline=inlinenewb rhcite}
      }
 }  # End of for (k; k<=2; k++)

################################################

       if (inlineupdate == 0)
          {print inlineorg}
       else
          {print inline}
 }
 else
  {print inlineorg}
}
END{  
      i=bgbitemcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@labelfile"
      }
#       print  bitemcount > "ch@exportb"
}' |\
$gawkbin  -v nochprotect=$nochprotect ' 
BEGIN{
#  if (nochprotect == "y")
#     {print "yes" > nochprotect}

   while (getline labeline < "ch@labelfile" >0)
      {labelper=index(labeline,"%")
       if (labelper > 0) 
        {
         labelat=index(labeline,"lb@")
         if (labelat > 0)
         {#++countb
          countb=substr(labeline,labelat+3,labelper-labelat-3)
          chlabel[countb]=substr(labeline,labelper+1,length(labeline)-labelper)
          labelstrorg[countb]=chlabel[countb]
          enlabel[countb]=substr(labeline,1,lablelper-1)
          labelstrtmp[countb]=countb
         }
        }
      }
   count=countb
   labelcount=count
   linecount=labelcount
   bglabelcount=labelcount+1
   ++labelcount

}
{
if (nochprotect == "y")
   {#print "yes" > "nochprotect"
    print $0; next}

   inline=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 0)
           {perfound=1
#            inline=substr(inline,1,percent-1);perfound=1
           }
       }


   if (percenttmp > 0)
       {
        if (substr(subinline,percent-1,1) == "\\")
           {per=1; subinline=substr(inline,percent+1,length(inline)-percent)
            perfound=0
            while (per !=0 && subinline !="" && perfound ==0)  
            {per=index(subinline,"%")
             if (per > 0)
                { perpos=perpos+per
                  if (substr(subinline,percent-1,1) == "\\")
                     {subinline=substr(subinline,per+1,length(subinline)-per)
                      percent=per
                     }
                  else
                     {perfound=1; break}
                }
             else
                {perfound=0}
            }
           }
        else
           {perfound=1}
       }


   if (perfound == 1)
#      {if (perpos != length(inline))
      {if (percent != length(inline))
         {inline=substr(inline,1,perpos-1)}}

   verbpos=index(inline,"\\verb")  
   verbatimpos=index(inline,"\\begin{verbatim")
   endverbatimpos=index(inline,"\\end{verbatim")
   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
   chtabverbatimpos=index(inline,"\\chtabverbatim")
   endchtabverbatimpos=index(inline,"\\endchtabverbatim")

   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")

   allttpos=index(inline,"\\begin{alltt}")
   endallttpos=index(inline,"\\end{alltt}")

   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
    {verbatim=1}
 if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
      ||endchtabverbatimpos >0)
    {verbatim=0}


 if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  labelpos=1
  while (inl != "" && labelpos != 0)
  {
   len=length(inl)
   labelposa=0
   labelposb=0
   labelpos=0
   labelrbpos=0

   labelposa=index(inl,"\\label{")                                                               
   labelposb=index(inl,"\\label {")                                                              
   
   if (labelposa > 0 )                                                       
      {labelpos=labelposa                                                                           
       labelbgpos=labelposa+7
      }                                                                      
   else                                                               
      {if (labelposb > 0 )                                               
       {labelpos=labelposb
        labelbgpos=labelposb+8}                                                                     
      }                                                                                             

   if (labelpos > 0)                                                                                
      {
       if (labelpos > 1)                                                                            
          {ltlabel=substr(inl,1,labelpos-1)}                                                     
       else                                                                                         
          {ltlabel=""}                                                                              
       sublabel=substr(inl,labelbgpos,len-labelbgpos+1)                                          
       ltlabel=substr(inl,1,labelpos-1)
#       labelrbpos=index(sublabel,"}")
       labelrbpos=0
       sublabelstr=sublabel
       rbrexist=0

          while (sublabelstr !="" && rbrexist == 0)
           {
            labelrbtmp=index(sublabelstr,"}")
            if (labelrbtmp > 0)
              {
               prerbr=substr(sublabelstr,1,labelrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;labelrbpos=labelrbpos+labelrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;labelrbpos=labelrbpos+labelrbtmp}
                 else
                    {sublabelstr= \
             substr(sublabelstr,labelrbtmp+1,length(sublabelstr)-labelrbtmp)
                     labelrbpos=labelrbpos+labelrbtmp
                    }
              }
            else
              {
               rbrexist=0; sublabelstr=""
              }              
           }
          

       if (labelrbpos > 0)
          {linecount=linecount + "1"
           labelstr=substr(sublabel,1,labelrbpos-1)                                                 
           lenn=length(labelstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(labelstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               labelcount=labelcount + 1 
               i=1; havelabelstr=0
               if (count == 0)
                  {havelabelstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (labelstrorg[i] == labelstr)
                           {havelabelstr=1;matchcount=i;break}
                       }
                  } 
               if (havelabelstr == 0)
                  {count=count+1
                   matchcount=count
                   labelstrorg[count]=labelstr
                   lencount=length(linecount)
                   labelstrtmp[count]=labelcount
                  }

               inlineextra[count]="ch@lb@" labelstrtmp[matchcount] "%" labelstr
#               inlinenew=inlinenew ltlabel "\\protect\\label{ch@lb@" \
               inlinenew=inlinenew ltlabel "\\label{ch@lb@" \
                  labelstrtmp[matchcount] "}"

               inlineupdate=1
             }
            else
             {inlinenew=inlinenew ltlabel "\\label{" labelstr "}"}
           rhlabel=substr(sublabel,labelrbpos+1,length(sublabel)-labelrbpos)                        #               
           inl=rhlabel
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhlabel}

 k=1
 for (k; k<=2; k++)
 {if (k ==2)
     {inl=inline}
  inlinenewb=""
  refpos=1
  while (inl != "" && refpos != 0)
  {len=length(inl)
   refposa=0
   refposb=0
   refpos=0
   refrbpos=0

   if (k == 1)
    {refposa=index(inl,"\\ref{");preaddstr="\\ref{"
     refposb=index(inl,"\\ref {");addno=0}
   else
    {refposa=index(inl,"\\pageref{");preaddstr="\\pageref{"                                                     #
     refposb=index(inl,"\\pageref {");addno=4}

   if (refposa > 0 )                                                                              #
      {refpos=refposa
       refbgpos=refposa+5+addno
      }                                                                      #
   else                                                                                             #
      {if (refposb > 0 )                                                                          #
       {refpos=refposb
        refbgpos=refposb+6+addno}
      }                                                                                             #


   if (refpos > 0)                                                                                #
      {if (refpos > 1)                                                                            #
          {ltref=substr(inl,1,refpos-1)}                                                     #
       else                                                                                         #
          {ltref=""}                                                                              #

       subref=substr(inl,refbgpos,len-refbgpos+1)                                          #
       ltref=substr(inl,1,refpos-1)                                                          #

       refrbpos=0
       subrefstr=subref
       rbrexist=0

          while (subrefstr !="" && rbrexist == 0)
           {
            refrbtmp=index(subrefstr,"}")
            if (refrbtmp > 0)
              {
               prerbr=substr(subrefstr,1,refrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;refrbpos=refrbpos+refrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;refrbpos=refrbpos+refrbtmp}
                 else
                    {subrefstr= \
             substr(subrefstr,refrbtmp+1,length(subrefstr)-refrbtmp)
                     refrbpos=refrbpos+refrbtmp
                    }
              }
            else
              {
               rbrexist=0; subrefstr=""
              }              
           }
          
          
#       refrbpos=index(subref,"}")

       if (refrbpos > 0)                                                                          #
          {refstr=substr(subref,1,refrbpos-1)                                                 #
           rhref=substr(subref,refrbpos+1,length(subref)-refrbpos)                        #
           i=1
           chinesematch=0
           for (i; i<=count; i++)
               {
                if (refstr == labelstrorg[i])
                   {
                    refstrorg=labelstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (chinesematch == 1)
#              {inlinenewb=inlinenewb ltref "\\ref{ch@lb@" refstrorg "}"
              {inlinenewb=inlinenewb ltref preaddstr "ch@lb@" refstrorg "}"
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(refstr)
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(refstr,i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                labelcount=labelcount+1
                lencount=length(linecount)
                labelstrtmp[count]=labelcount
                labelstrorg[count]=refstr
                inlineextra[count]="ch@lb@" labelcount "%" refstr
#                inlinenewb=inlinenewb ltref "\\ref{ch@lb@" \
                inlinenewb=inlinenewb ltref preaddstr "ch@lb@" \
                     labelcount "}"
                inlineupdate=3+addno
               }
              else
               {inlinenewb=inlinenewb ltref "\\ref{" refstr "}"}
              }
           inl=rhref
          }                                                                                        #
      }                                                                                             #
  }

 if (k ==1)
  {if (inlineupdate == 1)
    {inline=inlinenew rhlabel} 
   else
    {if (inlineupdate == 3)
        {inline=inlinenewb rhref}
    }
  }
 else
  {if (inlineupdate == 7)
    {inline=inlinenewb rhref}
  }

 }

################################################

#   item=index(inline,"\\item[")
   item=0
   if (item > 0)
      {if (item > 1)
        {lsubline= substr(inline,1,item-1)}
       else
        {lsubline=""}
       subline=substr(inline,item+6,length(inline)-item-5)
       sec=0
       found=0
       sublen=length(subline)
       i=1; big5=0
       for (i; i<=sublen; i++) 
        {fch=substr(subline,i,1)
         if (fch > "\240" && fch < "\372")
            {sec=1; big5=1
             i=i+1
            }             
         else
            {sec=0
             if (fch == "]")
                {found=1
                 content=substr(subline,1,i-1)
                 rsubline=substr(subline,i+1,sublen-i)
                 break
                }
            }
        }
      if (found == 1)
         {print "\\def\\chtempstr{" content "}"  # > "tmpp@"
          print lsubline "\\item[\\chtempstr]" rsubline # > "tmpp@"
         }
      }
     else
      {
       if (sectionline == 0 && inlineupdate == 0)
          {
           print $0 #  > "tmpp@"
          }
       else
          {
           print inline # > "tmpp@"
          }
      }
 }
 else
  {
   print $0  # > "tmpp@"
  }
}
END{  
      i=bglabelcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@labelfile"
      }
#       print labelcount > "ch@export"
} ' |\
$gawkbin -v chitranno=$chitranno -v addperc=$addperc -v beginpercprev=$beginpercprev '
BEGIN{
print "##??!!firstline"
#addperc=0
if (chitranno == 0)
   {
    addperc=1; beginpercprev=1}
latin=0
outlinelast=""
inlineold=""
endtab=1; endverb=1
}
{sww=0
 outline=""  
  {#llen=length($0)
#   crloc=index($0,"
")
    crloc=index($0,"\r")
   if (crloc > 0)
      {inline=substr($0,1,crloc-1) substr($0,crloc+1,length($0)-crloc)}
#      {if ($0 == "
")
#          {inline=" "}
#       else
#          {inline=substr($0,1,crloc-1)
#           if (inline == "")
#              {inline=" "}
#           }
#      }
   else 
      inline=$0
   fi
  }
  leftinlinenew=""
  rightinlinenew=""
  inlinenew=inline
  percent=index(inline,"%")
  {if (percent > 0)
     {if (percent != length(inline))
         {inline=substr(inline,1,percent-1)}
     }
  fi}       
#
  {if (addperc == 0)
     { beginperc=index(inline,"\\addpercent")
       if (beginperc > 0)
          {beginpercprev=1}
     }      
   fi
  }
  {if (latin == 0) 
     {
       beginlatin=index(inline,"\\latin")
      if (beginlatinprev >0)
         {latin=1}
      if (beginlatin > 0)
         {beginlatinprev=1
          latin=1
          endlatinb=index(inlinenew,"\\endlatin")
          leftinlinenew=substr(inlinenew,1,beginlatin-1)
          {if (endlatinb > 0)   
             {rightinlinenew=substr(inlinenew,endlatinb,12) "}"
              rightinlinenew=rightinlinenew substr(inlinenew,endlatinb+12,length(inlinenew)-endlatinb+11)
              inlinenew="{" substr(inlinenew,beginlatin,endlatinb-beginlatin)
              latinendlatin=1
             }
           else
              inlinenew="{" substr(inlinenew,beginlatin,length(inlinenew)-beginlatin+1)
          }
         }
     }
   fi
  }
  {if (beginpercprev > 0)
      {unaddperc=index(inline,"\\unaddpercent")
       if (unaddperc > 0)
          {beginpercprev=0}
       fi
      }
  }

  {if (beginlatinprev > 0)
      {endlatin=index(inline,"\\endlatin")
       if (endlatin > 0)
          {beginlatinprev=0
#           latin=0
          {if (latinendlatin != 1)
              {
         rightinlinenew= substr(inlinenew,endlatin,12) "}"
 rightinlinenew=rightinlinenew substr(inlinenew,endlatin+12,length(inlinenew)-endlatin-11)
              leftinlinenew=""
              inlinenew=substr(inlinenew,1,endlatin-1)
              }              
          }
              
          }
       fi
      }
  }
  {if (beginpercprev > 0 || beginlatinprev > 0)
      {
       if (endtab > 0)
          {begintab=index(inline,"\\chtabverbatim")}
       if (begintab > 0)
          {endtab=index(inline,"\\endchtabverbatim")}
       if (endverb > 0)
          {beginverb=index(inline,"\\begin{verbatim")}
       if (endverbcmd > 0)
          {beginverbcmd=index(inline,"\\begin{verbatimcmd}")}

       if (endvalltt > 0)
          {beginalltt=index(inline,"\\begin{alltt}")}
       if (beginverb > 0)       
          {endverb=index(inline,"\\end{verbatim")}
       if (beginverbcmd > 0)       
          {endverbcmd=index(inline,"\\end{verbatimcmd}")}

       if (beginalltt > 0)       
           {endalltt=index(inline,"\\end{alltt}")}

       if (begintab > 0 || beginverb > 0 || beginalltt > 0)
         {
           if (begintab > 0) 
             {if (endtab > 0)
                 {begintab=0
                  endverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

          if (beginverb > 0) 
             {
              if (endverb > 0)
                 {beginverb=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }
          if (beginverbcmd > 0) 
             {
              if (endverbcmd > 0)
                 {beginverbcmd=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

          if (beginalltt > 0) 
             {
              if (endalltt > 0)
                 {beginalltt=0
                  endtaverb=0
                  addperc=1
                  latin=0
                 }
              else
                {addperc=0
                 latin=0
                }
             }

         }
       else
             {if (beginpercprev > 0)
                  {addperc=1}
              if (beginlatinprev > 0)  
                  {latin=1
              fi}                
             }
      }
   else 
    {addperc=0
     latin=0
    }
  }
  {if (latin > 0)
   {str=""
   kl=0
   len=length(inlinenew)
   sec=0
   kk=1
   for (k=1; k<=len;k++)
   {if (sec == 0)
        {
         ch=substr(inlinenew,kk,1)
         kk=k+1
         if (ch > "\240" && ch < "\372")
            {sec=1
             chpost=substr(inlinenew,kk,1)
        if (chpost >= "a" && chpost <= "}" && chpost != "|")
        {
        {if (chpost == "{")                          
          chpost="\233"
        else
        {if (chpost == "|")                          
          chpost="\234"
        else
        {if (chpost == "~")                          
          chpost="\200"
        else
        {if (chpost == "}")
           chpost="\235"
         else
        {if (chpost == "\133")
            chpost="\236"
         else
        {if (chpost == "\134")
            chpost="\237"
#            chpost="\200"
         else
        {if (chpost == "\135")
             chpost="\238"
          else
          {if (chpost == "a") 
             chpost="\201"
           else
           {if (chpost == "b") 
              chpost="\202"
            else
            {if (chpost == "c") 
               chpost="\203"
             else
             {if (chpost == "d") 
                chpost="\204"
              else
              { if (chpost == "e") 
                  chpost="\205"
               else
               { if (chpost == "f") 
                  chpost="\206"
                else
                { if (chpost == "g") 
                   chpost="\207"
                 else
                 { if (chpost == "h") 
                    chpost="\210"
                  else
                  { if (chpost == "i") 
                      chpost="\211"
                   else
                   { if (chpost == "j") 
                      chpost="\212"
                    else
                    { if (chpost == "k") 
                       chpost="\213"
                    else
                    { if (chpost == "l") 
                        chpost="\214"
                     else
                     { if (chpost == "m") 
                        chpost="\215"
                     else
                     { if (chpost == "n") 
                         chpost="\216"
                      else
                      { if (chpost == "o") 
                         chpost="\217"
                       else
                       { if (chpost == "p") 
                          chpost="\220"
                        else
                        { if (chpost == "q") 
                           chpost="\221"
                         else
                         { if (chpost == "r") 
                            chpost="\222"
                          else
                          { if (chpost == "s") 
                             chpost="\223"
                           else
                           { if (chpost == "t") 
                              chpost="\224"
                            else
                            { if (chpost == "u") 
                               chpost="\225"
                             else
                             { if (chpost="v") 
                                chpost="\226"
                              else
                              { if (chpost == "w") 
                                 chpost="\227"
                               else
                               {  if (chpost == "x") 
                                   chpost="\230"
                                 else
                                 { if (chpost == "y") 
                                    chpost="\231"
                                  else
                                  { if (chpost == "z") 
                                     chpost="\232"
        }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
#        }}}}}}}}}}}}}}}}}}}}}}}}}}}}}
   }
             kk=kk+1
             str=str ch chpost}
         else
            {str=str ch}   
        }
    else
       {sec=0}
    }
     outline=str
   }
  }   

  {if (addperc > 0)
    {
     leninlinenew=length(inlinenew)
     if (substr(inlinenew,leninlinenew,1) == "%")
        {if (leninlinenew > 2)
           {if (substr(inlinenew,leninlinenew-2,1) > "\240")
               {inlinenew=substr(inlinenew,1,leninlinenew-1)}
           }
        }
#     outline=inlinenew "
"

     if (latin == 0)
      {outline=inlinenew "
"}
     else
      {rightinlinenew=rightinlinenew "
"}
    }
   else
    {if (latin == 0)
       {outline=inlinenew}
    }
  }

  {outlinelast=inlineold outlinelast
   if (outlinelast != "")
   {lastchar=substr(outlinelast,length(outlinelast),1)
    if (lastchar != "
")
      {outlinelast=inlineold outlinelast  
       print outlinelast
       inlineold=""}
    else
       {firstchar=substr(outline,1,1)
        if (firstchar > "\240" && firstchar < "\372")
           {lenoutline=length(outlinelast) 
            kk=lenoutline-1
            i=1
            sec=0
            while (kk > i-1)
                {ch=substr(outlinelast,i,1)
                 i=i+1
                 if (ch > "\240" && ch < "\372")
                    {sec=1
                     i=i+1
                     }
                 else
                     {sec=0}
                 }

            if (sec == 1 && lenoutline >2)
               {lchar=substr(outlinelast,lenoutline-2,2)
#                print substr(outlinelast,1,lenoutline-1) "%"
                inlineold=substr(outlinelast,1,lenoutline-1)
                }
            else
               {print substr(outlinelast,1,lenoutline-1)
                inlineold="" }
           }
        else
           {outlinelast=substr(outlinelast,1,length(outlinelast)-1)
            print outlinelast
            inlineold="" }
       }
   }
   else
   {print outlinelast}
   outlinelast=leftinlinenew outline rightinlinenew  
  }

  if (beginlatin > 0) 
     {beginlatin=0
      latinendlatin=0
      }
  if (endlatin > 0)
     {endlatin=0
      latin=0
     }
  
}
END{
print outlinelast
print "#!/bin/sh" > "setaddpercent@"
print "addperc=" addperc > "setaddpercent@"
print "export addperc" > "setaddpercent@"
print "beginpercprev=" beginpercprev > "setaddpercent@"
print "export beginpercprev" > "setaddpercent@"
print "chitranno=1" > "setaddpercent@"
print "export chitranno" > "setaddpercent@"

}' | \
$gawkbin '
BEGIN{
 fnum=0
 sww=0 
 nn=0
}
#
{
  {
    gsub("
","",$0)
#    gsub("
","",$1)
#   msign=index($0,
)
#   lenline=length($0)
#   if (msign == lenline)
#      {mvmline=substr($0,1,lenline-1)
#       print mvmline}
#   else
#      {print $0}
   nn=nn+1
   inlinea=$0
   gsub(" ","",inlinea)
   if (inlinea != "") 
      {firstnonnull="y"}
   if (inlinea == "" && firstnonnull == "") 
      { }
   else
      {print $0}
   if (sww == 0)
   {
    l1=index($1,"\\\documentclass")
    if (l1 > 0) 
    {
     if (l1 == 1) 
      {sww=1
       l11=index($1,"%")
       if (l11 > 0) 
          inline=substr($1,1,l11-1)
       else
          inline=$1
       fi
       l12=index(inline,"}")
       inline=substr(inline,1,l12)
       l122=index(inline,"{")      
       inlinefname=substr(inline,l122+1,l12-l122-1)
       if (inlinefname == "article")
          print "\\\usepackage{carticle}"
       else
        if (inlinefname == "report")
          print "\\\usepackage{creport}"
        else
          if (inlinefname == "book")
           print "\\\usepackage{cbook}"
          else
           if (inlinefname == "letter")
            print "\\\usepackage{cletter}"
           else
            if (inlinefname == "slides")
             print "\\\usepackage{cslides}"
            else
              if (inlinefname == "amsart")
               print "\\\usepackage{camsart}"
              else
               if (inlinefname == "amsbook")
                print "\\\usepackage{camsbook}"
               else
                  if (inlinefname == "amsmath")
                    print "\\\usepackage{camsart}"
                  fi
               fi
              fi
            fi
           fi
          fi
        fi
       fi
      }
     fi
    }
    fi
   }
   else
   {
   if (l3 == 0)
      {l3=index($1,"\134begin{document}")}

   if (l3 == 0)
   {
    l2=index($1,"\\\usepackage")
    if (l2 > 0) 
    {
     if (l2 == 1) 
      {
       l21=index($1,"%")
       if (l21 > 0) 
          inline=substr($1,1,l21-1)
       else
          inline=$1
       fi
       l22=index(inline,"}")
       inline=substr(inline,1,l22)
       l222=index(inline,"{")      
       inlinefname=substr(inline,l222+1,l22-l222-1)
       if (inlinefname == "amstex")
          print "\\\usepackage{camstex}"
       else
          {if (inlinefname == "quotchap")
             print "\\\usepackage{cengchhd}"
          fi}
       fi
      }
     fi
    }
   } 
   fi
   }
   fi 
  }     
}
END{}' | \
$gawkbin '
BEGIN{}
{
 inline=$0
 percent=index(inline,"%")
 if (percent > 0)
  {if (percent == 0)
     {inline=""}
   else
     {inline=substr(inline,1,percent-1)}
  } 
 if (l3 == 0)
    {l3=index(inline,"begin{document}")
     l33=index(inline,"\134begin {document}")
     }
 if (doclss == 0)
    {doclss=index(inline, "documentclass")
     amsbook=index(inline,"{amsbook}")}
 if ((l3 > 0 || l33 >0) && (doclss >0 && amsbook > 0))
    {while (getline tmpline < "newtheorem@" >0)   
           {print tmpline}
    }
 print $0
}
END{
#print NR > "recnum@"
} ' | \
$gawkbin '
BEGIN{firstnonnull=""
getline recnum < "recnum@"
}
{
 FS="%" 
 fnum=1
 inperc=0
# ++reccurrent
 wholeline=""
 {if (index($0,"\134input") != 0)
   while (fnum <=NF)
   {#print "fnum=" fnum " NF="  NF
   if (substr($fnum,length($fnum),1) != "\134"||inperc ==1)
   {     inperc=1
         fnumsec=fnum+1
         inputpos = index($fnumsec,"\134input")
         if (inputpos != 0)
            {if (inputpos == 1) 
               substrsec = ""
            else
	       substrsec = substr($fnumsec,1,inputpos-1)
            fi
            if (fnum == 1)
                {wholeline=wholeline $fnum "%" substrsec "\134@@!#?@enput" \
                substr($fnumsec,inputpos+6,length($fnumsec)-inputpos-5)}
            else
                {wholeline=wholeline "%" substrsec "\134@@!#?@enput" \
                substr($fnumsec,inputpos+6,length($fnumsec)-inputpos-5)}
            }
         else
            {if (NF == 1)
                wholeline=$0
            else
               {if (fnum == 1)
                  wholeline=wholeline $fnum "%" $fnumsec
               else
                  if (fnum <NF)
                    wholeline=wholeline "%" $fnumsec
                  fi
    	       fi
               }
            fi
            }
	 fi
      }
   else
   wholeline=$0
   fi
    fnum=fnum+1
   }
  else
   wholeline=$0
  fi
  }
 
  firstmarkpos=index($0,"##??!!firstline")
#  if (firstmarkpos == 1 || (reccurrent == recnum && ($0 == "" || $0 == "\r")))
  if (firstmarkpos == 1)
      { }
  else
     {if (firstnonnull == "")
         {inlinea=wholeline
          gsub(" ","",inlinea)
          crpos=index(inlinea,"\r")
          if (inlinea == "" || inlinea == "\r")
             { }
          else
            {firstnonnull="y"
            print wholeline > "maintempp@"}
         }
      else
        {print wholeline > "maintempp@"}
     }

  }
END{} '
