function eqn_supsub($latex_markup)
{
// LaTeX can only handle a single character or a block
// So start by turning single characters into blocks
global $lp ;
$end_characters[] = ' ' ;
$end_characters[] = PHP_EOL ;
$end_characters[] = '_' ;
$end_characters[] = $lp ;
$end_characters[] = '^' ;
$end_characters[] = '' ;
$latex_markup = $latex_markup . ' ' ;
$special_character[] = '^' ;
$special_character[] = '_' ;
$open[] = '##SUPA##' ;
$open[] = '##SUBA##' ;
$close[] = '##SUPB##' ;
$close[] = '##SUBB##' ;
for($i=0 ; $i<count($special_character) ; $i++)
{
$strlen = strlen($latex_markup) ;
for($j=$strlen ; $j>-1 ; $j--)
{
if(substr($latex_markup, $j, 2)==$special_character[$i] . $lp)
{
$start = $j ;
$end = match_parenthesis($latex_markup, $j+1) ;
$string = substr($latex_markup, $start+2, $end-$start-2) ;
$string = $open[$i] . $string . $close[$i] ;
$latex_markup = substr_replace($latex_markup, $string, $start, $end-$start+1) ;
}
}
}
for($i=0 ; $i<count($special_character) ; $i++)
{
$strlen = strlen($latex_markup)-2 ;
for($j=$strlen ; $j>-1 ; $j--)
{
$escape = false ;
if(substr($latex_markup, $j, 1)==$special_character[$i])
{
if(substr($latex_markup, $j+1, 1)!=$lp)
{
if(substr($latex_markup, $j+1, 1)!='')
{
$character = substr($latex_markup, $j+1, 1) ;
$latex_markup = substr_replace($latex_markup, $open[$i] . $character . $close[$i], $j, 2) ;
}
else
{
for($k=$j+2 ; $k<strlen($latex_markup) ; $k++)
{
for($l=0 ; $l<count($end_characters) ; $l++)
{
if(substr($latex_markup, $k, 1)==$end_characters[$l])
{
$string = substr($latex_markup, $j+1, $k-$j-1) ;
$string = $open[$i] . $string . $close[$i] ;
$latex_markup = substr_replace($latex_markup, $string , $j, $k-$j) ;
$escape = true ;
}
if($escape==true) break ;
}
if($escape==true) break ;
}
}
}
}
}
}
// Now look for ##SUP-A##...##SUP-B## and ##SUB-A##...##SUB-B## that are the wrong way around and swap them
$latex_markup = preg_replace('/##SUBB##( )*##SUPA##/', '##SUBB####SUPA##', $latex_markup) ;
$latex_markup = preg_replace('/##SUPB##( )*##SUBA##/', '##SUPB####SUBA##', $latex_markup) ;
$calls = 0 ;
while(preg_match('/##SUBB####SUPA##/', $latex_markup) AND $calls<10)
{
for($i=strlen($latex_markup)+1 ; $i>-1 ; $i--)
{
if(substr($latex_markup, $i, 16)=='##SUBB####SUPA##')
{
$sub_end = $i+7 ;
$sup_start = $i+8 ;
for($j=$i ; $j>-1 ; $j--)
{
if(substr($latex_markup, $j, 8)=='##SUBA##')
{
$sub_start = $j ;
break ;
}
}
for($j=$i+16 ; $j<strlen($latex_markup) ; $j++)
{
if(substr($latex_markup, $j, 8)=='##SUPB##')
{
$sup_end = $j+7 ;
break ;
}
}
$sub_length = $sub_end - $sub_start + 1 ;
$sup_length = $sup_end - $sup_start + 1 ;
$sub = substr($latex_markup, $sub_start, $sub_length) ;
$sup = substr($latex_markup, $sup_start, $sup_length) ;
$latex_markup = substr_replace($latex_markup, $sub, $sup_start, $sup_length) ;
$latex_markup = substr_replace($latex_markup, $sup, $sub_start, $sub_length) ;
break ;
}
}
$calls++ ;
}
$latex_markup = preg_replace('/##SUPB##( )*##SUBA##/', '##SUPB####SUBA##', $latex_markup) ;
// Now look for lonely sups and add empty subs to them and vice versa
$calls = 0 ;
while(substr_count($latex_markup, '##SUPB####SUBA##')<substr_count($latex_markup, '##SUPB##') AND $calls<10)
{
for($i=strlen($latex_markup)-1 ; $i>-1 ; $i--)
{
if(substr($latex_markup, $i, 8)=='##SUPB##' AND substr($latex_markup, $i, 16)!='##SUPB####SUBA##')
{
$latex_markup = substr_replace($latex_markup, '##SUPB####SUBA##,##SUBB##', $i, 8) ;
}
}
$calls++ ;
}
$calls = 0 ;
while(substr_count($latex_markup, '##SUPB####SUBA##')<substr_count($latex_markup, '##SUBA##') AND $calls<10)
{
for($i=strlen($latex_markup)-1 ; $i>-1 ; $i--)
{
if($i>8) // Protect against negative starting points
{
if(substr($latex_markup, $i, 8)=='##SUBA##' AND substr($latex_markup, $i-8, 16)!='##SUPB####SUBA##')
{
$latex_markup = substr_replace($latex_markup, '##SUPA####SUPB####SUBA##', $i, 8) ;
}
}
else
{
if(substr($latex_markup, $i, 8)=='##SUBA##')
{
$latex_markup = substr_replace($latex_markup, '##SUPA##,##SUPB####SUBA##', $i, 8) ;
}
}
}
$calls++ ;
}
return $latex_markup ;
}