File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.415: download - view: text, annotated - select for diffs
Mon Feb 1 12:03:19 2010 UTC (14 years, 3 months ago) by foxr
Branches: MAIN
CVS tags: version_2_9_99_0, version_2_10_X, bz6209-base, bz6209, HEAD
Remove some of the debugging output in 'tex' mode that was used in images as it has \n's that now appear to be stripped and cause issues when used in tables.

    1: # The LearningOnline Network with CAPA
    2: # Tags Default Definition Module 
    3: #
    4: # $Id: londefdef.pm,v 1.415 2010/02/01 12:03:19 foxr Exp $
    5: # 
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   30: # binary executable programs or libraries distributed by the 
   31: # Michigan State University (the "Licensee"), but any binaries so 
   32: # distributed are hereby licensed only for use in the context
   33: # of a program or computational system for which the Licensee is the 
   34: # primary author or distributor, and which performs substantial 
   35: # additional tasks beyond the translation of (La)TeX into HTML.
   36: # The C source of the Code may not be distributed by the Licensee
   37: # to any other parties under any circumstances.
   38: #
   39: 
   40: package Apache::londefdef; 
   41: 
   42: use Apache::lonnet;
   43: use strict;
   44: use Apache::lonxml;
   45: use Apache::lontable;
   46: use Apache::File();
   47: use Image::Magick;
   48: use Apache::lonmenu();
   49: use Apache::lonmeta();
   50: use Apache::lonlocal;
   51: use Apache::Constants qw(:common);
   52: use File::Basename;
   53: use LONCAPA();
   54: # use Data::Dumper;
   55: 
   56: BEGIN {
   57: 
   58:     &Apache::lonxml::register('Apache::londefdef',('a','abbr','acronym','accessrule','address','allow','applet','area','b','base','basefont','bgo','bgsound','big','blink','blockquote','blankspace','body','br','button','caption','center','cite','code','col','colgroup','dd','del','dfn','dir','div','dl','dt','em','embed','externallink','fieldset','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','i','iframe','img','input','ins','insert','isindex','kbd','keygen','label','layer','legend','li','link','m','map','marquee','menu','meta','multicol','nobr','noembed','noframes','nolayer','noscript','object','ol','optgroup','option','output','p','param','pre','q','s','samp','select','server','small','spacer','span','strike','strong','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','tthoption','u','ul','var','wbr','hideweboutput'));
   59: 
   60: }
   61: 
   62: 
   63: sub initialize_londefdef {
   64:     $Apache::londefdef::TD_redirection=0;
   65:     @Apache::londefdef::table = ();
   66:     $Apache::londefdef::select=0;
   67:     undef(@Apache::londefdef::description);
   68:     @Apache::londefdef::DD=(0);
   69:     @Apache::londefdef::DT=(0);
   70:     @Apache::londefdef::seenDT=(0);
   71:     $Apache::londefdef::list_index=0;
   72:     undef($Apache::londefdef::head);
   73:     undef($Apache::londefdef::title);
   74: }
   75: 
   76: #======================= TAG SUBROUTINES =====================
   77: #-- <output>
   78: sub start_output {
   79:     my ($target) = @_;
   80:     if ($target eq 'meta') { $Apache::lonxml::metamode--; }
   81:     return '';
   82: }
   83: sub end_output {
   84:     my ($target) = @_;
   85:     if ($target eq 'meta') { $Apache::lonxml::metamode++; }
   86:     return '';
   87: }
   88: #-- <m> tag
   89: sub start_m {
   90:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   91:     my $currentstring = '';
   92:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
   93:     if ($target eq 'web' || $target eq 'analyze') {
   94: 	&Apache::lonxml::debug("M is starting with:$inside:");
   95: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
   96: 	if ($eval eq 'on') {
   97: 	    $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
   98: 	    #&Apache::lonxml::debug("M is evaluated to:$inside:");
   99: 	}
  100: 	my $tex = $inside;
  101: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
  102: 	$currentstring = &Apache::lontexconvert::converted(\$inside,$display);
  103: 	if ($Apache::lontexconvert::errorstring) {
  104: 	    my $errormsg='<pre>'.&HTML::Entities::encode($Apache::lontexconvert::errorstring,'<>&"').'</pre> occurred while attempting to convert this TeX: <pre>';
  105: 	    $tex = &HTML::Entities::encode($tex,'<>&"');
  106: 	    my ($linenumber) =
  107: 		($Apache::lontexconvert::errorstring =~ /Line (\d+)/);
  108: 	    if (defined($linenumber)) {
  109: 		my @tex=split("\n",$tex);
  110: 		$tex[$linenumber]='<b><font color="red">'.
  111: 		    $tex[$linenumber].'</font></b>';
  112: 		$tex=join("\n",@tex);
  113: 	    }
  114: 	    &Apache::lonxml::warning($errormsg.$tex.'</pre>');
  115: 	    $Apache::lontexconvert::errorstring='';
  116: 	}
  117: 	#&Apache::lonxml::debug("M is ends with:$currentstring:");
  118: 	$Apache::lonxml::post_evaluate=0;
  119:     } elsif ($target eq 'tex') {
  120: 
  121: 	$currentstring = $inside;
  122: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
  123: 	if ($eval eq 'on') {
  124: 	    $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
  125: 	}
  126: 	if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
  127: 	# detect simple math mode entry exits, and convert them
  128:         # to use \ensuremath ... unless there's a \verb inside.
  129: 	if (! ($currentstring=~/\\verb/)) {
  130: 	    if ($currentstring=~/^\s*\$[^\$].*\$\s*$/) {
  131: 		$currentstring=~s/^(\s*)\$/$1/;
  132: 		$currentstring=~s/\$(\s*)$/$1/;
  133: 		$currentstring='\ensuremath{'.$currentstring.'}';
  134: 	    }
  135: 	}
  136: 	$Apache::lonxml::post_evaluate=0;
  137:     }
  138:     return $currentstring;
  139: }
  140: 
  141: sub end_m {
  142:     my ($target,$token) = @_;
  143:     my $currentstring = '';
  144:     if ($target eq 'tex') {
  145: 	$currentstring = "";
  146:     }
  147:     return $currentstring;
  148: }
  149: 
  150: sub start_tthoption {
  151:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  152:     my $result;
  153:     if ($target eq 'web' || $target eq 'webgrade') {
  154: 	my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser,
  155: 						   $style);
  156: 	$inside=~s/^\s*//;
  157: 	if ($env{'browser.mathml'}) {
  158: 	    &tth::ttmoptions($inside);
  159: 	} else {
  160: 	    &tth::tthoptions($inside);
  161: 	}
  162:     }
  163:     return $result;
  164: }
  165: 
  166: sub end_tthoption {
  167:     my ($target,$token) = @_;
  168:     my $result;
  169:     return $result;
  170: }
  171: 
  172: #-- <html> tag (end tag optional)
  173: sub start_html {
  174:     my ($target,$token) = @_;
  175:     my $currentstring = '';
  176:     if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
  177: 	# start_body() takes care of emitting the <html> 
  178:     } elsif ($target eq 'tex') {
  179: 
  180: 	$currentstring .= &latex_header();
  181:     }
  182:     return $currentstring;
  183: }
  184: 
  185: sub end_html {
  186:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  187:     my $currentstring = '';
  188:     if ($target eq 'web' || $target eq 'webgrade') {
  189: 	# end_body takes care of the </html>
  190:     }
  191:     return $currentstring;
  192: }
  193: 
  194: #-- <head> tag (end tag optional)
  195: sub start_head {
  196:     my ($target,$token) = @_;
  197:     my $currentstring = '';
  198:     if ($target eq 'web' || $target eq 'webgrade') {
  199: 	&Apache::lonxml::startredirection();
  200:     } 
  201:     return $currentstring;
  202: }
  203: 
  204: sub end_head {
  205:     my ($target,$token) = @_;
  206:     my $currentstring = '';
  207:     if (($target eq 'web'      && $env{'request.state'} eq 'published') ||
  208: 	($target eq 'webgrade' && $env{'request.state'} eq 'published')) {
  209: 	# in case there is a </head> but no <head>
  210: 	if ($Apache::lonxml::redirection) {
  211: 	    $Apache::londefdef::head = &Apache::lonxml::endredirection();
  212: 	}
  213:     } 
  214:     return $currentstring;
  215: }
  216: 
  217: #-- <map> tag (end tag required)
  218: sub start_map {
  219:     my ($target,$token) = @_;
  220:     my $currentstring = '';
  221:     if ($target eq 'web' || $target eq 'webgrade') {
  222: 	$currentstring = $token->[4];     
  223:     } 
  224:     return $currentstring;
  225: }
  226: 
  227: sub end_map {
  228:     my ($target,$token) = @_;
  229:     my $currentstring = '';
  230:     if ($target eq 'web' || $target eq 'webgrade') {
  231: 	$currentstring = $token->[2];    
  232:     } 
  233:     return $currentstring;
  234: }
  235: 
  236: #-- <select> tag (end tag required)
  237: sub start_select {
  238:     my ($target,$token) = @_;
  239:     my $currentstring = '';
  240:     if ($target eq 'web' || $target eq 'webgrade') {
  241: 	$currentstring = $token->[4];     
  242:     }  elsif ($target eq 'tex') {
  243: 	$Apache::londefdef::select=0;
  244:     }
  245:     return $currentstring;
  246: }
  247: 
  248: sub end_select {
  249:     my ($target,$token) = @_;
  250:     my $currentstring = '';
  251:     if ($target eq 'web' || $target eq 'webgrade') {
  252: 	$currentstring = $token->[2];    
  253:     } 
  254:     return $currentstring;
  255: }
  256: 
  257: #-- <option> tag (end tag optional)
  258: sub start_option {
  259:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  260:     my $currentstring = '';
  261:     if ($target eq 'web' || $target eq 'webgrade') {
  262: 	$currentstring = $token->[4];     
  263:     } elsif ($target eq 'tex') {
  264: 	$Apache::londefdef::select++;
  265: 	if ($Apache::londefdef::select == 1) {
  266: 	    $currentstring='\noindent\fbox{'.&Apache::lonxml::get_param('value',$parstack,$safeeval).'}\keephidden{';
  267: 	} else {
  268: 	    $currentstring='\keephidden{';
  269: 	}
  270:     }
  271:     return $currentstring;
  272: }
  273: 
  274: sub end_option {
  275:     my ($target,$token) = @_;
  276:     my $currentstring = '';
  277:     if ($target eq 'web' || $target eq 'webgrade') {
  278: 	$currentstring = $token->[2];    
  279:     }  elsif ($target eq 'tex') {
  280: 	$currentstring='}';
  281:     }
  282:     return $currentstring;
  283: }
  284: 
  285: #-- <input> tag (end tag forbidden)
  286: sub start_input {
  287:     my ($target,$token) = @_;
  288:     my $currentstring = '';
  289:     if ($target eq 'web' || $target eq 'webgrade') {
  290: 	$currentstring = $token->[4];     
  291:     } 
  292:     return $currentstring;
  293: }
  294: 
  295: sub end_input {
  296:     my ($target,$token) = @_;
  297:     my $currentstring = '';
  298:     if ($target eq 'web' || $target eq 'webgrade') {
  299: 	$currentstring = $token->[2];    
  300:     } 
  301:     return $currentstring;
  302: }
  303: 
  304: #-- <textarea> tag (end tag required)
  305: sub start_textarea {
  306:     my ($target,$token) = @_;
  307:     my $currentstring = '';
  308:     if ($target eq 'web' || $target eq 'webgrade') {
  309: 	$currentstring = $token->[4];     
  310:     } 
  311:     return $currentstring;
  312: }
  313: 
  314: sub end_textarea {
  315:     my ($target,$token) = @_;
  316:     my $currentstring = '';
  317:     if ($target eq 'web' || $target eq 'webgrade') {
  318: 	$currentstring = $token->[2];    
  319:     } 
  320:     return $currentstring;
  321: }
  322: 
  323: #-- <form> tag (end tag required)
  324: sub start_form {
  325:     my ($target,$token) = @_;
  326:     my $currentstring = '';
  327:     if ($target eq 'web' || $target eq 'webgrade') {
  328: 	$currentstring = $token->[4];     
  329:     } 
  330:     return $currentstring;
  331: }
  332: 
  333: sub end_form {
  334:     my ($target,$token) = @_;
  335:     my $currentstring = '';
  336:     if ($target eq 'web' || $target eq 'webgrade') {
  337: 	$currentstring = $token->[2];    
  338:     } 
  339:     return $currentstring;
  340: }
  341: 
  342: #-- <title> tag (end tag required)
  343: sub start_title {
  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  345:     my $currentstring = '';
  346:     if ($target eq 'web' || $target eq 'webgrade') {
  347: 	$Apache::londefdef::title = 
  348: 	    &Apache::lonxml::get_all_text('/title',$parser,$style);
  349:     } elsif ($target eq 'tex') {
  350: 	$currentstring .= '\keephidden{Title of the document:  ' 
  351:     }
  352:     if ($target eq 'meta') {
  353: 	$currentstring='<title>';
  354: 	&start_output($target);
  355:     }
  356:     return $currentstring;
  357: }
  358: 
  359: sub end_title {
  360:     my ($target,$token) = @_;
  361:     my $currentstring = '';
  362:     if ($target eq 'web' || $target eq 'webgrade') {
  363: 	# start_title takes care of swallowing the title
  364:     } elsif ($target eq 'tex') {
  365: 	$currentstring .= '}';
  366:     }  
  367:     if ($target eq 'meta') {
  368: 	&end_output($target);
  369: 	$currentstring='</title>';
  370:     } 
  371:     return $currentstring;
  372: }
  373: 
  374: #-- <meta> tag (end tag forbidden)
  375: sub start_meta {
  376:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  377:     my $currentstring = '';
  378:     if ($target eq 'web' || $target eq 'webgrade') {
  379: 	my $args='';
  380: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  381: 	if ($args eq '') {
  382: 	    &Apache::lonxml::get_all_text("/meta",$parser,$style);
  383: 	} else {
  384: 	    $currentstring = $token->[4];
  385: 	}
  386:     } elsif ($target eq 'meta') {
  387: 	unless (&Apache::lonxml::get_param
  388: 		('http-equiv',$parstack,$safeeval,undef,1)) {
  389: 	    my $name=$token->[2]->{'name'};
  390: 	    $name=~tr/A-Z/a-z/;
  391: 	    $name=~s/\s/\_/gs;
  392: 	    $name=~s/\W//gs;
  393: 	    if ($name) {
  394: 		$currentstring='<'.$name;
  395:                  my $display=&Apache::lonxml::get_param
  396: 		('display',$parstack,$safeeval,undef,1);
  397:                 if ($display) {
  398:                     $display=~s/\"/\'/g;
  399: 		    $currentstring.=' display="'.$display.'"';
  400:                 }
  401: 		$currentstring.='>'.
  402: 		    &Apache::lonxml::get_param
  403: 			('content',$parstack,$safeeval,undef,1).
  404: 			'</'.$name.'>';
  405: 	    }
  406:             my $display=&Apache::lonxml::get_param
  407: 		('display',$parstack,$safeeval,undef,1);
  408:             if ($display) {
  409: 		$display=&HTML::Entities::encode($display,'<>&"');
  410: 		$currentstring.='<'.$name.'.display>'.$display.
  411:                                '</'.$name.'.display>';
  412:             }
  413: 	}
  414:     } elsif ($target eq 'tex') {
  415: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  416: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  417: 	if ((not defined $content) && (not defined $name)) {
  418: 	    &Apache::lonxml::startredirection();
  419: 	}
  420:     } elsif ($target eq 'edit') {
  421: 	$currentstring .= &Apache::edit::tag_start($target,$token);
  422: 	$currentstring .= &Apache::edit::text_arg('Name:','name',$token,30);
  423: 	$currentstring .= &Apache::edit::text_arg('Content:','content',$token,70);
  424: 	$currentstring .= &Apache::edit::end_row();
  425:     } elsif ($target eq 'modified') {
  426: 	my $constructtag =
  427: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
  428: 					'name','content');
  429: 	if ($constructtag) { $currentstring = &Apache::edit::rebuild_tag($token); }
  430:     }
  431:     return $currentstring;
  432: }
  433: 
  434: sub end_meta {
  435:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  436:     my $currentstring = '';
  437:     if ($target eq 'web' || $target eq 'webgrade') {
  438: 	my $args='';
  439: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  440: 	if ($args ne '') {
  441: 	    $currentstring = $token->[4];
  442: 	}
  443:     } elsif ($target eq 'tex') {
  444: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  445: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  446: 	if ((not defined $content) && (not defined $name)) {
  447: 	    &Apache::lonxml::endredirection();
  448: 	}
  449:     }
  450:     return $currentstring;
  451: }
  452: 
  453: sub insert_meta {
  454:     return '
  455:     <meta />';
  456: }
  457: 
  458: # accessrule
  459: sub start_accessrule {
  460:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  461:     my $currentstring = '';
  462:     my $eff  =&Apache::lonxml::get_param('effect',$parstack,$safeeval,undef,1);
  463:     my $realm=&Apache::lonxml::get_param('realm', $parstack,$safeeval,undef,1);
  464:     my $role =&Apache::lonxml::get_param('role',  $parstack,$safeeval,undef,1);
  465:     my $type =&Apache::lonxml::get_param('type',  $parstack,$safeeval,undef,1);
  466: 
  467:     my ($dom,$crs,$sec,$separator);
  468:     if ($type eq 'user') {
  469: 	($dom,$crs,$sec)=split(m{/},$realm);
  470: 	$crs = &LONCAPA::clean_username($crs);
  471: 	$separator = '/';
  472:     } else {
  473: 	($dom,$crs,$sec)=split(/\_/,$realm);
  474: 	$crs = &LONCAPA::clean_courseid($crs);
  475: 	$separator = '_';
  476:     }
  477:     $dom = &LONCAPA::clean_domain($dom);
  478: 
  479:     $sec =~s/\W//;
  480:     $realm = $dom;
  481:     if ($crs =~ /\S/) { $realm .= $separator.$crs; }
  482:     if ($sec =~ /\S/) { $realm .= $separator.$sec; }
  483:     $role=~s/\W//g;
  484: 
  485:     if ($target eq 'web') {
  486: 	my $args='';
  487: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  488: 	if ($args eq '') {
  489: 	    &Apache::lonxml::get_all_text("/accessrule",$parser,$style);
  490: 	} else {
  491: 	    $currentstring = $token->[4];
  492: 	}
  493:     }
  494:     if ($target eq 'meta') {
  495: 	$currentstring='<rule>'.$eff.':'.$realm.':'.$role.':'.$type.'</rule>';
  496:     }
  497:     return $currentstring;
  498: }
  499: 
  500: sub end_accessrule {
  501:     my ($target,$token,$tagstack,$parstack,$parser) = @_;
  502:     my $currentstring = '';
  503:     if ($target eq 'web') {
  504: 	my $args='';
  505: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  506: 	if ($args ne '') {
  507: 	    $currentstring = $token->[4];
  508: 	}
  509:     } 
  510:     return $currentstring;
  511: }
  512: 
  513: sub generate_css_links {
  514:     my $links;
  515:     my $css_href = &Apache::lonnet::EXT('resource.0.cssfile');
  516:     if ($css_href =~ /\S/) {
  517: 	&Apache::lonxml::extlink($css_href);
  518: 	$links .= 
  519: 	    '<link rel="stylesheet" type="text/css" href="'.$css_href.'" />';
  520:     }
  521:     return $links;
  522: }
  523: 
  524: #-- <body> tag (end tag required)
  525: sub start_body {
  526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  527:     my $currentstring = '';
  528: 
  529:     if ($target eq 'web' || $target eq 'webgrade') {
  530: 	if ($Apache::lonhomework::parsing_a_problem) {
  531: 	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
  532: 	    return '';
  533: 	}
  534: 	
  535: 	if (&is_inside_of($tagstack, "head")) {
  536: 	    &end_head(@_);
  537: 	}
  538: 	
  539: 	my $extra_head = &generate_css_links();
  540: 
  541:     # Breadcrumbs
  542:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  543:     if ($env{'request.state'} eq 'construct') {
  544:         &Apache::lonhtmlcommon::add_breadcrumb({
  545:             'text'  => 'Construction Space',
  546:             'href'  => &Apache::loncommon::authorspace(),
  547:         });
  548:         &Apache::lonhtmlcommon::add_breadcrumb({
  549:             'text'  => 'HTML Editor',
  550:             'href'  => '',
  551:         });
  552:         # breadcrumbs (and tools) will be created 
  553:         # in start_page->bodytag->innerregister
  554:     } else {
  555:         # FIXME Where are we?
  556:     }
  557: 
  558: 	$currentstring = 
  559: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
  560: 					   $Apache::londefdef::head
  561: 					      .$extra_head,
  562: 					   {'add_entries'    => $token->[2],
  563: #					    'no_title'       => 1,
  564: 					    'force_register' => 1});
  565: 
  566:         my $header = '';
  567:         if ($env{'request.state'} ne 'published' &&
  568:             $env{'request.state'} ne 'construct') {
  569:             $header=&Apache::lonmenu::constspaceform();
  570:         }
  571:         if ($env{'request.state'} ne 'published') {
  572:             $header.=&Apache::londefdef::edit_controls();
  573:         }
  574:         if ($env{'request.state'} eq 'construct') {
  575:             $currentstring.=&Apache::loncommon::head_subbox(
  576:                                 &Apache::loncommon::CSTR_pageheader()
  577:                                .$header);
  578:         } elsif ($env{'request.state'} eq 'edit') {
  579:             $currentstring.=&Apache::loncommon::head_subbox($header);
  580:         }
  581:         $currentstring.=&Apache::lonxml::message_location();
  582:     } elsif ($target eq 'tex') {
  583:         $currentstring = '';   #  '\begin{document}' is in header.
  584:     }
  585: 
  586:     return $currentstring;
  587: }
  588: 
  589: sub edit_controls {
  590:     my ($nochgview) = @_;
  591:     my $result .= '
  592: <form method="post">
  593: <div class="LC_edit_problem_header">';
  594:     unless ($nochgview) {
  595:         $result .= '
  596: <div class="LC_edit_problem_header_row1">'.
  597: &Apache::lonxml::renderingoptions().'
  598: <input type="submit" name="changeproblemmode" value="'.&mt('Change View').'" />
  599: </div>';
  600:     }
  601:     $result .= '
  602: <div><input type="submit" name="editmode" accesskey="e" value="'.&mt('Edit').'" />';
  603:     if (($env{'request.course.id'}) && ($env{'form.forceedit'})) {
  604:         $result .= ('&nbsp;' x 3).'<input type="button" value="'.&mt('Course View').'" onclick="javascript:location.href=currentURL" />';
  605:     }
  606:     $result .= '</div>
  607: </div>
  608: </form>
  609: ';
  610:     return $result;
  611: }
  612: 
  613: sub end_body {
  614:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  615:     my $currentstring = &end_p();	# Close off unclosed <p>
  616:     if ($target eq 'web' || $target eq 'webgrade') {
  617: 	$currentstring .= &Apache::loncommon::end_page({'discussion' => 1});
  618:     } elsif ($target eq 'tex') {
  619: 	$currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
  620:     } 
  621:     return $currentstring;
  622: }
  623: 
  624: # \begin{center} causes a new paragprah spacing that looks odd inside 
  625: # of a table cell.  Same at the end of a \center but with a slightly
  626: # larger space .. hence center_correction and center_end_correction.
  627: #
  628: sub center_correction { return '\vspace*{-6 mm}'; } 
  629: sub center_end_correction { return '\vspace*{-7 mm}'; }
  630: 
  631: #-- <center> tag (end tag required)
  632: sub start_center {
  633:     my ($target,$token,$tagstack) = @_;
  634:     my $currentstring = &end_p();	# Close off any prior para.
  635:     if ($target eq 'web' || $target eq 'webgrade') {
  636: 	$currentstring .= $token->[4];     
  637:     } elsif ($target eq 'tex') {
  638: 	if (&is_inside_of($tagstack, "table")) {
  639: 	    $currentstring .= &center_correction();
  640: 	}
  641: 	$currentstring .= '\begin{center}';  
  642:     }
  643:     return $currentstring;
  644: }
  645: 
  646: sub end_center {
  647:     my ($target,$token,$tagstack) = @_;
  648:     my $currentstring = '';
  649:     if ($target eq 'web' || $target eq 'webgrade') {
  650: 	$currentstring = $token->[2];     
  651:     } elsif ($target eq 'tex') {
  652: 	$currentstring = '\end{center}';  
  653: 	if (&is_inside_of($tagstack, "table")) {
  654: 	    $currentstring .= &center_end_correction();
  655: 	}
  656:     }
  657:     return $currentstring;
  658: }
  659: 
  660: #-- <b> tag (end tag required)
  661: #      NOTE: In TeX mode disables internal <p>
  662: sub start_b {
  663:     my ($target,$token) = @_;
  664:     my $currentstring = '';
  665:     if ($target eq 'web' || $target eq 'webgrade') {
  666: 	$currentstring = $token->[4];     
  667:     } elsif ($target eq 'tex') {
  668: 	&disable_para();
  669: 	$currentstring .= '\textbf{';  
  670:     } 
  671:     return $currentstring;
  672: }
  673: 
  674: sub end_b {
  675:     my ($target,$token) = @_;
  676:     my $currentstring = '';
  677:     if ($target eq 'web' || $target eq 'webgrade') {
  678: 	$currentstring = $token->[2];     
  679:     } elsif ($target eq 'tex') {
  680: 	&enable_para();
  681: 	$currentstring = '}';
  682:     } 
  683:     return $currentstring;
  684: }
  685: 
  686: #-- <strong> tag (end tag required)
  687: #    NOTE: in TeX mode disables internal <p>
  688: sub start_strong {
  689:     my ($target,$token) = @_;
  690:     my $currentstring = '';
  691:     if ($target eq 'web' || $target eq 'webgrade') {
  692: 	$currentstring = $token->[4];     
  693:     } elsif ($target eq 'tex') {
  694: 	&disable_para();
  695: 	$currentstring = '\textbf{';  
  696:     } 
  697:     return $currentstring;
  698: }
  699: 
  700: sub end_strong {
  701:     my ($target,$token) = @_;
  702:     my $currentstring = '';
  703:     if ($target eq 'web' || $target eq 'webgrade') {	
  704: 	$currentstring = $token->[2];     
  705:     } elsif ($target eq 'tex') {
  706: 	&enable_para();
  707: 	$currentstring = '}';  
  708:     }
  709:     return $currentstring;
  710: }
  711: 
  712: #-- <h1> tag (end tag required)
  713: sub start_h1 {
  714:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  715:     my $currentstring = &end_p();	# Close off any prior para.
  716:     if ($target eq 'web' || $target eq 'webgrade') {
  717: 	$currentstring .= $token->[4];
  718:     } elsif ($target eq 'tex') {
  719: 	my $pre;
  720: 	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
  721: 	if ($align eq 'center') {
  722: 	    $pre='\begin{center}';
  723: 	} elsif ($align eq 'left') {
  724: 	    $pre='\rlap{';
  725: 	} elsif ($align eq 'right') {
  726: 	    $pre=' \hfill \llap{';
  727: 	}
  728: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  729: 	if (not defined $TeXsize) {$TeXsize="large";}
  730: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  731:     } elsif ($target eq 'meta') {
  732: 	$currentstring.='<subject>';
  733: 	&start_output($target);
  734:     }
  735:     return $currentstring;
  736: }
  737: 
  738: sub end_h1 {
  739:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  740:     my $currentstring = '';
  741:     if ($target eq 'web' || $target eq 'webgrade') {
  742: 	$currentstring .= $token->[2];
  743:     } elsif ($target eq 'tex') {
  744: 	my $post='\vskip 0 mm ';
  745: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  746: 	if ($align eq 'center') {
  747: 	    $post='\end{center}';
  748: 	} elsif ($align eq 'left') {
  749: 	    $post='} \hfill'.'\vskip 0 mm ';
  750: 	} elsif ($align eq 'right') {
  751: 	    $post='}'.'\vskip 0 mm ';
  752: 	}
  753: 	$currentstring .= '}}'.$post;
  754:     } elsif ($target eq 'meta') {
  755: 	&end_output($target);
  756: 	$currentstring='</subject>';
  757:     } 
  758:     return $currentstring;
  759: }
  760: 
  761: #-- <h2> tag
  762: sub start_h2 {
  763:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  764:     my $currentstring = &end_p();	# Close off any prior para.
  765:     if ($target eq 'web' || $target eq 'webgrade') {
  766: 	$currentstring .= $token->[4];
  767:     } elsif ($target eq 'tex') {
  768: 	my $pre;
  769: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  770: 	if ($align eq 'center') {
  771: 	    $pre='\begin{center}';
  772: 	} elsif ($align eq 'left') {
  773: 	    $pre='\rlap{';
  774: 	} elsif ($align eq 'right') {
  775: 	    $pre=' \hfill \llap{';
  776: 	}
  777: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  778: 	if (not defined $TeXsize) {$TeXsize="large";}
  779: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  780:     } 
  781:     return $currentstring;
  782: }
  783: 
  784: sub end_h2 {
  785:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  786:     my $currentstring = '';
  787:     if ($target eq 'web' || $target eq 'webgrade') {
  788: 	$currentstring .= $token->[2];
  789:     } elsif ($target eq 'tex') {
  790: 	my $post='\vskip 0 mm ';
  791: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  792: 	if ($align eq 'center') {
  793: 	    $post='\end{center}';
  794: 	} elsif ($align eq 'left') {
  795: 	    $post='} \hfill'.'\vskip 0 mm ';
  796: 	} elsif ($align eq 'right') {
  797: 	    $post='}'.'\vskip 0 mm ';
  798: 	}
  799: 	$currentstring .= '}}'.$post;
  800:     } 
  801:     return $currentstring;
  802: }
  803: 
  804: #-- <h3> tag
  805: sub start_h3 {
  806:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  807:     my $currentstring = &end_p();	# Close off any prior para.
  808:     if ($target eq 'web' || $target eq 'webgrade') {
  809: 	$currentstring .= $token->[4];
  810:     } elsif ($target eq 'tex') {
  811: 	my $pre;
  812: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  813: 	if ($align eq 'center') {
  814: 	    $pre='\begin{center}';
  815: 	} elsif ($align eq 'left') {
  816: 	    $pre='\rlap{';
  817: 	} elsif ($align eq 'right') {
  818: 	    $pre=' \hfill \llap{';
  819: 	}
  820: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  821: 	if (not defined $TeXsize) {$TeXsize="large";}
  822: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  823:     } 
  824:     return $currentstring;
  825: }
  826: 
  827: sub end_h3 {
  828:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  829:     my $currentstring = '';
  830:     if ($target eq 'web' || $target eq 'webgrade') {
  831: 	$currentstring .= $token->[2];
  832:     } elsif ($target eq 'tex') {
  833: 	my $post='\vskip 0 mm ';
  834: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  835: 	if ($align eq 'center') {
  836: 	    $post='\end{center}';
  837: 	} elsif ($align eq 'left') {
  838: 	    $post='} \hfill'.'\vskip 0 mm ';
  839: 	} elsif ($align eq 'right') {
  840: 	    $post='}'.'\vskip 0 mm ';
  841: 	}
  842: 	$currentstring .= '}}'.$post;
  843:     } 
  844:     return $currentstring;
  845: }
  846: 
  847: #-- <h4> tag
  848: sub start_h4 {
  849:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  850:     my $currentstring = &end_p();	# Close off any prior para.
  851:     if ($target eq 'web' || $target eq 'webgrade') {
  852: 	$currentstring .= $token->[4];
  853:     } elsif ($target eq 'tex') {
  854: 	my $pre;
  855: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  856: 	if ($align eq 'center') {
  857: 	    $pre='\begin{center}';
  858: 	} elsif ($align eq 'left') {
  859: 	    $pre='\rlap{';
  860: 	} elsif ($align eq 'right') {
  861: 	    $pre=' \hfill \llap{';
  862: 	}
  863: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  864: 	if (not defined $TeXsize) {$TeXsize="large";}
  865: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  866:     } 
  867:     return $currentstring;
  868: }
  869: 
  870: sub end_h4 {
  871:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  872:     my $currentstring = '';
  873:     if ($target eq 'web' || $target eq 'webgrade') {
  874: 	$currentstring .= $token->[2];
  875:     } elsif ($target eq 'tex') {
  876: 	my $post='\vskip 0 mm ';
  877: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  878: 	if ($align eq 'center') {
  879: 	    $post='\end{center}';
  880: 	} elsif ($align eq 'left') {
  881: 	    $post='} \hfill'.'\vskip 0 mm ';
  882: 	} elsif ($align eq 'right') {
  883: 	    $post='}'.'\vskip 0 mm ';
  884: 	}
  885: 	$currentstring .= '}}'.$post;
  886:     } 
  887:     return $currentstring;
  888: }
  889: 
  890: #-- <h5> tag
  891: sub start_h5 {
  892:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  893:     my $currentstring = &end_p();	# Close off any prior paras.
  894:     if ($target eq 'web' || $target eq 'webgrade') {
  895: 	$currentstring .= $token->[4];
  896:     } elsif ($target eq 'tex') {
  897: 	my $pre;
  898: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  899: 	if ($align eq 'center') {
  900: 	    $pre='\begin{center}';
  901: 	} elsif ($align eq 'left') {
  902: 	    $pre='\rlap{';
  903: 	} elsif ($align eq 'right') {
  904: 	    $pre=' \hfill \llap{';
  905: 	}
  906: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  907: 	if (not defined $TeXsize) {$TeXsize="large";}
  908: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  909:     } 
  910:     return $currentstring;
  911: }
  912: 
  913: sub end_h5 {
  914:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  915:     my $currentstring = '';
  916:     if ($target eq 'web' || $target eq 'webgrade') {
  917: 	$currentstring .= $token->[2];
  918:     } elsif ($target eq 'tex') {
  919: 	my $post='\vskip 0 mm ';
  920: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  921: 	if ($align eq 'center') {
  922: 	    $post='\end{center}';
  923: 	} elsif ($align eq 'left') {
  924: 	    $post='} \hfill'.'\vskip 0 mm ';
  925: 	} elsif ($align eq 'right') {
  926: 	    $post='}'.'\vskip 0 mm ';
  927: 	}
  928: 	$currentstring .= '}}'.$post;
  929:     } 
  930:     return $currentstring;
  931: }
  932: 
  933: #-- <h6> tag
  934: sub start_h6 {
  935:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  936:     my $currentstring = &end_p();	# Close off any prior paras.
  937:     if ($target eq 'web' || $target eq 'webgrade') {
  938: 	$currentstring .= $token->[4];
  939:     } elsif ($target eq 'tex') {
  940: 	my $pre;
  941: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  942: 	if ($align eq 'center') {
  943: 	    $pre='\begin{center}';
  944: 	} elsif ($align eq 'left') {
  945: 	    $pre='\rlap{';
  946: 	} elsif ($align eq 'right') {
  947: 	    $pre=' \hfill \llap{';
  948: 	}
  949: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  950: 	if (not defined $TeXsize) {$TeXsize="large";}
  951: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  952:     } 
  953:     return $currentstring;
  954: }
  955: 
  956: sub end_h6 {
  957:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  958:     my $currentstring = '';
  959:     if ($target eq 'web' || $target eq 'webgrade') {
  960: 	$currentstring .= $token->[2];
  961:     } elsif ($target eq 'tex') {
  962: 	my $post='\vskip 0 mm ';
  963: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  964: 	if ($align eq 'center') {
  965: 	    $post='\end{center}';
  966: 	} elsif ($align eq 'left') {
  967: 	    $post='} \hfill'.'\vskip 0 mm ';
  968: 	} elsif ($align eq 'right') {
  969: 	    $post='}'.'\vskip 0 mm ';
  970: 	}
  971: 	$currentstring .= '}}'.$post;
  972:     } 
  973:     return $currentstring;
  974: }
  975: 
  976: #--- <cite> tag (end tag required)
  977: sub start_cite {
  978:     my ($target,$token) = @_;
  979:     my $currentstring = '';
  980:     if ($target eq 'web' || $target eq 'webgrade') {
  981: 	$currentstring .= $token->[4];
  982:     } elsif ($target eq 'tex') {
  983: 	$currentstring .= '\textit{';
  984:     }
  985:     return $currentstring;
  986: }
  987: 
  988: sub end_cite {
  989:     my ($target,$token) = @_;
  990:     my $currentstring = '';
  991:     if ($target eq 'web' || $target eq 'webgrade') {
  992: 	$currentstring .= $token->[2];
  993:     } elsif ($target eq 'tex') {
  994: 	$currentstring .= '}';
  995:     }
  996:     return $currentstring;
  997: }
  998: 
  999: #-- <i> tag (end tag required)
 1000: sub start_i {
 1001:     my ($target,$token) = @_;
 1002:     my $currentstring = '';
 1003:     if ($target eq 'web' || $target eq 'webgrade') {
 1004: 	$currentstring .= $token->[4];
 1005:     } elsif ($target eq 'tex') {
 1006: 	$currentstring .= '\textit{';
 1007:     }
 1008:     return $currentstring;
 1009: }
 1010: 
 1011: sub end_i {
 1012:     my ($target,$token) = @_;
 1013:     my $currentstring = '';
 1014:     if ($target eq 'web' || $target eq 'webgrade') {
 1015: 	$currentstring .= $token->[2];
 1016:     } elsif ($target eq 'tex') {
 1017: 	$currentstring .= '}';
 1018:     } 
 1019:     return $currentstring;
 1020: }
 1021: 
 1022: #-- <address> tag (end tag required)
 1023: sub start_address {
 1024:     my ($target,$token) = @_;
 1025:     my $currentstring = '';
 1026:     if ($target eq 'web' || $target eq 'webgrade') {
 1027: 	$currentstring .= $token->[4];
 1028:     } elsif ($target eq 'tex') {
 1029: 	$currentstring .= '\textit{';
 1030:     }
 1031:     return $currentstring;
 1032: }
 1033: 
 1034: sub end_address {
 1035:     my ($target,$token) = @_;
 1036:     my $currentstring = '';
 1037:     if ($target eq 'web' || $target eq 'webgrade') {
 1038: 	$currentstring .= $token->[2];
 1039:     } elsif ($target eq 'tex') {
 1040: 	$currentstring .= '}';
 1041:     }
 1042:     return $currentstring;
 1043: }
 1044: 
 1045: #-- <dfn> tag (end tag required)
 1046: sub start_dfn {
 1047:     my ($target,$token) = @_;
 1048:     my $currentstring = '';
 1049:     if ($target eq 'web' || $target eq 'webgrade') {
 1050: 	$currentstring .= $token->[4];
 1051:     } elsif ($target eq 'tex') {
 1052: 	$currentstring .= '\textit{';
 1053:     } 
 1054:     return $currentstring;
 1055: }
 1056: 
 1057: sub end_dfn {
 1058:     my ($target,$token) = @_;
 1059:     my $currentstring = '';
 1060:     if ($target eq 'web' || $target eq 'webgrade') {
 1061: 	$currentstring .= $token->[2];
 1062:     } elsif ($target eq 'tex') {
 1063: 	$currentstring .= '}';
 1064:     }
 1065:     return $currentstring;
 1066: }
 1067: 
 1068: #-- <tt> tag (end tag required)
 1069: sub start_tt {
 1070:     my ($target,$token) = @_;
 1071:     my $currentstring = '';
 1072:     if ($target eq 'web' || $target eq 'webgrade') {
 1073: 	$currentstring .= $token->[4];
 1074:     } elsif ($target eq 'tex') {
 1075: 	$currentstring .= '\texttt{';
 1076:     }
 1077:     return $currentstring;
 1078: }
 1079: 
 1080: sub end_tt {
 1081:     my ($target,$token) = @_;
 1082:     my $currentstring = '';
 1083:     if ($target eq 'web' || $target eq 'webgrade') {
 1084: 	$currentstring .= $token->[2];
 1085:     } elsif ($target eq 'tex') {
 1086: 	$currentstring .= '}';
 1087:     }
 1088:     return $currentstring;
 1089: }
 1090: 
 1091: #-- <kbd> tag (end tag required)
 1092: sub start_kbd {
 1093:     my ($target,$token) = @_;
 1094:     my $currentstring = '';
 1095:     if ($target eq 'web' || $target eq 'webgrade') {
 1096: 	$currentstring .= $token->[4];
 1097:     } elsif ($target eq 'tex') {
 1098: 	$currentstring .= '\texttt{';
 1099:     }
 1100:     return $currentstring;
 1101: }
 1102: 
 1103: sub end_kbd {
 1104:     my ($target,$token) = @_;
 1105:     my $currentstring = '';
 1106:     if ($target eq 'web' || $target eq 'webgrade') {
 1107: 	$currentstring .= $token->[2];
 1108:     } elsif ($target eq 'tex') {
 1109: 	$currentstring .= '}';
 1110:     }
 1111:     return $currentstring;
 1112: }
 1113: 
 1114: #-- <code> tag (end tag required)
 1115: sub start_code {
 1116:     my ($target,$token) = @_;
 1117:     my $currentstring = '';
 1118:     if ($target eq 'web' || $target eq 'webgrade') {
 1119: 	$currentstring .= $token->[4];
 1120:     } elsif ($target eq 'tex') {
 1121: 	$currentstring .= '\texttt{';
 1122:     } 
 1123:     return $currentstring;
 1124: }
 1125: 
 1126: sub end_code {
 1127:     my ($target,$token) = @_;
 1128:     my $currentstring = '';
 1129:     if ($target eq 'web' || $target eq 'webgrade') {
 1130: 	$currentstring .= $token->[2];
 1131:     } elsif ($target eq 'tex') {
 1132: 	$currentstring .= '}';
 1133:     } 
 1134:     return $currentstring;
 1135: }
 1136: 
 1137: #-- <em> tag (end tag required)
 1138: sub start_em {
 1139:     my ($target,$token) = @_;
 1140:     my $currentstring = '';
 1141:     if ($target eq 'web' || $target eq 'webgrade') {
 1142: 	$currentstring .= $token->[4];
 1143:     } elsif ($target eq 'tex') {
 1144: 	$currentstring .= '\emph{';
 1145:     }
 1146:     return $currentstring;
 1147: }
 1148: 
 1149: sub end_em {
 1150:     my ($target,$token) = @_;
 1151:     my $currentstring = '';
 1152:     if ($target eq 'web' || $target eq 'webgrade') {
 1153: 	$currentstring .= $token->[2];
 1154:     } elsif ($target eq 'tex') {
 1155: 	$currentstring .= '}';
 1156:     } 
 1157:     return $currentstring;
 1158: }
 1159: 
 1160: #-- <q> tag (end tag required)
 1161: sub start_q {
 1162:     my ($target,$token) = @_;
 1163:     my $currentstring = '';
 1164:     if ($target eq 'web' || $target eq 'webgrade') {
 1165: 	$currentstring .= $token->[4];
 1166:     } elsif ($target eq 'tex') {
 1167: 	$currentstring .= '\emph{';
 1168:     }
 1169:     return $currentstring;
 1170: }
 1171: 
 1172: sub end_q {
 1173:     my ($target,$token) = @_;
 1174:     my $currentstring = '';
 1175:     if ($target eq 'web' || $target eq 'webgrade') {
 1176: 	$currentstring .= $token->[2];
 1177:     } elsif ($target eq 'tex') {
 1178: 	$currentstring .= '}';
 1179:     } 
 1180:     return $currentstring;
 1181: }
 1182: 
 1183: #  <p> is a bit strange since it does not require a closing </p>
 1184: #  However in latex, we must often output closing stuff to end
 1185: #  environments and {}'s etc.  Therefore we do all the work
 1186: #  of figuring out the ending strings in the start tag processing,
 1187: #  and provide a mechanism to output the stop text external
 1188: #  to tag processing.
 1189: #
 1190: {
 1191: 
 1192:     my $closing_string = '';		# String required to close <p>
 1193: 
 1194: #   Some tags are <p> fragile meaning that <p> inside of them
 1195: #   does not work within TeX mode.  This is managed via the 
 1196: #   counter below:
 1197: #
 1198: 
 1199:     my $para_disabled = 0;
 1200: 
 1201: sub disable_para {
 1202:     $para_disabled++;
 1203: }
 1204: sub enable_para {
 1205:     $para_disabled--;
 1206: }
 1207: 
 1208: 
 1209: #-- <p> tag (end tag optional)
 1210: #optional attribute - align="center|left|right"
 1211: sub start_p {
 1212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1213:     my $currentstring = '';
 1214:     if ($target eq 'web' || $target eq 'webgrade') {
 1215: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1216: 	$currentstring .= $token->[4];
 1217: 	if (! ($currentstring =~ /\//)) {
 1218: 	    $closing_string = '</p>'; # Deal correctly with <p /> e.g.
 1219: 	}
 1220:     } elsif ($target eq 'tex' && !$para_disabled) {
 1221: 
 1222: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1223: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1224: 	if ($align eq 'center') {
 1225: 	    $currentstring .='\begin{center}\par ';
 1226: 	    $closing_string = '\end{center}';
 1227: 	    if (&is_inside_of($tagstack, "table")) {
 1228: 		$currentstring = &center_correction().$currentstring;
 1229: 	    }
 1230: 	} elsif ($align eq 'right') {
 1231: 	    $currentstring.="\n".'{\flushright ';
 1232: #	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
 1233: 	    $closing_string= "}\n";
 1234: 	} elsif ($align eq 'left') {
 1235: 	    $currentstring.= "\n".'{\flushleft ';
 1236: #	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
 1237: 	    $closing_string = "}\n";
 1238: 	} else {
 1239:             $currentstring.='\par ';
 1240: 	    if (&is_inside_of($tagstack, 'table')) {
 1241: 		$closing_string = '\vskip 0pt'; # Seems to be consistent with <p> in tables.
 1242: 	    } else {
 1243: 		$closing_string = '\strut\\\\\strut ';
 1244: 	    }
 1245:         }
 1246: 
 1247:     }
 1248:     return $currentstring;
 1249: }
 1250: #
 1251: #  End paragraph processing just requires that we output the
 1252: #  closing string that was saved and blank it.
 1253: sub end_p {
 1254:     #  Note only 'tex' mode uses disable_para and enable_para
 1255:     #  so we don't need to know the target in the check below:
 1256: 
 1257:     if (!$para_disabled) {
 1258: 	my $current_string = $closing_string;
 1259: 	$closing_string = '';	# Not in a para anymore.
 1260: 	return $current_string;
 1261:     } else {
 1262: 	return '';
 1263:     }
 1264: 
 1265: }
 1266: }
 1267: #-- <br> tag (end tag forbidden)
 1268: sub start_br {
 1269:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1270:     my $currentstring = '';
 1271:     if ($target eq 'web' || $target eq 'webgrade') {
 1272: 	$currentstring .= $token->[4];
 1273:     } elsif ($target eq 'tex') {
 1274: 	my @tempo=@$tagstack;
 1275: 	my $signal=0;
 1276: 	#  Not going to factor this to is_inside_of since that would require
 1277:         #  multiple stack traversals.
 1278: 	#
 1279: 	for (my $i=$#tempo;$i>=0;$i--) {
 1280: 	    if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
 1281:                 ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul'))  {
 1282: 		$signal=1;
 1283: 	    }
 1284: 	    if (($tempo[$i] eq 'td') || ($tempo[$i] eq 'th')) {
 1285: 		$signal = 1;
 1286: 	    }
 1287: 	}
 1288: 	if ($signal != 1) {
 1289: 	    $currentstring .= '\strut \\\\ \strut ';
 1290: 	}
 1291:     
 1292:     }
 1293:     return $currentstring;
 1294: }
 1295: 
 1296: sub end_br {
 1297:     my ($target,$token) = @_;
 1298:     my $currentstring = '';
 1299:     if ($target eq 'web' || $target eq 'webgrade') {
 1300: 	$currentstring .= $token->[2];
 1301:     }
 1302:     return $currentstring;
 1303: }
 1304: 
 1305: #-- <big> tag (end tag required)
 1306: sub start_big {
 1307:     my ($target,$token) = @_;
 1308:     my $currentstring = '';
 1309:     if ($target eq 'web' || $target eq 'webgrade') {
 1310: 	$currentstring .= $token->[4];
 1311:     } elsif ($target eq 'tex') {
 1312: 	$currentstring .= '{\large ';
 1313:     } 
 1314:     return $currentstring;
 1315: }
 1316: 
 1317: sub end_big {
 1318:     my ($target,$token) = @_;
 1319:     my $currentstring = '';
 1320:     if ($target eq 'web' || $target eq 'webgrade') {
 1321: 	$currentstring .= $token->[2];
 1322:     } elsif ($target eq 'tex') {
 1323: 	$currentstring .= '}';
 1324:     }
 1325:     return $currentstring;
 1326: }
 1327: 
 1328: #-- <small> tag (end tag required)
 1329: sub start_small {
 1330:     my ($target,$token) = @_;
 1331:     my $currentstring = '';
 1332:     if ($target eq 'web' || $target eq 'webgrade') {
 1333: 	$currentstring .= $token->[4];
 1334:     } elsif ($target eq 'tex') {
 1335: 	$currentstring .= '{\footnotesize ';
 1336:     }
 1337:     return $currentstring;
 1338: }
 1339: 
 1340: sub end_small {
 1341:     my ($target,$token) = @_;
 1342:     my $currentstring = '';
 1343:     if ($target eq 'web' || $target eq 'webgrade') {
 1344: 	$currentstring .= $token->[2];
 1345:     } elsif ($target eq 'tex') {
 1346: 	$currentstring .= '}';
 1347:     }
 1348:     return $currentstring;
 1349: }
 1350: 
 1351: #-- <basefont> tag (end tag forbidden)
 1352: sub start_basefont {
 1353:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1354:     my $currentstring = '';
 1355:     if ($target eq 'web' || $target eq 'webgrade') {
 1356: 	$currentstring = $token->[4];     
 1357:     } elsif ($target eq 'tex') {
 1358: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1359: 	if (defined $basesize) {
 1360: 	    $currentstring = '{\\'.$basesize.' ';
 1361: 	}
 1362:     }
 1363:     return $currentstring;
 1364: }
 1365: 
 1366: sub end_basefont {
 1367:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1368:     my $currentstring = '';
 1369:     if ($target eq 'web' || $target eq 'webgrade') {
 1370: 	$currentstring = $token->[4];     
 1371:     } elsif ($target eq 'tex') {
 1372: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1373: 	if (defined $basesize) {
 1374: 	    $currentstring = '}';
 1375: 	}
 1376:     }
 1377:     return $currentstring;
 1378: }
 1379: 
 1380: #-- <font> tag (end tag required)
 1381: sub start_font {
 1382:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1383:     my $currentstring = '';
 1384:     if ($target eq 'web' || $target eq 'webgrade') {
 1385: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1386: 	$currentstring = $token->[4];     
 1387:     }  elsif ($target eq 'tex') {
 1388: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1389: 	if (defined $fontsize) {
 1390: 	    $currentstring = '{\\'.$fontsize.' ';
 1391: 	}
 1392:     }
 1393:     return $currentstring;
 1394: }
 1395: 
 1396: sub end_font {
 1397:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1398:     my $currentstring = '';
 1399:     if ($target eq 'web' || $target eq 'webgrade') {
 1400: 	$currentstring = $token->[2];    
 1401:     }  elsif ($target eq 'tex') {
 1402: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1403: 	if (defined $fontsize) {
 1404: 	    $currentstring = '}';
 1405: 	}
 1406:     }
 1407:     return $currentstring;
 1408: }
 1409:  
 1410: #-- <strike> tag (end tag required)
 1411: sub start_strike {
 1412:     my ($target,$token) = @_;
 1413:     my $currentstring = '';
 1414:     if ($target eq 'web' || $target eq 'webgrade') {
 1415: 	$currentstring .= $token->[4];
 1416:     } elsif ($target eq 'tex') {
 1417: 	&Apache::lonxml::startredirection();
 1418:     } 
 1419:     return $currentstring;
 1420: }
 1421: 
 1422: sub end_strike {
 1423:     my ($target,$token) = @_;
 1424:     my $currentstring = '';
 1425:     if ($target eq 'web' || $target eq 'webgrade') {
 1426: 	$currentstring .= $token->[2];
 1427:     } elsif ($target eq 'tex') {
 1428: 	$currentstring=&Apache::lonxml::endredirection();
 1429: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1430: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1431: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1432:     }
 1433:     return $currentstring;
 1434: }
 1435: 
 1436: #-- <s> tag (end tag required)
 1437: sub start_s {
 1438:     my ($target,$token) = @_;
 1439:     my $currentstring = '';
 1440:     if ($target eq 'web' || $target eq 'webgrade') {
 1441: 	$currentstring .= $token->[4];
 1442:     } elsif ($target eq 'tex') {
 1443: 	&Apache::lonxml::startredirection();
 1444:     } 
 1445:     return $currentstring;
 1446: }
 1447: 
 1448: sub end_s {
 1449:     my ($target,$token) = @_;
 1450:     my $currentstring = '';
 1451:     if ($target eq 'web' || $target eq 'webgrade') {
 1452: 	$currentstring .= $token->[2];
 1453:     } elsif ($target eq 'tex') {
 1454: 	$currentstring=&Apache::lonxml::endredirection();
 1455: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1456: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1457: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1458:     }
 1459:     return $currentstring;
 1460: }
 1461: 
 1462: #-- <sub> tag (end tag required)
 1463: sub start_sub {
 1464:     my ($target,$token) = @_;
 1465:     my $currentstring = '';
 1466:     if ($target eq 'web' || $target eq 'webgrade') {
 1467: 	$currentstring .= $token->[4];
 1468:     } elsif ($target eq 'tex') {
 1469: 	$currentstring .= '\raisebox{-\smallskipamount}{\scriptsize{';
 1470:     } 
 1471:     return $currentstring;
 1472: }
 1473: 
 1474: sub end_sub {
 1475:     my ($target,$token) = @_;
 1476:     my $currentstring = '';
 1477:     if ($target eq 'web' || $target eq 'webgrade') {
 1478: 	$currentstring .= $token->[2];
 1479:     } elsif ($target eq 'tex') {
 1480: 	$currentstring .= '}}';
 1481:     }
 1482:     return $currentstring;
 1483: }
 1484: 
 1485: #-- <sup> tag (end tag required)
 1486: sub start_sup {
 1487:     my ($target,$token) = @_;
 1488:     my $currentstring = '';
 1489:     if ($target eq 'web' || $target eq 'webgrade') {
 1490: 	$currentstring .= $token->[4];
 1491:     } elsif ($target eq 'tex') {
 1492: 	$currentstring .= '\raisebox{\smallskipamount}{\scriptsize{';
 1493:     } 
 1494:     return $currentstring;
 1495: }
 1496: 
 1497: sub end_sup {
 1498:     my ($target,$token) = @_;
 1499:     my $currentstring = '';
 1500:     if ($target eq 'web' || $target eq 'webgrade') {
 1501: 	$currentstring .= $token->[2];
 1502:     } elsif ($target eq 'tex') {
 1503: 	$currentstring .= '}}';
 1504:     }
 1505:     return $currentstring;
 1506: }
 1507: 
 1508: #-- <hr> tag (end tag forbidden)
 1509: sub start_hr {
 1510:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1511:     my $currentstring = &end_p();	# End enclosing para.
 1512:     if ($target eq 'web' || $target eq 'webgrade') {
 1513: 	$currentstring .= $token->[4];
 1514:     } elsif ($target eq 'tex') {
 1515: 
 1516: 	# <hr /> can't be inside of <sup><sub> thank you LaTeX.
 1517: 	# 
 1518: 	my $restart_sub = 0;
 1519: 	my $restart_sup = 0;
 1520: 
 1521: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 1522: 	# using the start_ stop_ functions.. those tags only care about
 1523: 	# $target.
 1524: 
 1525: 	if (&is_inside_of($tagstack, "sub")) {
 1526: 	    $restart_sub = 1;
 1527: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 1528: 				       $parstack, $parser, $safeeval);
 1529: 	}
 1530: 	if (&is_inside_of($tagstack, "sup")) {
 1531: 	    $restart_sup = 1;
 1532: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 1533: 				       $parstack, $parser, $safeeval);
 1534: 	}	
 1535: 
 1536: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1537: 	if (defined $LaTeXwidth) {
 1538: 	    if ($LaTeXwidth=~/^%/) {
 1539: 		substr($LaTeXwidth,0,1)='';
 1540: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1541: 	    }
 1542: 	} else {
 1543: 	    $LaTeXwidth ='0.9\textwidth';
 1544: 	}
 1545: 	my ($pre,$post);
 1546: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1547: 	if (($align eq 'center') || (not defined $align)) {
 1548: 	    $pre=''; $post='';
 1549: 	} elsif ($align eq 'left') {
 1550: 	    $pre='\rlap{'; $post='} \hfill';
 1551: 	} elsif ($align eq 'right') {
 1552: 	    $pre=' \hfill \llap{'; $post='}';
 1553: 	}
 1554: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1555:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1556: 	# Turn stuff back on that we can't be inside of.
 1557: 
 1558: 	if ($restart_sub) {
 1559: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 1560: 					$parstack, $parser, $safeeval);
 1561: 	}
 1562: 	if ($restart_sup) {
 1563: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 1564: 					 $parstack, $parser, $safeeval);
 1565: 	}	
 1566:     } 
 1567:     return $currentstring;
 1568: }
 1569: 
 1570: sub end_hr {
 1571:     my ($target,$token) = @_;
 1572:     my $currentstring = '';
 1573:     if ($target eq 'web' || $target eq 'webgrade') {
 1574: 	$currentstring .= $token->[2];
 1575:     }
 1576:     return $currentstring;
 1577: }
 1578: 
 1579: #-- <div> tag (end tag required)
 1580: {
 1581: 
 1582: #  Since div can be nested, the stack below is used
 1583: #  in 'tex' mode to store the ending strings
 1584: #  for the div stack.
 1585: 
 1586:     my @div_end_stack;
 1587: 
 1588: sub start_div {
 1589:     my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
 1590:     my $currentstring = &end_p();	# Close enclosing para.
 1591:     if ($target eq 'web' || $target eq 'webgrade') {
 1592: 	$currentstring .= $token->[4];
 1593:     } 
 1594:     if ($target eq 'tex') {
 1595: 	# 4 possible alignments: left, right, center, and -missing-.
 1596:         # If inside a table row, we must let the table logic
 1597: 	# do the alignment, however.
 1598: 	# 
 1599: 
 1600: 	my $endstring = '';
 1601: 
 1602: 	my $align = lc(&Apache::lonxml::get_param('align', $parstack,
 1603: 						  $safeeval, undef, 1));
 1604: 	if ($align eq 'center') {
 1605: 	    $currentstring .= '\begin{center}';
 1606: 	    $endstring      = '\end{center}';
 1607: 	    if (&is_inside_of($tagstack, "table")) {
 1608: 		$currentstring = &center_correction().$currentstring;
 1609: 		$endstring    .= &center_end_correction(); 
 1610: 	    }
 1611: 	}
 1612: 	elsif ($align eq 'right') {
 1613: 	    $currentstring .= '\begin{flushright}';
 1614: 	    $endstring     .= '\end{flushright}';
 1615: 	} elsif ($align eq 'left') {
 1616: 	    $currentstring .= '\begin{flushleft}';
 1617: 	    $endstring     = '\end{flushleft}';
 1618: 	} else {
 1619: 	
 1620: 	}
 1621: 	$currentstring .= "\n";   # For human readability.
 1622: 	$endstring       = "\n$endstring\n"; # For human readability
 1623: 	push(@div_end_stack, $endstring);
 1624:     }
 1625:     return $currentstring;
 1626: }
 1627: 
 1628: sub end_div {
 1629:     my ($target,$token) = @_;
 1630:     my $currentstring = '';
 1631:     if ($target eq 'web' || $target eq 'webgrade') {
 1632: 	$currentstring .= $token->[2];
 1633:     }
 1634:     if ($target eq 'tex') {
 1635: 	my $endstring = pop @div_end_stack;
 1636: 	$currentstring .= $endstring;
 1637:     }
 1638:     return $currentstring;
 1639: }
 1640: }
 1641: 
 1642: #-- <a> tag (end tag required)
 1643: sub start_a {
 1644:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1645:     my $currentstring = '';
 1646:     if ($target eq 'web' || $target eq 'webgrade') {
 1647: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 1648: 					    undef,1);
 1649: 	$currentstring=&Apache::lonenc::encrypt_ref($token,{'href'=>$href});
 1650:         if ($href =~ /\S/) {
 1651:             if ($href !~ m{^https?://}) {
 1652:                 my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
 1653:                 my $linkurl;
 1654:                 if ($href =~ m{^/uploaded/}) {
 1655:                     $linkurl = $href;
 1656:                 } elsif ($href =~ m{^[^/]}) {
 1657:                     my $path = $url;
 1658:                     $path  =~ s{[^/]*$}{};
 1659:                     $linkurl = $path.$href;
 1660:                 }
 1661:                 if ($linkurl =~ m{^/uploaded/}) {
 1662:                     if (!&Apache::lonnet::allowed('bre',$linkurl)) {
 1663:                         if (&Apache::lonnet::is_on_map($url)) {
 1664:                             &Apache::lonxml::extlink($linkurl);
 1665:                         }
 1666:                     }
 1667:                 }
 1668:             }
 1669:         }
 1670:     }
 1671:     return $currentstring;
 1672: }
 1673: 
 1674: sub end_a {
 1675:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1676:     my $currentstring = '';
 1677:     if ($target eq 'web' || $target eq 'webgrade') {
 1678: 	$currentstring .= $token->[2];
 1679:     }
 1680:     if ($target eq 'tex') {
 1681: 	my $href =
 1682: 	    &Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1683: 	my $name =
 1684: 	    &Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1685:         my $uriprint =
 1686:             &Apache::lonxml::get_param('uriprint',$parstack,$safeeval,undef,1);
 1687:         my $anchorprint =
 1688:             &Apache::lonxml::get_param('anchorprint',$parstack,$safeeval,undef,1);
 1689: 	if (($href =~ /\S/) && ($uriprint=~/^on|uriprint|yes|1$/i)) {
 1690: 	    $href =~ s/([^\\])%/$1\\\%/g;
 1691: 	    # Substitute special symbols... and allow line breaks at each /
 1692: 	    #
 1693: 	    $href = &Apache::lonxml::latex_special_symbols($href);
 1694: 	    $href =~ s/\//\/\\-/g;              # Map / to /\- to allow hyphenation.
 1695: 	    $currentstring .= ' ({\tt URI:'.$href.'})';
 1696: 	} elsif (($name =~ /\S/) && ($anchorprint=~/^on|anchorprint|yes|1$/i)) {
 1697: 	    $currentstring .= ' ({\tt Anchor:'.&Apache::lonxml::latex_special_symbols($name).'})';
 1698: 	} else {
 1699: 	    $currentstring.='';
 1700: 	}	
 1701:     }
 1702:     return $currentstring;
 1703: }
 1704: 
 1705: #-- <li> tag (end tag optional)
 1706: sub start_li {
 1707:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1708:     my $currentstring = '';
 1709:     if ($target eq 'web' || $target eq 'webgrade') {
 1710: 	$currentstring = $token->[4];     
 1711:     } elsif ($target eq 'tex') {
 1712: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1713: 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
 1714: 	#FIXME need to support types i and I 
 1715: 	if ($type=~/disc/) {
 1716: 	    $currentstring .= ' \item[$\bullet$] ';
 1717: 	} elsif ($type=~/circle/) {
 1718: 	    $currentstring .= ' \item[$\circ$] ';
 1719: 	} elsif ($type=~/square/) {
 1720: 	    $currentstring .= ' \item[$\diamond$] ';
 1721: 	} elsif ($type eq '1') {
 1722: 	    $currentstring .= ' \item['.($Apache::londefdef::list_index+1).'.]';
 1723: 	} elsif ($type eq 'A') {
 1724: 	    $currentstring .= ' \item['.('A'..'Z')[$Apache::londefdef::list_index].'.]';
 1725: 	} elsif ($type eq 'a') {
 1726: 	    $currentstring .= ' \item['.('a'..'z')[$Apache::londefdef::list_index].'.]';
 1727: 	} elsif ($value ne '') {
 1728: 	    $currentstring .= ' \item['.$value.'] ';
 1729: 	} else {
 1730: 	    $currentstring .= ' \item ';
 1731: 	}  
 1732: 	$Apache::londefdef::list_index++;
 1733:     }
 1734:     return $currentstring;
 1735: }
 1736: 
 1737: sub end_li {
 1738:     my ($target,$token) = @_;
 1739:     my $currentstring = &end_p();	# In case there's a <p> in the <li>
 1740:     if ($target eq 'web' || $target eq 'webgrade') {
 1741: 	$currentstring .= $token->[2];     
 1742:     } 
 1743:     return $currentstring;
 1744: }
 1745: 
 1746: #-- <u> tag (end tag required)
 1747: sub start_u {
 1748:     my ($target,$token) = @_;
 1749:     my $currentstring = '';
 1750:     if ($target eq 'web' || $target eq 'webgrade') {
 1751: 	$currentstring .= $token->[4];
 1752:     } elsif ($target eq 'tex') {
 1753: 	&Apache::lonxml::startredirection();
 1754:     } 
 1755:     return $currentstring;
 1756: }
 1757: 
 1758: sub end_u {
 1759:     my ($target,$token) = @_;
 1760:     my $currentstring = '';
 1761:     if ($target eq 'web' || $target eq 'webgrade') {
 1762: 	$currentstring .= $token->[2];
 1763:     } elsif ($target eq 'tex') {
 1764: 	$currentstring=&Apache::lonxml::endredirection();
 1765: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1766: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1767: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1768:     }
 1769:     return $currentstring;
 1770: }
 1771: 
 1772: #-- <ul> tag (end tag required)
 1773: sub start_ul {
 1774:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1775:     my $currentstring = &end_p();	# Close off enclosing list.
 1776:     if ($target eq 'web' || $target eq 'webgrade') {
 1777: 	$currentstring .= $token->[4];     
 1778:     } elsif ($target eq 'tex') {
 1779: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1780: 	$Apache::londefdef::list_index=0;
 1781: 	if ($TeXtype eq 'disc') {
 1782: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1783:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1784:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1785:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1786: 	} elsif ($TeXtype eq 'circle') {
 1787: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1788:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1789:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1790:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1791: 	} elsif ($TeXtype eq 'square') {
 1792: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1793:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1794:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1795:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1796: 	}
 1797: 	$currentstring .= '\strut \begin{itemize}';  
 1798:     } 
 1799:     return $currentstring;
 1800: }
 1801: 
 1802: sub end_ul {
 1803:     my ($target,$token) = @_;
 1804:     my $currentstring = '';
 1805:     if ($target eq 'web' || $target eq 'webgrade') {
 1806: 	$currentstring = $token->[2];     
 1807:     } elsif ($target eq 'tex') {
 1808: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1809:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1810:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1811:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1812:     } 
 1813:     return $currentstring;
 1814: }
 1815: 
 1816: #-- <menu> tag (end tag required)
 1817: sub start_menu {
 1818:     my ($target,$token) = @_;
 1819:     my $currentstring = '';
 1820:     if ($target eq 'web' || $target eq 'webgrade') {
 1821: 	$currentstring = $token->[4];     
 1822:     } elsif ($target eq 'tex') {
 1823: 	$currentstring = " \\begin{itemize} ";  
 1824:     } 
 1825:     return $currentstring;
 1826: }
 1827: 
 1828: sub end_menu {
 1829:     my ($target,$token) = @_;
 1830:     my $currentstring = '';
 1831:     if ($target eq 'web' || $target eq 'webgrade') {
 1832: 	$currentstring = $token->[2];     
 1833:     } elsif ($target eq 'tex') {
 1834: 	$currentstring = " \\end{itemize}";  
 1835:     } 
 1836:     return $currentstring;
 1837: }
 1838: 
 1839: #-- <dir> tag (end tag required)
 1840: sub start_dir {
 1841:     my ($target,$token) = @_;
 1842:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1843:     if ($target eq 'web' || $target eq 'webgrade') {
 1844: 	$currentstring .= $token->[4];     
 1845:     } elsif ($target eq 'tex') {
 1846: 	$currentstring .= " \\begin{itemize} ";  
 1847:     } 
 1848:     return $currentstring;
 1849: }
 1850: 
 1851: sub end_dir {
 1852:     my ($target,$token) = @_;
 1853:     my $currentstring = '';
 1854:     if ($target eq 'web' || $target eq 'webgrade') {
 1855: 	$currentstring = $token->[2];     
 1856:     } elsif ($target eq 'tex') {
 1857: 	$currentstring = " \\end{itemize}";  
 1858:     } 
 1859:     return $currentstring;
 1860: }
 1861: 
 1862: #-- <ol> tag (end tag required)
 1863: sub start_ol {
 1864:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1865:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1866:     if ($target eq 'web' || $target eq 'webgrade') {
 1867: 	$currentstring .= $token->[4];     
 1868:     } elsif ($target eq 'tex') {
 1869: 	$Apache::londefdef::list_index=0;
 1870: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1871: 	if ($type eq '1') {
 1872: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1873:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1874:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1875:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1876: 	} elsif ($type eq 'A') {
 1877: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1878:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1879:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1880:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1881: 	} elsif ($type eq 'a') {
 1882: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1883:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1884:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1885:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1886: 	} elsif ($type eq 'i') {
 1887: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1888:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1889:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1890:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1891: 	} elsif ($type eq 'I') {
 1892: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1893:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1894:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1895:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1896: 	}
 1897: 	$currentstring .= '\strut \begin{enumerate}';  
 1898:     } 
 1899:     return $currentstring;
 1900: }
 1901: 
 1902: sub end_ol {
 1903:     my ($target,$token) = @_;
 1904:     my $currentstring = '';
 1905:     if ($target eq 'web' || $target eq 'webgrade') {
 1906: 	$currentstring = $token->[2];     
 1907:     } elsif ($target eq 'tex') {
 1908: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1909:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1910:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1911:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1912:     } 
 1913:     return $currentstring;
 1914: }
 1915: 
 1916: #-- <dl> tag (end tag required)
 1917: sub start_dl {
 1918:     my ($target,$token) = @_;
 1919:     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
 1920:     if ($target eq 'web' || $target eq 'webgrade') {
 1921: 	$currentstring .= $token->[4];     
 1922:     } elsif ($target eq 'tex') {
 1923: 	$currentstring .= '\begin{description}';
 1924: 	$Apache::londefdef::DL++;
 1925: 	push(@Apache::londefdef::description,[]);
 1926: 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 1927: 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 1928: 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
 1929:     } 
 1930:     return $currentstring;
 1931: }
 1932: 
 1933: sub end_dl {
 1934:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1935:     my $currentstring = '';
 1936:     if ($target eq 'web' || $target eq 'webgrade') {
 1937: 	$currentstring = $token->[2];     
 1938:     } elsif ($target eq 'tex') {
 1939: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1940: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1941: 	foreach my $element (@{$Apache::londefdef::description[-1]}) {
 1942: 	    $currentstring.=' '.$element.' ';
 1943: 	}
 1944: 	pop(@Apache::londefdef::description);
 1945: 	$currentstring.='\end{description}';  
 1946: 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 1947: 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 1948: 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 1949: 	$Apache::londefdef::DL--;
 1950:     } 
 1951:     return $currentstring;
 1952: }
 1953: 
 1954: #-- <dt> tag (end tag optional)
 1955: sub start_dt {
 1956:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1957:     my $currentstring='';
 1958:     if ($target eq 'web' || $target eq 'webgrade') {
 1959: 	$currentstring = $token->[4];     
 1960:     } elsif ($target eq 'tex') {
 1961: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1962: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1963: 	&Apache::lonxml::startredirection();
 1964: 	$Apache::londefdef::DT[-1]++;
 1965: 	$Apache::londefdef::seenDT[-1]=1;
 1966:     } 
 1967:     return $currentstring;
 1968: }
 1969: 
 1970: sub end_dt {
 1971:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1972:     my $currentstring = '';
 1973:     if ($target eq 'web' || $target eq 'webgrade') {
 1974: 	$currentstring = $token->[2];    
 1975:     } elsif ($target eq 'tex') {
 1976: 	if ($Apache::londefdef::DT[-1]) {
 1977: 	    my $data=&item_cleanup();
 1978: 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 1979: 	    $Apache::londefdef::DT[-1]--;
 1980: 	}
 1981:     } 
 1982:     return $currentstring;
 1983: }
 1984: 
 1985: sub item_cleanup {
 1986:     my $item=&Apache::lonxml::endredirection();
 1987:     $item=~s/\\begin{center}//g;
 1988:     $item=~s/\\end{center}//g;
 1989:     return $item;
 1990: }
 1991: 
 1992: #-- <dd> tag (end tag optional)
 1993: sub start_dd {
 1994:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1995:     my $currentstring = '';
 1996:     if ($target eq 'web' || $target eq 'webgrade') {
 1997: 	$currentstring = $token->[4];     
 1998:     } elsif ($target eq 'tex') {
 1999: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 2000: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
 2001: 	if (!$Apache::londefdef::seenDT[-1]) {
 2002: 	    push(@{$Apache::londefdef::description[-1]},'\item[\strut] \strut \vskip 0mm ');
 2003: 	}
 2004: 	push(@{$Apache::londefdef::description[-1]},'');
 2005: 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 2006: 	$Apache::londefdef::DD[-1]++;
 2007: 	&Apache::lonxml::startredirection();
 2008:     } 
 2009:     return $currentstring;
 2010: }
 2011: 
 2012: sub end_dd {
 2013:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2014:     my $currentstring = '';
 2015:     if ($target eq 'web' || $target eq 'webgrade') {
 2016: 	$currentstring = $token->[2];    
 2017:     }  elsif ($target eq 'tex') {
 2018: 	$Apache::londefdef::description[-1]->[-1].=
 2019: 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
 2020: 	$Apache::londefdef::DD[-1]--;
 2021:     }
 2022:     return $currentstring;
 2023: }
 2024: 
 2025: #-- <table> tag (end tag required)
 2026: #       <table> also ends any prior <p> that is not closed.
 2027: #               but, unless I allow <p>'s to nest, that's the
 2028: #               only way I could think of to allow <p> in 
 2029: #               <tr> <th> bodies
 2030: #
 2031: #list of supported attributes: border,width,TeXwidth,TeXtheme
 2032: #                              align
 2033: sub start_table {
 2034:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2035:     my $textwidth = '';
 2036:     my $currentstring = &end_p();
 2037:     if ($target eq 'web' || $target eq 'webgrade') {
 2038: 	$currentstring .= $token->[4];     
 2039:     } elsif ($target eq 'tex') {
 2040: 	&disable_para();	# Can't have paras in a table.
 2041: 
 2042: 
 2043: 	#  New table code:
 2044: 
 2045: 	#  Get the parameters that we can do something about:
 2046: 
 2047: 	my $border = &Apache::lonxml::get_param('border', $parstack, $safeeval, undef, 0);
 2048: 	my $width  = &Apache::lonxml::get_param('TeXwidth', $parstack, $safeeval, undef, 0);
 2049: 	my $theme  = &Apache::lonxml::get_param('TeXtheme', $parstack, $safeeval, undef, 0);
 2050: 	my $align  = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 0);
 2051: 
 2052: 	# The only thing that needs any figuring out is the width.. and then only if it is
 2053: 	# a percent. If not it's assumed to be some valid TeX measurement unit e.g. 3.0cm
 2054: 	#
 2055: 
 2056: 	my $table = new Apache::lontable();
 2057: 	if ($border ne '') {
 2058: 	    $table->table_border(1);
 2059: 	    $table->cell_border(1);
 2060: 	}
 2061: 	if ($theme ne '') {
 2062: 	    $table->theme($theme);
 2063: 	}
 2064: 	if ($align ne '') {
 2065: 	    $table->alignment($align);
 2066: 	}
 2067: 
 2068: 	# Missing width is most of page width
 2069: 
 2070: 	if ($width eq "") {
 2071: 	    $width = '70%';
 2072: 	}
 2073: 	
 2074: 	# If a percentage, need to calculate what this means in terms of
 2075: 	# page width:
 2076: 	
 2077: 	if ($width =~ /%$/) {
 2078: 	    my $textwidth = &recalc($env{'form.textwidth'});  # Page width in mm.
 2079: 	    $width =~ s/%//;
 2080: 	    $width = $width * $textwidth / 100.0;
 2081: 	    $width .= " mm";
 2082: 	    $table->width($width);
 2083: 	}
 2084: 
 2085: 	push(@Apache::londefdef::table, $table);
 2086:         $currentstring.=' \keephidden{NEW TABLE ENTRY}';
 2087: 
 2088: 	#--------------------------------------------------------
 2089: 	#  Old table code here.
 2090: 	#--------------------------------------------------------
 2091: 
 2092: 
 2093: 	if (0) {
 2094: 	push(@Apache::londefdef::table, {}); 
 2095: 	$Apache::londefdef::table[-1]{'row_number'} = -1;
 2096:         #maximum table's width (default coincides with text line length)
 2097: 	if ($#Apache::londefdef::table==0) {
 2098: 	    $textwidth=&recalc($env{'form.textwidth'}); #result is always in mm
 2099: 	    $textwidth=~/(\d+\.?\d*)/;
 2100: 	    $textwidth=0.85*$1; #accounts "internal" LaTeX space for table frame
 2101: 	} else {
 2102: 	    if ($Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]=~/\d/) {
 2103: 		#the maximum width of nested table is determined by LATeX width of parent cell
 2104: 		$textwidth=$Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]; 
 2105: 	    } else {
 2106:               #try to use all space not used before (minus 5% for LaTeX table internal) - rather silly
 2107: 		$textwidth=$Apache::londefdef::table[-2]{'width'};
 2108: 		for (my $i=0;$i<$Apache::londefdef::table[-2]{'counter_columns'};$i++) {
 2109: 		    $textwidth=$textwidth-$Apache::londefdef::table[-2]{'TeXlen'}[0][$i];
 2110: 		}
 2111: 	    }
 2112: 	}
 2113: 
 2114: 	# width either comes forced from the TeXwidth or the width parameters.
 2115: 	# in either case it can be a percentage or absolute width.
 2116: 	# in the width case we ignore absolute width 
 2117: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2118: 	if (!defined($TeXwidth)) {
 2119: 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,
 2120: 						       $safeeval,undef,1);
 2121: 	    if ($htmlwidth =~ /%/) {
 2122: 		$TeXwidth = $htmlwidth;
 2123: 	    } else { 
 2124: 		$TeXwidth = $textwidth;
 2125: 	    }
 2126: 	}
 2127: 	# if the width is specified as a % it is converted to an absolute width.
 2128: 	# otherwise.. just plugged right in the hash
 2129: 
 2130: 	if ($TeXwidth=~/%/) {
 2131: 	    $TeXwidth=~/(\d+)/;
 2132:             $Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;
 2133: 	} else {
 2134: 	    $Apache::londefdef::table[-1]{'width'}=$TeXwidth;
 2135: 	}
 2136:         #  In the end, however the table width cannot be wider than $textwidth...
 2137: 	
 2138: 	if ($Apache::londefdef::table[-1]{'width'} > $textwidth) {
 2139: 	    $Apache::londefdef::table[-1]{'width'} = $textwidth;
 2140: 	}
 2141:         #table's border
 2142: 	my $border = &Apache::lonxml::get_param('border',$parstack,$safeeval); 
 2143:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2144: 	unless (defined $border) { $border = 0; }
 2145: 	if ($border) { 
 2146: 	    $Apache::londefdef::table[-1]{'hinc'} = '\hline '; 
 2147: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2148: 	    $Apache::londefdef::table[-1]{'vvinc'} = '|';
 2149: 	} else {
 2150: 	    $Apache::londefdef::table[-1]{'hinc'} = ''; 
 2151: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2152: 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 2153: 	}
 2154: 	if ($#Apache::londefdef::table==0) {
 2155: 	    #    Note that \newline seems to destroy the alignment envs.
 2156: 	    # $Apache::londefdef::table[-1]{'output'}='\strut\newline\strut\setlength{\tabcolsep}{1 mm}';
 2157: 	    $Apache::londefdef::table[-1]{'output'}='\strut'.'\\\\'."\n".'\strut\setlength{\tabcolsep}{1 mm}';
 2158: 	}
 2159: 	$Apache::londefdef::table[-1]{'output'}.=' \noindent \begin{tabular} ';
 2160:         $Apache::londefdef::table[-1]{'TeXlen'}=[];
 2161:         $Apache::londefdef::table[-1]{'objectlen'}=[];
 2162:         $Apache::londefdef::table[-1]{'objectsignal'}=[];
 2163:         $Apache::londefdef::table[-1]{'maxlen'}=[];
 2164:         $Apache::londefdef::table[-1]{'minlen'}=[];
 2165:         $Apache::londefdef::table[-1]{'content'}=[];
 2166:         $Apache::londefdef::table[-1]{'align'}=[];
 2167:         $currentstring.=' \keephidden{NEW TABLE ENTRY}';
 2168:     }
 2169: 
 2170:     }
 2171:     return $currentstring;
 2172: }
 2173:  
 2174: sub end_table {
 2175:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2176:     my $currentstring = '';
 2177:     if ($target eq 'web' || $target eq 'webgrade') {
 2178: 	$currentstring = $token->[2];     
 2179:     } elsif ($target eq 'tex') {
 2180: 
 2181: 
 2182: 	#  New table code:
 2183: 
 2184: 	my $table = pop(@Apache::londefdef::table);
 2185: 	my $t     = $table->generate();
 2186: 	$currentstring = $t->generate_string();
 2187: 	&enable_para();
 2188: 	#--------------------------------------------------------------
 2189: 	#  Old table code:
 2190: 	#--------------------------------------------------------------
 2191: 
 2192: 	if (0) {
 2193: 
 2194: 	my $border =  &Apache::lonxml::get_param('border',$parstack,$safeeval);
 2195: 	my $inmemory = '';
 2196: 	my $output = '';
 2197: 	my $WARNING='';
 2198:         #width of columns from TeXwidth attributes
 2199: 
 2200: 	# Protect against unbalanced </table> tag.
 2201: 
 2202: 	if (scalar(@Apache::londefdef::table) > 0) {
 2203: 
 2204: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2205: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2206: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
 2207: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
 2208: 		}	
 2209: 	    }
 2210: 	}
 2211:         #free space and number of empty columns
 2212: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
 2213: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
 2214: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2215: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
 2216: 		$empty_columns++;
 2217: 	    } else {
 2218: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
 2219: 	    }
 2220: 	}
 2221: 
 2222:         #boundaries for contents columns
 2223: 	my @min_len=();#columns can not be narrower 
 2224: 	my @max_len=();#maximum length of column
 2225: 	my $avg_max;
 2226: 	my $avg_min;
 2227: 	my $counter_cols = $Apache::londefdef::table[-1]{'counter_columns'};
 2228: 	for (my $jn=0;$jn<=$counter_cols; $jn++) {
 2229: 		my ($localmin,$localmax)=(0,0);
 2230: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2231: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
 2232: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
 2233: 		    }
 2234: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
 2235: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
 2236: 		    }
 2237: 		}
 2238: 		push @min_len, $localmin;
 2239: 		push @max_len, $localmax;
 2240: 		$avg_max = $localmax + $avg_max;
 2241: 		$avg_min = $localmin + $avg_min;
 2242: 	}
 2243: 	# Does not really matter what the average max/min are if there are no cols.
 2244: 	# and this prevents div 0 in that case.
 2245: 
 2246: 	if ($counter_cols != 0) {
 2247: 	    $avg_max = $avg_max/$counter_cols;
 2248: 	    $avg_min = $avg_min/$counter_cols;
 2249: 	}
 2250: 
 2251: 
 2252: 	#  I don't think the below is needed.. but just in case:
 2253: 
 2254: 	if ($avg_min > $avg_max) {
 2255: 	    my $temp = $avg_min;
 2256: 	    $avg_min = $avg_max;
 2257: 	    $avg_max = $temp;
 2258: 	}
 2259: 
 2260: 
 2261: 	for (my $jn=0;$jn<=$counter_cols;$jn++) {
 2262: 	    my $localmin=0,;
 2263: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2264: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
 2265: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
 2266: 		}
 2267: 	    }
 2268: 	    if ($max_len[$jn]<$localmin) {
 2269: 		$max_len[$jn]=$localmin;
 2270: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2271: 	    }#object size is bigger
 2272: 	    if ($min_len[$jn]<$localmin) {
 2273: 		$min_len[$jn]=$localmin;
 2274: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2275: 	    }#object size is bigger
 2276: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
 2277: 		$min_len[$jn]=0;
 2278: 		$max_len[$jn]=0;
 2279: 	    }
 2280: 	    #  Spans seem to be really bothered by max/min = 0.  So if we have one
 2281: 	    #  make it an average joe max/min.
 2282: 	    
 2283: 	    if ($max_len[$jn] == 0) {
 2284: 		$max_len[$jn] = $avg_max;
 2285: 	    }
 2286: 	    if ($min_len[$jn] == 0) {
 2287: 		$min_len[$jn] = $avg_min;
 2288: 	    }
 2289: 
 2290: 	}
 2291:        #final adjustment of column width
 2292: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
 2293: 	my @adjust=();
 2294:         #step 1. adjustment by maximum value
 2295: 	my $space_needed=0;
 2296: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
 2297: 	    $space_needed=$space_needed+$max_len[$jn];
 2298: 	}
 2299: 	if ($space_needed<=$available_space) {
 2300: 
 2301: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
 2302: 		if ($fwidth[$jn]==0) {
 2303: 		    $fwidth[$jn]=$max_len[$jn];
 2304: 		}
 2305: 	    }
 2306: 	} else {
 2307:         #step 2. adjustment by minimum value (estimation)
 2308: 	    $space_needed=0;
 2309: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2310: 		$space_needed+=$min_len[$jn];
 2311: 	    }
 2312: 	    if ($space_needed>$available_space) {
 2313: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
 2314: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2315: 		    if ($fwidth[$jn]==0) {
 2316: 			$fwidth[$jn]=$min_len[$jn];
 2317: 		    }
 2318: 		}
 2319: 		#check if we have objects which can be scaled
 2320: 		my $how_many_to_scale=0;
 2321: 		my @to_scale=();
 2322: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2323: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
 2324: 			$how_many_to_scale++;
 2325: 			push @to_scale, $jn;
 2326: 		    }
 2327: 		}
 2328: 		if ($how_many_to_scale>0) {
 2329: 		    my $space_to_adjust=($space_needed-$available_space)/$how_many_to_scale;
 2330: 		    foreach my $jn (@to_scale) {
 2331: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2332: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
 2333: 			    if ($1 ne '') {
 2334: 				my $current_length=&recalc($1);
 2335: 				$current_length=~/(\d+\.?\d*)/;
 2336: 				$current_length=$current_length-$space_to_adjust;
 2337: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
 2338: 			    }
 2339: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
 2340: 			    if ($1 ne '') {
 2341: 				my $current_length=$1;
 2342: 				$current_length=$current_length-$space_to_adjust;
 2343: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
 2344: 			    }				
 2345: 			}
 2346: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
 2347: 		    }
 2348: 		}
 2349: 	    } else {
 2350: 	      #step 3. adjustment over minimal + corrections
 2351: 		my $enlarge_coef=$available_space/$space_needed;
 2352: 		my $acsessive=0;
 2353: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
 2354: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
 2355: 		    if ($adjust[$jn]>$max_len[$jn]) {
 2356: 			$fwidth[$jn]=$max_len[$jn];
 2357: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
 2358: 			$adjust[$jn]=0;
 2359: 
 2360: 		    }
 2361: 		}
 2362: 		if ($acsessive>0) {
 2363: 		#we have an excess of space and can redistribute it
 2364: 		    my $notempty_columns=0;
 2365: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2366: 			if ($adjust[$jn]!=0) {
 2367: 			    $notempty_columns++;
 2368: 			}
 2369: 		    }
 2370: 		    my $per_column=$acsessive/$notempty_columns;
 2371: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2372: 			if ($adjust[$jn]!=0) {
 2373: 			    $adjust[$jn]+=$per_column;
 2374: 			    $fwidth[$jn]=$adjust[$jn];
 2375: 			}
 2376: 		    }
 2377: 		} else {
 2378: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2379: 			$fwidth[$jn]=$adjust[$jn];
 2380: 		    }
 2381: 		}
 2382: 	    }
 2383: 	}
 2384:         # use all available width or specified width as if not specified,
 2385: 	# the specified width gets defaulted to the available width.
 2386: 
 2387: 	my $current=0; 
 2388: 	for (my $i=0;$i<=$#fwidth;$i++) {  
 2389: 	    $current+=$fwidth[$i];
 2390: 	}
 2391: 	if ($current == 0) {
 2392:             $current = $Apache::londefdef::table[-1]{'width'};
 2393:         }
 2394: 	my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
 2395: 	for (my $i=0;$i<=$#fwidth;$i++) {  
 2396: 	    $fwidth[$i]*=$coef;
 2397: 	}
 2398:         #removing of empty columns if allowed
 2399:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2400: 	if ($permission eq 'yes') {
 2401: 	    my @cleaned_table=();
 2402:             my @cleaned_header=();
 2403: 	    my $colind=0;
 2404: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2405: 		if ($fwidth[$jn]!=0) {
 2406: 		    #we need to copy column
 2407: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2408: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2409: 			$cleaned_header[$colind]=$fwidth[$jn];
 2410: 		    }
 2411: 		    $colind++;
 2412: 		}
 2413: 	    }
 2414: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
 2415: 	    @fwidth=@cleaned_header;
 2416: 	}
 2417: 
 2418: 
 2419: 	#construct header of the table
 2420: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
 2421: 	for (my $in=0;$in<=$#fwidth;$in++) {
 2422: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
 2423: 	}
 2424: 	$header_of_table .= '}';
 2425: 
 2426: 	#fill the table
 2427: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2428: 	    my $have_rowspan = 0;
 2429: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 2430: 
 2431: 		#-----------------------------------------------------------
 2432:                 #   I think this order of doing things will ensure that
 2433: 		#   single rowspan, columspan and combined row/colspans will
 2434:                 #   work correctly.  LaTeX is delicate here.
 2435: 		#    RF.
 2436: 		
 2437: 		# Start a rowspan if necessary:
 2438: 		
 2439: 		my $primary_col_width = $fwidth[$jn]; # Width of primary column.
 2440: 		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2441: 		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
 2442: 		#
 2443: 		#  Do the appropriate magic if this has a colspan
 2444: 		# 
 2445: 		
 2446: 		my $border_char = "";
 2447: 		if ($border) {
 2448: 		    $border_char = "|";
 2449: 		}
 2450: 		my $spanwidth = 0;
 2451: 		if ($colspan > 1) {
 2452: 		    for (my $spancol = $jn; $spancol < $jn + $colspan; $spancol++) {
 2453: 			$spanwidth += $fwidth[$spancol];
 2454: 		    }
 2455: 		    $output .= '\multicolumn{'.
 2456: 			$colspan
 2457: 			."}";
 2458: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2459: 			$output .= '{'.$border_char.'c'.$border_char.'}{';
 2460: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2461: 			$output .= '{'.$border_char.'r'.$border_char.'}{';
 2462: 		    }
 2463: 		    else {
 2464: 			$output .= '{'.$border_char."p{$spanwidth mm}".$border_char.'}{';
 2465: 		    }
 2466: 		    
 2467: 		} else {
 2468: 		    $spanwidth = $primary_col_width; # If no span width will be just colwidth
 2469: 		}
 2470: 
 2471: 		# Rowspan... if colspan is 1, and there's an alignment we'll need
 2472: 		# to kick in a multicolumn in order to get the alignment spec.
 2473: 		# this must precede the multirow or LaTex gets quite upset.
 2474: 		# Naturally if colspan > 1 we've already done that above ^
 2475: 		#
 2476: 		my $multirow_aligned = 0;
 2477: 		if ($rowspan > 1) {
 2478: 		    if ($colspan == 1) {
 2479: 			if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2480: 			    $output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
 2481: 			    $multirow_aligned = 1;
 2482: 			} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2483: 			    $output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
 2484: 			    $multirow_aligned = 1;
 2485: 			}
 2486: 		    }
 2487: 		    $have_rowspan++;
 2488: 		    if ($multirow_aligned) {
 2489: 			$output .= '\multirow{'.$rowspan.'}[0]{*}{';
 2490: 		    } else {
 2491: 			$output .= '\multirow{'.$rowspan."}[0]{$spanwidth mm}{";
 2492: 		    }
 2493: 		    
 2494: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
 2495: 			s{^\s*\\par\s*}{};
 2496: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
 2497: 			s{\s*\\vskip\s*0pt\s*$}{};
 2498: 			  
 2499: 		    #
 2500: 		    # If we did not throw in a multicolumn to align, then add 
 2501: 		    # an extra {
 2502: 		    # so we close correctly without having to keep additional state
 2503: 		    # around
 2504: 		    #
 2505: 		    if (!$multirow_aligned) {
 2506: 			$output .= '{';
 2507: 		    }
 2508: 		}
 2509: 		if (($rowspan eq '^') || ($rowspan eq '_')) {
 2510: 		    $have_rowspan++;
 2511: 		}
 2512: 		    #--------------------------------------------------------------
 2513: 
 2514: 
 2515: 		# For right and center alignment of single cells.
 2516: 		# we are going to use a multicolumn with a span of 1 to specify alignment.
 2517: 		#
 2518: 		if ($colspan == 1  && $rowspan == 1) {
 2519: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2520: 			$output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
 2521: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2522: 			$output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
 2523: 		    }
 2524: 		}
 2525: 
 2526: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2527: 
 2528: 		if (($colspan == 1 && $rowspan == 1)   &&
 2529: 		    (($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') ||
 2530: 		     ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r'))) {
 2531: 		    $output .= '}';
 2532: 		}
 2533: 
 2534: 		# Close off any open multirow:
 2535: 		
 2536: 		if ($rowspan > 1) {
 2537: 		    $output .= '}}';
 2538: 		}
 2539: 		#  Close off the colspan...
 2540: 		#
 2541: 		if ($colspan > 1)  {
 2542: 		    $output .= '}';
 2543: 		    $jn += $colspan-1; # Adjust for number of rows really left.
 2544: 		}
 2545:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 2546: 	    }
 2547: 	    #  If have_rowspan > 0, and borders are on, then 
 2548: 	    #  we need to do more than put an \hline at the bottom of row.
 2549: 	    #  we need to do the appropriate \cline to ensure that
 2550: 	    #  the spanned rows don't have \hlines through them.
 2551: 
 2552: 	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
 2553: 		$output .= ' \\\\ ';
 2554: 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
 2555: 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2556: 		    if ($rowspan ne "^") {
 2557: 			if (($rowspan <= 1) || ($rowspan eq '_')) {
 2558: 			    my $column = $jn+1;
 2559: 			    $output .= '\cline{'.$column.'-'.$column.'} ';
 2560: 			}
 2561: 		    }
 2562: 		}
 2563: 
 2564: 	    } else {
 2565: 		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
 2566: 	    }
 2567: 	}
 2568: 	# Note that \newline destroys alignment env's produced  by e.g. <div>
 2569: 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
 2570: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut'.'\\\\'."\n".'\strut ';
 2571: 	if ($#Apache::londefdef::table > 0) {	    
 2572: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
 2573: 	    # Figure out max/and min width  by summing us and then
 2574: 	    # apply that to the current column of the table we nest in
 2575: 	    # if it's larger than the current width or the current width
 2576: 	    # is undefined.
 2577: 	    #
 2578: 	    my $min_nested_width = 0;
 2579: 	    my $max_nested_width = 0;
 2580: 	    for (my $col = 0; $col <= $Apache::londefdef::table[-1]{'counter_columns'}; $col++) {
 2581: 		$min_nested_width +=  $min_len[$col];
 2582: 		$max_nested_width +=  $max_len[$col];
 2583: 		
 2584: 	    }
 2585: 	    # Fudge in an extra 5 mm for borders etc:
 2586: 	    
 2587: 	    $min_nested_width += 5;
 2588: 	    $max_nested_width += 5;
 2589: 
 2590: 	    my $outer_column = $Apache::londefdef::table[-2]{'counter_columns'};
 2591: 	    my $outer_row    = $Apache::londefdef::table[-2]{'row_number'};
 2592: 	    if ($min_nested_width > $Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column]) {
 2593: 		$Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column] = $min_nested_width;
 2594: 	    }
 2595: 	    if ($max_nested_width > $Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column]) {
 2596: 		$Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column] = $max_nested_width;
 2597: 	    }
 2598: 
 2599: 	    pop @Apache::londefdef::table;
 2600: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
 2601: 	} else {
 2602: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 2603: 	    pop @Apache::londefdef::table;
 2604: 	    undef @Apache::londefdef::table;
 2605: 	}
 2606: 	}
 2607: 	&enable_para();
 2608:     }
 2609:     }
 2610:     return $currentstring;
 2611: }
 2612: 
 2613: #-- <tr> tag (end tag optional)
 2614: sub start_tr {
 2615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2616:     my $currentstring = '';
 2617:     if ($target eq 'web' || $target eq 'webgrade') {
 2618: 	$currentstring = $token->[4];     
 2619:     } elsif ($target eq 'tex') {
 2620: 
 2621: 	my $align = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
 2622: 	$Apache::londefdef::table[-1]->start_row();
 2623: 	
 2624: 	if ($align ne '') {
 2625: 	    $Apache::londefdef::table[-1]->configure_row({default_halign => $align});
 2626: 	}
 2627: 
 2628: 	#---------------------------------------------------------------
 2629: 	# Old table code.
 2630: 	#---------------------------------------------------------------
 2631: 
 2632: 	if (0) {
 2633: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2634: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2635: 	if ($alignchar ne '') {
 2636: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2637: 	} else {
 2638: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2639: 	}
 2640: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2641: 	#
 2642: 	#  Need to save the number of table columns to preserve the max # columns.
 2643: 	#
 2644: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
 2645: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2646: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2647: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2648: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2649: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2650: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2651:     }
 2652:     } 
 2653:     return $currentstring;
 2654: }
 2655:         
 2656: sub end_tr {
 2657:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2658:     my $currentstring = &end_p();	# Close any pending <p> in the row.
 2659:     if ($target eq 'web' || $target eq 'webgrade') {
 2660: 	$currentstring .= $token->[2];     
 2661:     } elsif ($target eq 'tex') {
 2662: 
 2663: 	# In case the user is missing a </td> or </th> tag:
 2664: 
 2665: 	if ($Apache::londefdef::TD_redirection) {
 2666: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2667: 	}
 2668: 	$Apache::londefdef::table[-1]->end_row();
 2669: 
 2670: 	#-----------------------------------------------
 2671: 	# Old table code
 2672: 	#-----------------------------------------------
 2673: 
 2674: 	if (0) {
 2675: 	if ($Apache::londefdef::TD_redirection) {
 2676: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2677: 	}
 2678: 	# Counter columns must be the maximum number of columns seen
 2679: 	# in the table so far so:
 2680: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
 2681: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
 2682: 	}
 2683:     }
 2684: 
 2685: 	
 2686:     }
 2687:     return $currentstring;
 2688: }
 2689: 
 2690: #-- <td> tag (end tag optional)
 2691: sub start_td {
 2692:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2693:     my $currentstring = '';
 2694:     if ($target eq 'web' || $target eq 'webgrade') {
 2695: 	$currentstring = $token->[4];     
 2696:     } elsif ($target eq 'tex') {
 2697: 	$Apache::londefdef::TD_redirection = 1;
 2698: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2699:     } 
 2700:     return $currentstring;
 2701: }   
 2702:     
 2703: sub tag_check {
 2704:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2705:     my @ar=@$parstack; 
 2706:     for (my $i=$#ar-1;$i>=0;$i--) {
 2707: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2708: 	    &start_td_tex($parstack,$parser,$safeeval);
 2709: 	    last;
 2710: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2711: 	    splice @ar, $i+1;
 2712: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2713: 	    &start_td_tex($parstack,$parser,$safeeval);
 2714: 	    last;
 2715: 	}
 2716:     }
 2717:     return '';
 2718: }
 2719: 
 2720: #
 2721: #  Factor out cell configuration hash generation:
 2722: #
 2723: 
 2724: sub cell_config_hash {
 2725:     my ($align, $rowspan, $colspan) = @_;
 2726:     my %config;
 2727:     if ($align ne '') {
 2728: 	$config{'halign'} = $align;
 2729:     }
 2730:     if ($colspan ne "") {
 2731: 	$config{'colspan'} = $colspan;
 2732:     }
 2733:     if ($rowspan ne '') {
 2734: 	$config{'rowspan'} = $rowspan;
 2735:     }
 2736:     return \%config;
 2737: }
 2738:  
 2739: sub start_td_tex {
 2740:     my ($parstack,$parser,$safeeval) = @_;
 2741: 
 2742:     # At this stage, an empty cell is created with the
 2743:     # appropriate rowspan/colspan and alignment
 2744:     # attributes, but empty of text.  end_td_tex will
 2745:     # fetch the contents from the recursive parse and
 2746:     # fill the cell with them:
 2747:     my $align   = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
 2748:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 1);
 2749:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 1);
 2750: 
 2751:     my $config = &cell_config_hash($align, $rowspan, $colspan);
 2752: 
 2753:     my $table = $Apache::londefdef::table[-1];
 2754:     $table->add_cell('', $config);
 2755:     
 2756: 
 2757:     #------------------------------------------------
 2758:     #  Old table code.
 2759:     #------------------------------------------------
 2760: 
 2761:     if (0) {
 2762: 
 2763:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2764:     if ($alignchar eq '') {
 2765: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2766:     }
 2767:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2768:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2769:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2770:     if (defined $TeXwidth) {		
 2771: 	my $current_length=&recalc($TeXwidth);
 2772: 	$current_length=~/(\d+\.?\d*)/;
 2773: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2774:     }
 2775:     }
 2776:     &Apache::lonxml::startredirection();
 2777:     return '';
 2778: }
 2779: 
 2780: sub end_td_tex {
 2781: 
 2782:     my $text = &Apache::lonxml::endredirection();
 2783:     my $table = $Apache::londefdef::table[-1];
 2784:     $table->append_cell_text($text);
 2785: 
 2786:     #-------------------------------------------------
 2787:     # Old table code
 2788:     #-------------------------------------------------
 2789: 
 2790:     if (0) {
 2791:     my ($parstack,$parser,$safeeval) = @_;
 2792:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
 2793:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
 2794:     my $data = &Apache::lonxml::endredirection();
 2795: 
 2796:     #  The rowspan array of the table indicates which cells are part of a span.
 2797:     #  n indicates the start of a span set of n rows.
 2798:     #  ^ indicates a cell that continues a span set.
 2799:     #  _ indicates the cell is at the bottom of a span set.
 2800:     #  If this and subsequent cells are part of a rowspan, we must
 2801:     #  push along the row until we find one that is not.
 2802: 
 2803:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
 2804: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 2805: 	# Part of a span.
 2806: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 2807: 	$current_column++;
 2808:     }
 2809:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
 2810:    
 2811: 
 2812:     # Get the column and row spans.
 2813:     # Colspan can be done via \multicolumn if I can figure out the data structs.
 2814: 
 2815:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
 2816:     if (!$colspan) {
 2817: 	$colspan = 1;
 2818:     }
 2819: 
 2820:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 2821:     if (!$rowspan) {
 2822: 	$rowspan = 1;
 2823:     }
 2824: 
 2825: 
 2826: 
 2827:     for (my $c = 0; $c < $colspan; $c++) {
 2828: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
 2829: 	for (my $i = 1; $i < $rowspan; $i++) {
 2830: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
 2831: 	    if ($i == ($rowspan-1)) {
 2832: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
 2833: 	    }
 2834: 	}
 2835:     }
 2836: 
 2837:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2838:     if (defined $TeXwidth) {		
 2839: 	for (my $c = 0; $c < $colspan; $c++) {
 2840: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2841: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2842: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2843: 	}
 2844:     } else {
 2845: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2846: 	    my $garbage_data=$data;
 2847: 	    my $fwidth=0;
 2848:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2849: 		my $current_length=&recalc($1);
 2850: 		$current_length=~/(\d+\.?\d*)/;
 2851: 		if ($fwidth<$1) {$fwidth=$1;}
 2852: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2853: 	    }
 2854:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2855: 		my $current_length=$1;
 2856: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2857: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2858: 	    }
 2859: 	    for (my $c = 0; $c < $colspan; $c++) {
 2860: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2861: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2862: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2863: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2864: 	    }
 2865: 	} elsif ($data=~/\\parbox\{\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*\s*\}/ or $data=~/\\epsfxsize\s*=\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*/) {
 2866: 	    my $garbage_data=$data;
 2867: 	    my $fwidth=0;
 2868:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
 2869: 		my $current_length=&recalc($1);
 2870: 		$current_length=~/(\d+\.?\d*)/;
 2871: 		if ($fwidth<$1) {$fwidth=$1;}
 2872: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2873: 	    }
 2874:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2875: 		my $current_length=&recalc($1);
 2876: 		$current_length=~/(\d+\.?\d*)/;
 2877: 		if ($fwidth<$1) {$fwidth=$1;}
 2878: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2879: 	    }
 2880: 	    for (my $c = 0; $c < $colspan; $c++) {
 2881: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2882: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2883: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2884: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2885: 	    }
 2886: 	    $data=~s/\\\\\s*$//; 
 2887: 	} else {  
 2888: 	    $data=~s/^\s+(\S.*)/$1/; 
 2889: 	    $data=~s/(.*\S)\s+$/$1/;
 2890: 	    $data=~s/(\s)+/$1/;
 2891: 	    my ($current_length,$min_length)=(0,0);
 2892: 	    if ($data=~/\\vskip/) {
 2893:                 my $newdata=$data;
 2894: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2895: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2896: 		foreach my $elementdata (@newdata) {
 2897: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2898: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2899:                     my @words=split(/ /,$elementdata);
 2900: 		    foreach my $word (@words) {
 2901: 			my $lengthword=2.5*&LATEX_length($word);
 2902: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2903: 		    }
 2904: 		}
 2905: 	    } else {
 2906: 		$current_length=2.5*&LATEX_length($data);
 2907:                     my @words=split(/ /,$data);
 2908: 		    foreach my $word (@words) {
 2909: 			my $lengthword=2*&LATEX_length($word);
 2910: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2911: 		    }
 2912: 	    }
 2913: 	    for (my $c = 0; $c < $colspan; $c++) {
 2914: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2915: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2916: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2917: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2918: 	    }
 2919: 	}        
 2920:     }
 2921:     # Substitute all of the tables nested in this cell in their appropriate places.
 2922: 
 2923: 
 2924:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
 2925:     for (my $in=0; $in<=$nested_count; $in++) {    
 2926: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 2927: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 2928: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2929: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
 2930: 
 2931:     }
 2932:     # Should be be killing off the 'include' elements as they're used up?
 2933: 
 2934:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2935: 
 2936: 
 2937: 
 2938: 
 2939:     #  the colspan array will indicate how many columns will be spanned by this
 2940:     #  cell..this requires that counter_columns also be adjusted accordingly
 2941:     #  so that the next bunch of text goes in the right cell.  Note that since
 2942:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
 2943:     #
 2944: 
 2945:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
 2946:     for (my $i = 0; $i < ($colspan -1); $i++) {
 2947: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
 2948:     }
 2949:     for (my $r = 0; $r < $rowspan; $r++) {
 2950: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
 2951: 	# Put empty text in spanned cols.
 2952: 	
 2953:     }
 2954: 
 2955:     }
 2956: 
 2957:     return '';
 2958: }
 2959: 
 2960: sub end_td {
 2961:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2962:     my $currentstring = '';
 2963:     if ($target eq 'web' || $target eq 'webgrade') {
 2964: 	$currentstring = $token->[2];     
 2965:     } elsif ($target eq 'tex') {
 2966:         $Apache::londefdef::TD_redirection =0;
 2967: 	&end_td_tex($parstack,$parser,$safeeval);
 2968:     }
 2969:     return $currentstring;
 2970: }
 2971: 
 2972: #-- <th> tag (end tag optional)
 2973: sub start_th {
 2974:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2975:     my $currentstring = '';
 2976:     if ($target eq 'web' || $target eq 'webgrade') {
 2977: 	$currentstring = $token->[4];     
 2978:     } elsif ($target eq 'tex') {
 2979: 	$Apache::londefdef::TD_redirection = 1;
 2980: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2981:     } 
 2982:     return $currentstring;
 2983: }   
 2984:     
 2985: sub tagg_check {
 2986:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2987:     my @ar=@$parstack; 
 2988:     for (my $i=$#ar-1;$i>=0;$i--) {
 2989: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2990: 	    &start_th_tex($parstack,$parser,$safeeval);
 2991: 	    last;
 2992: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2993: 	    splice @ar, $i+1;
 2994: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2995: 	    &start_th_tex($parstack,$parser,$safeeval);
 2996: 	    last;
 2997: 	}
 2998:     }
 2999:     return '';
 3000: }
 3001:  
 3002: sub start_th_tex {
 3003:     my ($parstack,$parser,$safeeval) = @_;
 3004: 
 3005:     my $alignment = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef,1);
 3006:     my $rowspan  =  &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 1);
 3007:     my $colspan  =  &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 1);
 3008: 
 3009:     my $config   = cell_config_hash($alignment, $rowspan, $colspan);
 3010:     my $table    = $Apache::londefdef::table[-1];
 3011:     $table->add_cell('\textbf{', $config);
 3012: 
 3013:     #-------------------------------------------------------------------------------------
 3014:     #
 3015:     #  Old table code.
 3016:     #
 3017:     #--------------------------------------------------------------------------------------
 3018: 
 3019:     if (0) {
 3020: 
 3021: 
 3022:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 3023:     if ($alignchar eq '') {
 3024: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 3025:     }
 3026:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 3027:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 3028:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 3029:     if (defined $TeXwidth) {		
 3030: 	my $current_length=&recalc($TeXwidth);
 3031: 	$current_length=~/(\d+\.?\d*)/;
 3032: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 3033:     }
 3034:     }
 3035: 
 3036:     # Accept xml until the </th> tag.
 3037: 
 3038:     &Apache::lonxml::startredirection();
 3039:     return '';
 3040: }
 3041: 
 3042: sub end_th_tex {
 3043:     my ($parstack,$parser,$safeeval) = @_;
 3044: 
 3045:     my $table = $Apache::londefdef::table[-1];
 3046:     my $text  = &Apache::lonxml::endredirection();
 3047:     $table->append_cell_text($text.'}');
 3048: 
 3049:     #-----------------------------------------------------------------------------
 3050:     #  Old table code:
 3051:     #-----------------------------------------------------------------------------
 3052: 
 3053:     if (0) {
 3054:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 3055:     my $data=&Apache::lonxml::endredirection();
 3056:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 3057:     if (defined $TeXwidth) {		
 3058: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3059: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3060: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3061:     } else {
 3062: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 3063: 	    my $garbage_data=$data;
 3064: 	    my $fwidth=0;
 3065:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 3066: 		my $current_length=&recalc($1);
 3067: 		$current_length=~/(\d+\.?\d*)/;
 3068: 		if ($fwidth<$1) {$fwidth=$1;}
 3069: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 3070: 	    }
 3071:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 3072: 		my $current_length=$1;
 3073: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 3074: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 3075: 	    }
 3076: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3077: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 3078: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3079: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3080: 	} else {  
 3081: 	    $data=~s/^\s+(\S.*)/$1/; 
 3082: 	    $data=~s/(.*\S)\s+$/$1/;
 3083: 	    $data=~s/(\s)+/$1/;
 3084: 	    my ($current_length,$min_length)=(0,0);
 3085: 	    if ($data=~/\\vskip/) {
 3086:                 my $newdata=$data;
 3087: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 3088: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 3089: 		foreach my $elementdata (@newdata) {
 3090: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 3091: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 3092:                     my @words=split(/ /,$elementdata);
 3093: 		    foreach my $word (@words) {
 3094: 			my $lengthword=2.5*&LATEX_length($word);
 3095: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 3096: 		    }
 3097: 		}
 3098: 	    } else {
 3099: 		$current_length=2.5*&LATEX_length($data);
 3100:                     my @words=split(/ /,$data);
 3101: 		    foreach my $word (@words) {
 3102: 			my $lengthword=2*&LATEX_length($word);
 3103: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 3104: 		    }
 3105: 	    }
 3106: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3107: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3108: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 3109: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 3110: 	}        
 3111:     }
 3112: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 3113: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 3114: 	}
 3115:     #make data bold
 3116:     $data='\textbf{'.$data.'}';
 3117:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 3118:     }
 3119:     return'';
 3120: }
 3121: 
 3122: sub end_th {
 3123:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3124:     my $currentstring = &end_p();	# Close any open <p> in the row.
 3125:     if ($target eq 'web' || $target eq 'webgrade') {
 3126: 	$currentstring .= $token->[2];     
 3127:     } elsif ($target eq 'tex') {
 3128:         $Apache::londefdef::TD_redirection =0;
 3129: 	&end_th_tex($parstack,$parser,$safeeval);
 3130:     }
 3131:     return $currentstring;
 3132: }
 3133:      
 3134: #-- <img> tag (end tag forbidden)
 3135: #
 3136: #  Render the <IMG> tag.
 3137: #     <IMG> has the following attributes (in addition to the 
 3138: #     standard HTML ones:
 3139: #      TeXwrap   - Governs how the tex target will try to wrap text around
 3140: #                  horizontally aligned images.
 3141: #      TeXwidth  - The width of the image when rendered for print (mm).
 3142: #      TeXheight - The height of the image when rendered for print (mm)
 3143: #         (Note there seems to also be support for this as a % of page size)
 3144: #      
 3145: sub start_img {
 3146:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
 3147:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 3148: 					 undef,1);
 3149:     if (! $src && 
 3150: 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
 3151: 	) { 
 3152: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 3153: 	return '';
 3154:     }
 3155:     &Apache::lonxml::extlink($src);
 3156:     my $currentstring = '';
 3157:     my $scaling = .3;
 3158: 
 3159:    # Render unto browsers that which are the browser's...
 3160: 
 3161:     if ($target eq 'web' || $target eq 'webgrade') {
 3162: 	if ($env{'browser.imagesuppress'} ne 'on') {
 3163: 	    my $enc = ('yes' eq 
 3164: 		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
 3165: 						     $safeeval)));
 3166: 	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
 3167: 							 $enc);
 3168: 	} else {
 3169: 	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
 3170: 						    undef,1);
 3171: 	    if (!$alttag) {
 3172: 		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3173: 						   $src);
 3174: 	    }
 3175: 	    $currentstring.='[IMAGE: '.$alttag.']';
 3176: 	}
 3177: 
 3178: 	# and render unto TeX that which is LaTeX
 3179: 
 3180:     } elsif ($target eq 'tex') {
 3181: 	#
 3182: 	#  The alignment will require some superstructure to be put around
 3183: 	#  the \includegraphics stuff.  At present we can only partially
 3184: 	#  simulate the alignments offered by html.
 3185: 	#
 3186: 	#
 3187: 	my $align = lc(&Apache::lonxml::get_param('align', 
 3188: 						  $parstack,
 3189: 						  $safeeval,
 3190: 						  undef,1));
 3191: 	if(!$align) {
 3192: 		$align = "bottom";	# This is html's default so it's ours too.
 3193: 	}
 3194: 	#
 3195: 	&Apache::lonxml::debug("Alignemnt = $align");
 3196: 	#  LaTeX's image/text wrapping is really bad since it wants to
 3197: 	#  make figures float.  
 3198:         #   The user has the optional parameter (applicable only to l/r
 3199: 	# alignment to use the picins/parpic directive to get wrapped text
 3200: 	# this is also imperfect.. that's why we give them a choice...
 3201: 	# so they can't yell at us for our choice.
 3202: 	#
 3203: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
 3204: 							    $parstack,
 3205: 							    $safeeval,
 3206: 							    undef,0);
 3207: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
 3208: 	if(!$latex_rendering) {
 3209: 		$latex_rendering = "texwrap";
 3210: 	}
 3211: 	# using texwrap inside a table does not work. So, if after all of this,
 3212: 	# texwrap is on, we turn it off if we detect we're in a table:
 3213: 	#
 3214: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 3215: 	    $latex_rendering = 'parpic';
 3216: 	}
 3217: 
 3218: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 3219: 
 3220: 	#if original bmp/gif/jpg/png file exist do following:
 3221: 	my $origsrc=$src;
 3222: 	my ($path,$file) = &get_eps_image($src);
 3223: 	# &Apache::lonnet::logthis("Image source: $src result: $path $file");
 3224: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 3225: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
 3226: 	if (-e $src) {
 3227: 	    &Apache::lonxml::debug("$src exists");
 3228: 	    my ($height_param,$width_param)=
 3229: 		&image_size($origsrc,0.3,$parstack,$safeeval);
 3230: 	    my $size;
 3231: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 3232: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 3233: 	    # Default size if not able to extract that (e.g. eps image).
 3234: 	    
 3235: 	    # &Apache::lonnet::logthis("Size = $size");
 3236: 	    
 3237: 	    $size='['.$size;
 3238: 	    $size=~s/,$/]/; 
 3239: 	    $currentstring .= '\graphicspath{{'.$path.'}}'
 3240: 		.'\includegraphics'.$size.'{'.$file.'} ';
 3241: 	    my $closure;
 3242: 	    ($currentstring, $closure) = &align_latex_image($align, 
 3243: 							    $latex_rendering, 
 3244: 							    $currentstring, 
 3245: 							    $width_param, 
 3246: 							    $height_param);
 3247: 	    $currentstring .= $closure;
 3248: 						
 3249: 	} else {
 3250: 	    &Apache::lonxml::debug("$src does not exist");
 3251: 	    #original image file doesn't exist so check the alt attribute
 3252: 	    my $alt = 
 3253: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 3254: 	    unless ($alt) {
 3255: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3256: 	    }
 3257: 
 3258: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
 3259: 	}
 3260: 
 3261: 	# And here's where the semi-quote breaks down: allow the user
 3262:         # to edit the beast as well by rendering the problem for edit:
 3263:     } elsif ($target eq 'edit') {
 3264:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
 3265: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 3266: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 3267: 	    &Apache::edit::browse('src',undef,'alt',$only).' '.
 3268: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 3269: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 3270: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 3271: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 3272: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 3273: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 3274: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
 3275: 						   ['','bottom','middle','top','left','right'],$token,5);
 3276: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 3277: 						   ['', 'none','parbox', 'parpic', 'wrapfigure'], $token, 2);
 3278: 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
 3279: 						   ['no','yes'], $token, 2);
 3280: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 3281: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 3282: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
 3283: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 3284: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 3285: 
 3286:         if ($token->[2]{'src'}=~/\$/) {
 3287:            $currentstring.='Variable image source';
 3288:         } else {
 3289: 	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 3290: 	   if ($width) { $currentstring.=' width="'.$width.'" '; }
 3291: 	   if ($height) { $currentstring.=' height="'.$height.'" '; }
 3292: 	   $currentstring .= ' />';
 3293:         }
 3294:     } elsif ($target eq 'modified') {
 3295: 	my ($osrc,$owidth,$oheight)=
 3296: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 3297: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 3298: 					     $safeeval,'src','alt','align',
 3299: 					     'TeXwidth','TeXheight', 'TeXwrap',
 3300: 					     'width','height','encrypturl');
 3301: 	my ($nsrc,$nwidth,$nheight)=
 3302: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 3303: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 3304: 	&image_replication($loc);
 3305: 	my ($iwidth,$iheight);
 3306: 	if (-e $loc) {
 3307: 	    my $image = Image::Magick->new;
 3308: 	    $image->Read($loc);
 3309: 	    ($iwidth, $iheight) = ($image->Get('width'),
 3310: 				   $image->Get('height'));
 3311: 	}
 3312: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 3313: 	    # changed image or no size specified,
 3314:             # if they didn't explicitly change the 
 3315:             # width or height use the ones from the image
 3316: 	    if ($iwidth && $iheight) {
 3317: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 3318: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 3319: 		}
 3320: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 3321: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 3322: 		}
 3323: 	    }
 3324: 	}
 3325: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 3326: 	# if we don't have a width or height
 3327: 	if ($iwidth && $cwidth && !$cheight) {
 3328: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 3329: 	}
 3330: 	if ($iheight && $cheight && !$cwidth) {
 3331: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 3332: 	}
 3333: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 3334:     }
 3335: 
 3336:     return $currentstring;
 3337: }
 3338: 
 3339: sub end_img {
 3340:     my ($target,$token) = @_;
 3341:     my $currentstring = '';
 3342:     if ($target eq 'web' || $target eq 'webgrade') {
 3343: 	$currentstring = $token->[2];
 3344:     } elsif ($target eq 'tex') {
 3345: 	$currentstring = '';
 3346:     }
 3347:     return $currentstring;
 3348: }
 3349: 
 3350: #-- <applet> tag (end tag required)
 3351: sub start_applet {
 3352:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3353:     
 3354:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 3355:     &Apache::lonxml::extlink($code);
 3356:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 3357: 					   undef,1);
 3358:     &Apache::lonxml::extlink($archive);
 3359:     my $currentstring = '';
 3360:     if ($target eq 'web' || $target eq 'webgrade') {
 3361: 	if ($env{'browser.appletsuppress'} ne 'on') {
 3362: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
 3363: 							  {'code'=>$code,
 3364: 							   'archive'=>$archive}
 3365: 							  );
 3366: 	} else {
 3367: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 3368: 						   $safeeval,undef,1);
 3369: 	    unless ($alttag) {
 3370: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3371: 						 $code);
 3372: 	    }
 3373: 	    $currentstring='[APPLET: '.$alttag.']';
 3374: 	}
 3375:     } elsif ($target eq 'tex') {
 3376: 	# Turn off some stuff we can't be inside thank you LaTeX
 3377: 	
 3378: 
 3379: 	my $restart_sub = 0;
 3380: 	my $restart_sup = 0;
 3381: 
 3382: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 3383: 	# using the start_ stop_ functions.. those tags only care about
 3384: 	# $target.
 3385: 
 3386: 	if (&is_inside_of($tagstack, "sub")) {
 3387: 	    $restart_sub = 1;
 3388: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 3389: 				       $parstack, $parser, $safeeval);
 3390: 	}
 3391: 	if (&is_inside_of($tagstack, "sup")) {
 3392: 	    $restart_sup = 1;
 3393: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 3394: 				       $parstack, $parser, $safeeval);
 3395: 	}
 3396: 
 3397: 	# Now process the applet; just replace it with its alt attribute.
 3398: 
 3399: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 3400: 					       $safeeval,undef,1);
 3401: 	unless ($alttag) {
 3402: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 3403: 						undef,1);
 3404: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3405: 					     $code);
 3406: 	}
 3407: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 3408: 	    '.}\end{center}';
 3409: 
 3410: 	# Turn stuff back on that we can't be inside of.
 3411: 
 3412: 	if ($restart_sub) {
 3413: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 3414: 					$parstack, $parser, $safeeval);
 3415: 	}
 3416: 	if ($restart_sup) {
 3417: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 3418: 					 $parstack, $parser, $safeeval);
 3419: 	}
 3420:     } 
 3421:     return $currentstring;
 3422: }
 3423: 
 3424: sub end_applet {
 3425:     my ($target,$token) = @_;
 3426:     my $currentstring = '';
 3427:     if ($target eq 'web' || $target eq 'webgrade') {
 3428: 	$currentstring = $token->[2];
 3429:     } elsif ($target eq 'tex') {
 3430:     } 
 3431:     return $currentstring;
 3432: }
 3433: 
 3434: #-- <embed> tag (end tag optional/required)
 3435: sub start_embed {    
 3436:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3437:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3438:     &Apache::lonxml::extlink($src);
 3439:     my $currentstring = '';
 3440:     if ($target eq 'web' || $target eq 'webgrade') {
 3441: 	if ($env{'browser.embedsuppress'} ne 'on') {
 3442: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
 3443: 	} else {
 3444: 	    my $alttag=&Apache::lonxml::get_param
 3445: 		('alt',$parstack,$safeeval,undef,1);
 3446: 	    unless ($alttag) {
 3447: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3448: 	    }
 3449: 	    $currentstring='[EMBED: '.$alttag.']';
 3450: 	}
 3451:     } elsif ($target eq 'tex') {
 3452:     } 
 3453:     return $currentstring;
 3454: }
 3455: 
 3456: sub end_embed {
 3457:     my ($target,$token) = @_;
 3458:     my $currentstring = '';
 3459:     if ($target eq 'web' || $target eq 'webgrade') {
 3460: 	$currentstring = $token->[2];     
 3461:     } elsif ($target eq 'tex') {  
 3462:     } 
 3463:     return $currentstring;
 3464: }
 3465: 
 3466: #-- <param> tag (end tag forbidden)
 3467: sub start_param {
 3468:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3469:     if (&Apache::lonxml::get_param('name',$parstack,
 3470: 				   $safeeval,undef,1)=~/^cabbase$/i) {
 3471: 	my $value=&Apache::lonxml::get_param('value',$parstack,
 3472: 					     $safeeval,undef,1);
 3473: 	&Apache::lonxml::extlink($value);
 3474:     } 
 3475:   
 3476:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3477:     &Apache::lonxml::extlink($src);
 3478:     my $currentstring = '';
 3479:     if ($target eq 'web' || $target eq 'webgrade') {
 3480: 	my %toconvert;
 3481: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3482: 	if ($src) { $toconvert{'src'}= $src; }
 3483: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
 3484: 					    undef,1);
 3485: 	if ($name=~/^cabbase$/i) {
 3486: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
 3487: 							   $safeeval,undef,1);
 3488: 	}
 3489: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
 3490:     } elsif ($target eq 'tex') {
 3491:     } 
 3492:     return $currentstring;
 3493: }
 3494: 
 3495: sub end_param {
 3496:     my ($target,$token) = @_;
 3497:     my $currentstring = '';
 3498:     if ($target eq 'web' || $target eq 'webgrade') {
 3499: 	$currentstring = $token->[2];     
 3500:     } elsif ($target eq 'tex') {
 3501:     } 
 3502:     return $currentstring;
 3503: }
 3504: 
 3505: #-- <allow> tag
 3506: sub start_allow {
 3507:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3508:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3509:     &Apache::lonxml::extlink($src);
 3510: 
 3511:     if ($target eq 'tex') { &image_replication($src); }
 3512:     my $result;
 3513:     if ($target eq 'edit') {
 3514: 	$result .=&Apache::edit::tag_start($target,$token);
 3515: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 3516: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 3517:     } elsif ($target eq 'modified') {
 3518: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 3519: 						     $safeeval,'src');
 3520: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 3521:     }
 3522:     return $result;
 3523: }
 3524: 
 3525: sub end_allow {
 3526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3527:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 3528:     return '';
 3529: }
 3530: 
 3531: #-- Frames (end tag required)
 3532: #-- <frameset>
 3533: sub start_frameset {
 3534:     my ($target,$token) = @_;
 3535:     my $currentstring = '';	# Close any pending para.
 3536:     if ($target eq 'web' || $target eq 'webgrade') { 
 3537: 	$currentstring = 
 3538: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 3539: 					   $Apache::londefdef::head,
 3540: 					   {'add_entries'    => $token->[2],
 3541: #					    'no_title'       => 1,
 3542: 					    'force_register' => 1,
 3543: 					    'frameset'       => 1,});
 3544: 
 3545:     }
 3546:     return $currentstring;
 3547: }
 3548: 
 3549: sub end_frameset {
 3550:     my ($target,$token) = @_;
 3551:     my $currentstring = '';
 3552:     if ($target eq 'web' || $target eq 'webgrade') {
 3553: 	$currentstring = $token->[2];
 3554:     }
 3555:     return $currentstring;
 3556: }
 3557: 
 3558: #-- <xmp> (end tag required)
 3559: sub start_xmp {
 3560:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3561:     my $currentstring = '';
 3562:     if ($target eq 'web' || $target eq 'webgrade') {
 3563: 	$currentstring .= $token->[4];
 3564:     } elsif ($target eq 'tex') {
 3565: 	$currentstring .= '\begin{verbatim}';
 3566:     } 
 3567:     return $currentstring;
 3568: }
 3569: 
 3570: sub end_xmp {
 3571:     my ($target,$token) = @_;
 3572:     my $currentstring = '';
 3573:     if ($target eq 'web' || $target eq 'webgrade') {
 3574: 	$currentstring .= $token->[2];
 3575:     } elsif ($target eq 'tex') {
 3576: 	$currentstring .= '\end{verbatim}';
 3577:     }
 3578:     return $currentstring;
 3579: }
 3580: 
 3581: #-- <pre> (end tag required)
 3582: sub start_pre {
 3583:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3584:     my $currentstring = &end_p();	# close off pending <p>
 3585:     if ($target eq 'web' || $target eq 'webgrade') {
 3586: 	$currentstring .= $token->[4];
 3587:     } elsif ($target eq 'tex') {
 3588: 	$currentstring .= '\begin{verbatim}';
 3589: 	&Apache::lonxml::disable_LaTeX_substitutions();
 3590:     } 
 3591:     return $currentstring;
 3592: }
 3593: 
 3594: sub end_pre {
 3595:     my ($target,$token) = @_;
 3596:     my $currentstring = '';
 3597:     if ($target eq 'web' || $target eq 'webgrade') {
 3598: 	$currentstring .= $token->[2];
 3599:     } elsif ($target eq 'tex') {
 3600: 	$currentstring .= '\end{verbatim}';
 3601: 	&Apache::lonxml::enable_LaTeX_substitutions();
 3602:     }
 3603:     return $currentstring;
 3604: }
 3605: 
 3606: #-- <insert>
 3607: sub start_insert {
 3608:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3609:     my $currentstring = '';
 3610:     if ($target eq 'web' || $target eq 'webgrade') {
 3611: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3612: 	$currentstring .= '<b>'.$display.'</b>';;
 3613:     }
 3614:     return $currentstring;
 3615: }
 3616: 
 3617: sub end_insert {
 3618:     my ($target,$token) = @_;
 3619:     my $currentstring = '';
 3620:     if ($target eq 'web' || $target eq 'webgrade') {
 3621: 	$currentstring .= '';
 3622:     }
 3623:     return $currentstring;
 3624: }
 3625: 
 3626: #-- <externallink>
 3627: sub start_externallink {
 3628:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3629:     my $currentstring = '';
 3630:     if ($target eq 'web' || $target eq 'webgrade') {
 3631: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3632: 	$currentstring .= '<b>'.$display.'</b>';;
 3633:     }
 3634:     return $currentstring;
 3635: }
 3636: 
 3637: sub end_externallink {
 3638:     my ($target,$token) = @_;
 3639:     my $currentstring = '';
 3640:     if ($target eq 'web' || $target eq 'webgrade') {
 3641: 	$currentstring .= '';
 3642:     }
 3643:     return $currentstring;
 3644: }
 3645: 
 3646: #-- <blankspace heigth="">
 3647: sub start_blankspace {
 3648:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3649:     my $currentstring = &end_p();	# closes off any unclosed <p>
 3650:     if ($target eq 'tex') {
 3651: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 3652: 	$currentstring .= '\vskip '.$howmuch.' ';
 3653:     }
 3654:     return $currentstring;
 3655: }
 3656: 
 3657: sub end_blankspace {
 3658:     my ($target,$token) = @_;
 3659:     my $currentstring = '';
 3660:     if ($target eq 'tex') {
 3661: 	$currentstring .= '';
 3662:     }
 3663:     return $currentstring;
 3664: }
 3665: 
 3666: #-- <abbr> tag (end tag required)
 3667: sub start_abbr {
 3668:     my ($target,$token) = @_;
 3669:     my $currentstring = '';
 3670:     if ($target eq 'web' || $target eq 'webgrade') {
 3671: 	$currentstring = $token->[4];     
 3672:     } 
 3673:     return $currentstring;
 3674: }
 3675: 
 3676: sub end_abbr {
 3677:     my ($target,$token) = @_;
 3678:     my $currentstring = '';
 3679:     if ($target eq 'web' || $target eq 'webgrade') {
 3680: 	$currentstring = $token->[2];    
 3681:     } 
 3682:     return $currentstring;
 3683: }
 3684: 
 3685: #-- <acronym> tag (end tag required)
 3686: sub start_acronym {
 3687:     my ($target,$token) = @_;
 3688:     my $currentstring = '';
 3689:     if ($target eq 'web' || $target eq 'webgrade') {
 3690: 	$currentstring = $token->[4];     
 3691:     } 
 3692:     return $currentstring;
 3693: }
 3694: 
 3695: sub end_acronym {
 3696:     my ($target,$token) = @_;
 3697:     my $currentstring = '';
 3698:     if ($target eq 'web' || $target eq 'webgrade') {
 3699: 	$currentstring = $token->[2];    
 3700:     } 
 3701:     return $currentstring;
 3702: }
 3703: 
 3704: #-- <area> tag (end tag forbidden)
 3705: sub start_area {
 3706:     my ($target,$token) = @_;
 3707:     my $currentstring = '';
 3708:     if ($target eq 'web' || $target eq 'webgrade') {
 3709: 	$currentstring = $token->[4];     
 3710:     } 
 3711:     return $currentstring;
 3712: }
 3713: 
 3714: sub end_area {
 3715:     my ($target,$token) = @_;
 3716:     my $currentstring = '';
 3717:     if ($target eq 'web' || $target eq 'webgrade') {
 3718: 	$currentstring = $token->[2];    
 3719:     } 
 3720:     return $currentstring;
 3721: }
 3722: 
 3723: #-- <base> tag (end tag forbidden)
 3724: sub start_base {
 3725:     my ($target,$token) = @_;
 3726:     my $currentstring = '';
 3727:     if ($target eq 'web' || $target eq 'webgrade') {
 3728: 	$currentstring = $token->[4];     
 3729:     }
 3730:     return $currentstring;
 3731: }
 3732: 
 3733: sub end_base {
 3734:     my ($target,$token) = @_;
 3735:     my $currentstring = '';
 3736:     if ($target eq 'web' || $target eq 'webgrade') {
 3737: 	$currentstring = $token->[2];    
 3738:     } 
 3739:     return $currentstring;
 3740: }
 3741: 
 3742: #-- <bdo> tag (end tag required)
 3743: sub start_bdo {
 3744:     my ($target,$token) = @_;
 3745:     my $currentstring = '';
 3746:     if ($target eq 'web' || $target eq 'webgrade') {
 3747: 	$currentstring = $token->[4];     
 3748:     } 
 3749:     return $currentstring;
 3750: }
 3751: 
 3752: sub end_bdo {
 3753:     my ($target,$token) = @_;
 3754:     my $currentstring = '';
 3755:     if ($target eq 'web' || $target eq 'webgrade') {
 3756: 	$currentstring = $token->[2];    
 3757:     } 
 3758:     return $currentstring;
 3759: }
 3760: 
 3761: #-- <bgsound> tag (end tag optional)
 3762: sub start_bgsound {
 3763:     my ($target,$token) = @_;
 3764:     my $currentstring = '';
 3765:     if ($target eq 'web' || $target eq 'webgrade') {
 3766: 	$currentstring = $token->[4];     
 3767:     } 
 3768:     return $currentstring;
 3769: }
 3770: 
 3771: sub end_bgsound {
 3772:     my ($target,$token) = @_;
 3773:     my $currentstring = '';
 3774:     if ($target eq 'web' || $target eq 'webgrade') {
 3775: 	$currentstring = $token->[2];    
 3776:     } 
 3777:     return $currentstring;
 3778: }
 3779: 
 3780: #-- <blink> tag (end tag required)
 3781: sub start_blink {
 3782:     my ($target,$token) = @_;
 3783:     my $currentstring = '';
 3784:     if ($target eq 'web' || $target eq 'webgrade') {
 3785: 	$currentstring = $token->[4];     
 3786:     } 
 3787:     return $currentstring;
 3788: }
 3789: 
 3790: sub end_blink {
 3791:     my ($target,$token) = @_;
 3792:     my $currentstring = '';
 3793:     if ($target eq 'web' || $target eq 'webgrade') {
 3794: 	$currentstring = $token->[2];    
 3795:     } 
 3796:     return $currentstring;
 3797: }
 3798: 
 3799: #-- <blockquote> tag (end tag required)
 3800: sub start_blockquote {
 3801:     my ($target,$token) = @_;
 3802:     my $currentstring = &end_p();	# Close any unclosed <p>
 3803:     if ($target eq 'web' || $target eq 'webgrade') {
 3804: 	$currentstring .= $token->[4];     
 3805:     } 
 3806:     if ($target eq 'tex') {
 3807: 	$currentstring .= '\begin{quote}';
 3808:     }
 3809:     return $currentstring;
 3810: }
 3811: 
 3812: sub end_blockquote {
 3813:     my ($target,$token) = @_;
 3814:     my $currentstring = '';
 3815:     if ($target eq 'web' || $target eq 'webgrade') {
 3816: 	$currentstring = $token->[2];    
 3817:     } 
 3818:     if ($target eq 'tex') {
 3819: 	$currentstring = '\end{quote}';
 3820:     }
 3821:     return $currentstring;
 3822: }
 3823: 
 3824: #-- <button> tag (end tag required)
 3825: sub start_button {
 3826:     my ($target,$token) = @_;
 3827:     my $currentstring = '';
 3828:     if ($target eq 'web' || $target eq 'webgrade') {
 3829: 	$currentstring = $token->[4];     
 3830:     } 
 3831:     return $currentstring;
 3832: }
 3833: 
 3834: sub end_button {
 3835:     my ($target,$token) = @_;
 3836:     my $currentstring = '';
 3837:     if ($target eq 'web' || $target eq 'webgrade') {
 3838: 	$currentstring = $token->[2];    
 3839:     } 
 3840:     return $currentstring;
 3841: }
 3842: 
 3843: #-- <caption> tag (end tag required)
 3844: sub start_caption {
 3845:     my ($target,$token) = @_;
 3846:     my $currentstring = '';
 3847:     if ($target eq 'web' || $target eq 'webgrade') {
 3848: 	$currentstring = $token->[4];     
 3849:     } 
 3850:     return $currentstring;
 3851: }
 3852: 
 3853: sub end_caption {
 3854:     my ($target,$token) = @_;
 3855:     my $currentstring = '';
 3856:     if ($target eq 'web' || $target eq 'webgrade') {
 3857: 	$currentstring = $token->[2];    
 3858:     } 
 3859:     return $currentstring;
 3860: }
 3861: 
 3862: #-- <col> tag (end tag forbdden)
 3863: sub start_col {
 3864:     my ($target,$token) = @_;
 3865:     my $currentstring = '';
 3866:     if ($target eq 'web' || $target eq 'webgrade') {
 3867: 	$currentstring = $token->[4];     
 3868:     } 
 3869:     return $currentstring;
 3870: }
 3871: 
 3872: sub end_col {
 3873:     my ($target,$token) = @_;
 3874:     my $currentstring = '';
 3875:     if ($target eq 'web' || $target eq 'webgrade') {
 3876: 	$currentstring = $token->[2];    
 3877:     } 
 3878:     return $currentstring;
 3879: }
 3880: 
 3881: #-- <colgroup> tag (end tag optional)
 3882: sub start_colgroup {
 3883:     my ($target,$token) = @_;
 3884:     my $currentstring = '';
 3885:     if ($target eq 'web' || $target eq 'webgrade') {
 3886: 	$currentstring = $token->[4];     
 3887:     } 
 3888:     return $currentstring;
 3889: }
 3890: 
 3891: sub end_colgroup {
 3892:     my ($target,$token) = @_;
 3893:     my $currentstring = '';
 3894:     if ($target eq 'web' || $target eq 'webgrade') {
 3895: 	$currentstring = $token->[2];    
 3896:     } 
 3897:     return $currentstring;
 3898: }
 3899: 
 3900: #-- <del> tag (end tag required)
 3901: sub start_del {
 3902:     my ($target,$token) = @_;
 3903:     my $currentstring = '';
 3904:     if ($target eq 'web' || $target eq 'webgrade') {
 3905: 	$currentstring = $token->[4];     
 3906:     } 
 3907:     return $currentstring;
 3908: }
 3909: 
 3910: sub end_del {
 3911:     my ($target,$token) = @_;
 3912:     my $currentstring = '';
 3913:     if ($target eq 'web' || $target eq 'webgrade') {
 3914: 	$currentstring = $token->[2];    
 3915:     } 
 3916:     return $currentstring;
 3917: }
 3918: 
 3919: #-- <fieldset> tag (end tag required)
 3920: sub start_fieldset {
 3921:     my ($target,$token) = @_;
 3922:     my $currentstring = '';
 3923:     if ($target eq 'web' || $target eq 'webgrade') {
 3924: 	$currentstring = $token->[4];     
 3925:     } 
 3926:     return $currentstring;
 3927: }
 3928: 
 3929: sub end_fieldset {
 3930:     my ($target,$token) = @_;
 3931:     my $currentstring = '';
 3932:     if ($target eq 'web' || $target eq 'webgrade') {
 3933: 	$currentstring = $token->[2];    
 3934:     } 
 3935:     return $currentstring;
 3936: }
 3937: 
 3938: #-- <frame> tag (end tag forbidden)
 3939: sub start_frame {
 3940:     my ($target,$token) = @_;
 3941:     my $currentstring = '';
 3942:     if ($target eq 'web' || $target eq 'webgrade') {
 3943: 	$currentstring = $token->[4];     
 3944:     } 
 3945:     return $currentstring;
 3946: }
 3947: 
 3948: sub end_frame {
 3949:     my ($target,$token) = @_;
 3950:     my $currentstring = '';
 3951:     if ($target eq 'web' || $target eq 'webgrade') {
 3952: 	$currentstring = $token->[2];    
 3953:     } 
 3954:     return $currentstring;
 3955: }
 3956: 
 3957: #-- <iframe> tag (end tag required)
 3958: sub start_iframe {
 3959:     my ($target,$token) = @_;
 3960:     my $currentstring = '';
 3961:     if ($target eq 'web' || $target eq 'webgrade') {
 3962: 	$currentstring = $token->[4];     
 3963:     } 
 3964:     return $currentstring;
 3965: }
 3966: 
 3967: sub end_iframe {
 3968:     my ($target,$token) = @_;
 3969:     my $currentstring = '';
 3970:     if ($target eq 'web' || $target eq 'webgrade') {
 3971: 	$currentstring = $token->[2];    
 3972:     } 
 3973:     return $currentstring;
 3974: }
 3975: 
 3976: #-- <ins> tag (end tag required)
 3977: sub start_ins {
 3978:     my ($target,$token) = @_;
 3979:     my $currentstring = '';
 3980:     if ($target eq 'web' || $target eq 'webgrade') {
 3981: 	$currentstring = $token->[4];     
 3982:     } 
 3983:     return $currentstring;
 3984: }
 3985: 
 3986: sub end_ins {
 3987:     my ($target,$token) = @_;
 3988:     my $currentstring = '';
 3989:     if ($target eq 'web' || $target eq 'webgrade') {
 3990: 	$currentstring = $token->[2];    
 3991:     } 
 3992:     return $currentstring;
 3993: }
 3994: 
 3995: #-- <isindex> tag (end tag forbidden)
 3996: sub start_isindex {
 3997:     my ($target,$token) = @_;
 3998:     my $currentstring = '';
 3999:     if ($target eq 'web' || $target eq 'webgrade') {
 4000: 	$currentstring = $token->[4];     
 4001:     } 
 4002:     return $currentstring;
 4003: }
 4004: 
 4005: sub end_isindex {
 4006:     my ($target,$token) = @_;
 4007:     my $currentstring = '';
 4008:     if ($target eq 'web' || $target eq 'webgrade') {
 4009: 	$currentstring = $token->[2];    
 4010:     } 
 4011:     return $currentstring;
 4012: }
 4013: 
 4014: #-- <keygen> tag (end tag forbidden)
 4015: sub start_keygen {
 4016:     my ($target,$token) = @_;
 4017:     my $currentstring = '';
 4018:     if ($target eq 'web' || $target eq 'webgrade') {
 4019: 	$currentstring = $token->[4];     
 4020:     } 
 4021:     return $currentstring;
 4022: }
 4023: 
 4024: sub end_keygen {
 4025:     my ($target,$token) = @_;
 4026:     my $currentstring = '';
 4027:     if ($target eq 'web' || $target eq 'webgrade') {
 4028: 	$currentstring = $token->[2];    
 4029:     } 
 4030:     return $currentstring;
 4031: }
 4032: 
 4033: #-- <label> tag
 4034: sub start_label {
 4035:     my ($target,$token) = @_;
 4036:     my $currentstring = '';
 4037:     if ($target eq 'web' || $target eq 'webgrade') {
 4038: 	$currentstring = $token->[4];     
 4039:     } 
 4040:     return $currentstring;
 4041: }
 4042: 
 4043: sub end_label {
 4044:     my ($target,$token) = @_;
 4045:     my $currentstring = '';
 4046:     if ($target eq 'web' || $target eq 'webgrade') {
 4047: 	$currentstring = $token->[2];    
 4048:     } 
 4049:     return $currentstring;
 4050: }
 4051: 
 4052: #-- <layer> tag (end tag required)
 4053: sub start_layer {
 4054:     my ($target,$token) = @_;
 4055:     my $currentstring = '';
 4056:     if ($target eq 'web' || $target eq 'webgrade') {
 4057: 	$currentstring = $token->[4];     
 4058:     } 
 4059:     return $currentstring;
 4060: }
 4061: 
 4062: sub end_layer {
 4063:     my ($target,$token) = @_;
 4064:     my $currentstring = '';
 4065:     if ($target eq 'web' || $target eq 'webgrade') {
 4066: 	$currentstring = $token->[2];    
 4067:     } 
 4068:     return $currentstring;
 4069: }
 4070: 
 4071: #-- <legend> tag (end tag required)
 4072: sub start_legend {
 4073:     my ($target,$token) = @_;
 4074:     my $currentstring = '';
 4075:     if ($target eq 'web' || $target eq 'webgrade') {
 4076: 	$currentstring = $token->[4];     
 4077:     } 
 4078:     return $currentstring;
 4079: }
 4080: 
 4081: sub end_legend {
 4082:     my ($target,$token) = @_;
 4083:     my $currentstring = '';
 4084:     if ($target eq 'web' || $target eq 'webgrade') {
 4085: 	$currentstring = $token->[2];    
 4086:     } 
 4087:     return $currentstring;
 4088: }
 4089: 
 4090: #-- <link> tag (end tag forbidden)
 4091: sub start_link {
 4092:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 4093:     my $currentstring = '';
 4094:     if ($target eq 'web' || $target eq 'webgrade') {
 4095: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 4096: 					    undef,1);
 4097: 	&Apache::lonxml::extlink($href);
 4098: 	$currentstring = $token->[4];     
 4099:     } 
 4100:     return $currentstring;
 4101: }
 4102: 
 4103: sub end_link {
 4104:     my ($target,$token) = @_;
 4105:     my $currentstring = '';
 4106:     if ($target eq 'web' || $target eq 'webgrade') {
 4107: 	$currentstring = $token->[2];    
 4108:     } 
 4109:     return $currentstring;
 4110: }
 4111: 
 4112: #-- <marquee> tag (end tag optional)
 4113: sub start_marquee {
 4114:     my ($target,$token) = @_;
 4115:     my $currentstring = '';
 4116:     if ($target eq 'web' || $target eq 'webgrade') {
 4117: 	$currentstring = $token->[4];     
 4118:     } 
 4119:     return $currentstring;
 4120: }
 4121: 
 4122: sub end_marquee {
 4123:     my ($target,$token) = @_;
 4124:     my $currentstring = '';
 4125:     if ($target eq 'web' || $target eq 'webgrade') {
 4126: 	$currentstring = $token->[2];    
 4127:     } 
 4128:     return $currentstring;
 4129: }
 4130: 
 4131: #-- <multicol> tag (end tag required)
 4132: sub start_multicol {
 4133:     my ($target,$token) = @_;
 4134:     my $currentstring = &end_p();	# Close any pending <p>
 4135:     if ($target eq 'web' || $target eq 'webgrade') {
 4136: 	$currentstring .= $token->[4];     
 4137:     } 
 4138:     return $currentstring;
 4139: }
 4140: 
 4141: sub end_multicol {
 4142:     my ($target,$token) = @_;
 4143:     my $currentstring = '';
 4144:     if ($target eq 'web' || $target eq 'webgrade') {
 4145: 	$currentstring = $token->[2];    
 4146:     } 
 4147:     return $currentstring;
 4148: }
 4149: 
 4150: #-- <nobr> tag (end tag required)
 4151: sub start_nobr {
 4152:     my ($target,$token) = @_;
 4153:     my $currentstring = '';
 4154:     if ($target eq 'web' || $target eq 'webgrade') {
 4155: 	$currentstring = $token->[4];     
 4156:     }  elsif ($target eq 'tex') {
 4157: 	$currentstring='\mbox{';
 4158:     }
 4159:     return $currentstring;
 4160: }
 4161: 
 4162: sub end_nobr {
 4163:     my ($target,$token) = @_;
 4164:     my $currentstring = '';
 4165:     if ($target eq 'web' || $target eq 'webgrade') {
 4166: 	$currentstring = $token->[2];    
 4167:     }   elsif ($target eq 'tex') {
 4168: 	$currentstring='}';
 4169:     }
 4170:     return $currentstring;
 4171: }
 4172: 
 4173: #-- <noembed> tag (end tag required)
 4174: sub start_noembed {
 4175:     my ($target,$token) = @_;
 4176:     my $currentstring = '';
 4177:     if ($target eq 'web' || $target eq 'webgrade') {
 4178: 	$currentstring = $token->[4];     
 4179:     } 
 4180:     return $currentstring;
 4181: }
 4182: 
 4183: sub end_noembed {
 4184:     my ($target,$token) = @_;
 4185:     my $currentstring = '';
 4186:     if ($target eq 'web' || $target eq 'webgrade') {
 4187: 	$currentstring = $token->[2];    
 4188:     } 
 4189:     return $currentstring;
 4190: }
 4191: 
 4192: #-- <noframes> tag (end tag required)
 4193: sub start_noframes {
 4194:     my ($target,$token) = @_;
 4195:     my $currentstring = '';
 4196:     if ($target eq 'web' || $target eq 'webgrade') {
 4197: 	$currentstring = $token->[4];     
 4198:     } 
 4199:     return $currentstring;
 4200: }
 4201: 
 4202: sub end_noframes {
 4203:     my ($target,$token) = @_;
 4204:     my $currentstring = '';
 4205:     if ($target eq 'web' || $target eq 'webgrade') {
 4206: 	$currentstring = $token->[2];    
 4207:     } 
 4208:     return $currentstring;
 4209: }
 4210: 
 4211: #-- <nolayer> tag (end tag required)
 4212: sub start_nolayer {
 4213:     my ($target,$token) = @_;
 4214:     my $currentstring = '';
 4215:     if ($target eq 'web' || $target eq 'webgrade') {
 4216: 	$currentstring = $token->[4];     
 4217:     } 
 4218:     return $currentstring;
 4219: }
 4220: 
 4221: sub end_nolayer {
 4222:     my ($target,$token) = @_;
 4223:     my $currentstring = '';
 4224:     if ($target eq 'web' || $target eq 'webgrade') {
 4225: 	$currentstring = $token->[2];    
 4226:     } 
 4227:     return $currentstring;
 4228: }
 4229: 
 4230: #-- <noscript> tag (end tag required)
 4231: sub start_noscript {
 4232:     my ($target,$token) = @_;
 4233:     my $currentstring = '';
 4234:     if ($target eq 'web' || $target eq 'webgrade') {
 4235: 	$currentstring = $token->[4];     
 4236:     } 
 4237:     return $currentstring;
 4238: }
 4239: 
 4240: sub end_noscript {
 4241:     my ($target,$token) = @_;
 4242:     my $currentstring = '';
 4243:     if ($target eq 'web' || $target eq 'webgrade') {
 4244: 	$currentstring = $token->[2];    
 4245:     } 
 4246:     return $currentstring;
 4247: }
 4248: 
 4249: #-- <object> tag (end tag required)
 4250: sub start_object {
 4251:     my ($target,$token) = @_;
 4252:     my $currentstring = '';
 4253:     if ($target eq 'web' || $target eq 'webgrade') {
 4254: 	$currentstring = $token->[4];     
 4255:     } 
 4256:     return $currentstring;
 4257: }
 4258: 
 4259: sub end_object {
 4260:     my ($target,$token) = @_;
 4261:     my $currentstring = '';
 4262:     if ($target eq 'web' || $target eq 'webgrade') {
 4263: 	$currentstring = $token->[2];    
 4264:     } 
 4265:     return $currentstring;
 4266: }
 4267: 
 4268: #-- <optgroup> tag (end tag required)
 4269: sub start_optgroup {
 4270:     my ($target,$token) = @_;
 4271:     my $currentstring = '';
 4272:     if ($target eq 'web' || $target eq 'webgrade') {
 4273: 	$currentstring = $token->[4];     
 4274:     } 
 4275:     return $currentstring;
 4276: }
 4277: 
 4278: sub end_optgroup {
 4279:     my ($target,$token) = @_;
 4280:     my $currentstring = '';
 4281:     if ($target eq 'web' || $target eq 'webgrade') {
 4282: 	$currentstring = $token->[2];    
 4283:     } 
 4284:     return $currentstring;
 4285: }
 4286: 
 4287: #-- <samp> tag (end tag required)
 4288: sub start_samp {
 4289:     my ($target,$token) = @_;
 4290:     my $currentstring = '';
 4291:     if ($target eq 'web' || $target eq 'webgrade') {
 4292: 	$currentstring = $token->[4];     
 4293:     } elsif ($target eq 'tex') {
 4294: 	$currentstring='\texttt{';
 4295:     }
 4296:     return $currentstring;
 4297: }
 4298: 
 4299: sub end_samp {
 4300:     my ($target,$token) = @_;
 4301:     my $currentstring = '';
 4302:     if ($target eq 'web' || $target eq 'webgrade') {
 4303: 	$currentstring = $token->[2];    
 4304:     } elsif ($target eq 'tex') {
 4305: 	$currentstring='}';
 4306:     }
 4307:     return $currentstring;
 4308: }
 4309: 
 4310: #-- <server> tag
 4311: sub start_server {
 4312:     my ($target,$token) = @_;
 4313:     my $currentstring = '';
 4314:     if ($target eq 'web' || $target eq 'webgrade') {
 4315: 	$currentstring = $token->[4];     
 4316:     } 
 4317:     return $currentstring;
 4318: }
 4319: 
 4320: sub end_server {
 4321:     my ($target,$token) = @_;
 4322:     my $currentstring = '';
 4323:     if ($target eq 'web' || $target eq 'webgrade') {
 4324: 	$currentstring = $token->[2];    
 4325:     } 
 4326:     return $currentstring;
 4327: }
 4328: 
 4329: #-- <spacer> tag (end tag forbidden)
 4330: sub start_spacer {
 4331:     my ($target,$token) = @_;
 4332:     my $currentstring = &end_p();	# Close off any open <p> tag.
 4333:     if ($target eq 'web' || $target eq 'webgrade') {
 4334: 	$currentstring .= $token->[4];     
 4335:     } 
 4336:     return $currentstring;
 4337: }
 4338: 
 4339: sub end_spacer {
 4340:     my ($target,$token) = @_;
 4341:     my $currentstring = '';
 4342:     if ($target eq 'web' || $target eq 'webgrade') {
 4343: 	$currentstring = $token->[2];    
 4344:     } 
 4345:     return $currentstring;
 4346: }
 4347: 
 4348: #-- <span> tag (end tag required)
 4349: sub start_span {
 4350:     my ($target,$token) = @_;
 4351:     my $currentstring = '';
 4352:     if ($target eq 'web' || $target eq 'webgrade') {
 4353: 	$currentstring = $token->[4];     
 4354:     } 
 4355:     return $currentstring;
 4356: }
 4357: 
 4358: sub end_span {
 4359:     my ($target,$token) = @_;
 4360:     my $currentstring = '';
 4361:     if ($target eq 'web' || $target eq 'webgrade') {
 4362: 	$currentstring = $token->[2];    
 4363:     } 
 4364:     return $currentstring;
 4365: }
 4366: 
 4367: #-- <tbody> tag (end tag optional)
 4368: sub start_tbody {
 4369:     my ($target,$token) = @_;
 4370:     my $currentstring = '';
 4371:     if ($target eq 'web' || $target eq 'webgrade') {
 4372: 	$currentstring = $token->[4];     
 4373:     } 
 4374:     return $currentstring;
 4375: }
 4376: 
 4377: sub end_tbody {
 4378:     my ($target,$token) = @_;
 4379:     my $currentstring = '';
 4380:     if ($target eq 'web' || $target eq 'webgrade') {
 4381: 	$currentstring = $token->[2];    
 4382:     } 
 4383:     return $currentstring;
 4384: }
 4385: 
 4386: #-- <tfoot> tag (end tag optional)
 4387: sub start_tfoot {
 4388:     my ($target,$token) = @_;
 4389:     my $currentstring = '';
 4390:     if ($target eq 'web' || $target eq 'webgrade') {
 4391: 	$currentstring = $token->[4];     
 4392:     } 
 4393:     return $currentstring;
 4394: }
 4395: 
 4396: sub end_tfoot {
 4397:     my ($target,$token) = @_;
 4398:     my $currentstring = '';
 4399:     if ($target eq 'web' || $target eq 'webgrade') {
 4400: 	$currentstring = $token->[2];    
 4401:     } 
 4402:     return $currentstring;
 4403: }
 4404: 
 4405: #-- <thead> tag (end tag optional)
 4406: sub start_thead {
 4407:     my ($target,$token) = @_;
 4408:     my $currentstring = '';
 4409:     if ($target eq 'web' || $target eq 'webgrade') {
 4410: 	$currentstring = $token->[4];     
 4411:     } 
 4412:     return $currentstring;
 4413: }
 4414: 
 4415: sub end_thead {
 4416:     my ($target,$token) = @_;
 4417:     my $currentstring = '';
 4418:     if ($target eq 'web' || $target eq 'webgrade') {
 4419: 	$currentstring = $token->[2];    
 4420:     } 
 4421:     return $currentstring;
 4422: }
 4423: 
 4424: #-- <var> tag
 4425: sub start_var {
 4426:     my ($target,$token) = @_;
 4427:     my $currentstring = '';
 4428:     if ($target eq 'web' || $target eq 'webgrade') {
 4429: 	$currentstring = $token->[4];     
 4430:     } elsif ($target eq 'tex') {
 4431: 	$currentstring = '\textit{'; 
 4432:     }
 4433:     return $currentstring;
 4434: }
 4435: 
 4436: sub end_var {
 4437:     my ($target,$token) = @_;
 4438:     my $currentstring = '';
 4439:     if ($target eq 'web' || $target eq 'webgrade') {
 4440: 	$currentstring = $token->[2];
 4441:     } elsif ($target eq 'tex') {
 4442: 	$currentstring = '}'; 
 4443:     } 
 4444:     return $currentstring;
 4445: }
 4446: 
 4447: #-- <wbr> tag (end tag forbidden)
 4448: sub start_wbr {
 4449:     my ($target,$token) = @_;
 4450:     my $currentstring = '';
 4451:     if ($target eq 'web' || $target eq 'webgrade') {
 4452: 	$currentstring = $token->[4];     
 4453:     } 
 4454:     return $currentstring;
 4455: }
 4456: 
 4457: sub end_wbr {
 4458:     my ($target,$token) = @_;
 4459:     my $currentstring = '';
 4460:     if ($target eq 'web' || $target eq 'webgrade') {
 4461: 	$currentstring = $token->[2];    
 4462:     } 
 4463:     return $currentstring;
 4464: }
 4465: 
 4466: #-- <hideweboutput> tag
 4467: sub start_hideweboutput {
 4468:     my ($target,$token) = @_;
 4469:     if ($target eq 'web' || $target eq 'webgrade') {
 4470: 	&Apache::lonxml::startredirection();     
 4471:     } 
 4472:     return '';
 4473: }
 4474: 
 4475: sub end_hideweboutput {
 4476:     my ($target,$token) = @_;
 4477:     my $currentstring = '';
 4478:     if ($target eq 'web' || $target eq 'webgrade') {
 4479: 	$currentstring = &Apache::lonxml::endredirection();    
 4480:     } 
 4481:     return '';
 4482: }
 4483: 
 4484: 
 4485: sub image_replication {
 4486:     my $src = shift;
 4487:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 4488:     #replicates eps or ps 
 4489:     my $epssrc = my $pssrc = $src;
 4490:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 4491:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 4492:     if (not -e $epssrc && not -e $pssrc) {
 4493: 	my $result=&Apache::lonnet::repcopy($epssrc);
 4494: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
 4495:     }
 4496:     return '';
 4497: }
 4498: 
 4499: 
 4500: 
 4501: sub resize_image {
 4502:     my ($height_param, $width_param, $scaling,
 4503: 	$parstack, $safeeval, $depth, $cis) = @_;
 4504: 
 4505:     # First apply the scaling...
 4506: 
 4507:     $height_param = $height_param * $scaling;
 4508:     $width_param  = $width_param  * $scaling;
 4509: 
 4510:     #do we have any specified LaTeX size of the picture?
 4511:     my $toget='TeXwidth'; 
 4512:     if ($cis) { 
 4513: 	$toget=lc($toget); 
 4514:     }
 4515:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 4516: 					      $safeeval,$depth,$cis);
 4517:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
 4518:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
 4519: 					       $safeeval,$depth,$cis);
 4520:     #do we have any specified web size of the picture?
 4521:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 4522: 					   $depth,1);
 4523:     if ($TeXwidth) { 
 4524: 	my $old_width_param=$width_param;
 4525: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 4526: 	    $width_param = $1*$env{'form.textwidth'}/100;
 4527: 	} else { 
 4528: 	    $width_param = $TeXwidth;
 4529: 	}
 4530: 	if ($TeXheight) {
 4531: 	    $height_param = $TeXheight;
 4532: 	} elsif ($old_width_param) {
 4533: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 4534: 	}
 4535:     } elsif ($TeXheight) {
 4536: 	$height_param = $TeXheight;
 4537: 	if ($height_param) {
 4538: 	    $width_param  = $TeXheight/$height_param*$width_param;
 4539: 	}
 4540:     } elsif ($width) {
 4541: 	my $old_width_param=$width_param;
 4542: 	$width_param = $width*$scaling;
 4543: 	if ($old_width_param) {
 4544: 	    $height_param=$width_param/$old_width_param*$height_param;
 4545: 	}
 4546:     }
 4547:     if ($width_param > $env{'form.textwidth'}) {
 4548:         my $old_width_param=$width_param;
 4549: 	$width_param =0.95*$env{'form.textwidth'};
 4550: 	if ($old_width_param) {
 4551: 	    $height_param=$width_param/$old_width_param*$height_param;
 4552: 	}
 4553:     }
 4554: 
 4555:     return ($height_param, $width_param);
 4556: }
 4557: 
 4558: sub image_size {
 4559:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 4560: 
 4561:     #size of image from gif/jpg/jpeg/png 
 4562:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4563:     if (-e $ressrc) {
 4564: 	$src = $ressrc;
 4565:     }
 4566:     my $image = Image::Magick->new;
 4567:     my $current_figure = $image->Read($src);
 4568:     my $width_param = $image->Get('width');
 4569:     my $height_param = $image->Get('height');
 4570:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
 4571:     undef($image);
 4572: 
 4573:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
 4574: 						  $scaling, $parstack, $safeeval, 
 4575: 						  $depth, $cis);
 4576: 
 4577:     return ($height_param, $width_param);
 4578: }
 4579: 
 4580: sub image_width {
 4581:     my ($height, $width) = &image_size(@_);
 4582:     return $width;
 4583: }
 4584: #  Not yet 100% sure this is correct in all circumstances..
 4585: #  due to my uncertainty about mods to image_size.
 4586: #
 4587: sub image_height {
 4588:     my ($height, $width) = &image_size(@_);
 4589:     return $height;
 4590: }
 4591: 
 4592: sub get_eps_image {
 4593:     my ($src)=@_;
 4594:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
 4595: 
 4596:     # In order to prevent the substitution of the alt text, we need to
 4597:     # be sure the orig_src file is on system now so:
 4598: 
 4599:     if (! -e $orig_src) {
 4600: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
 4601:     }
 4602:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
 4603:     my ($spath, $sname, $sext) = &fileparse($src, qr/\.(bmp|gif|png|jpg|jpeg)/i);
 4604:     $src=~s/\.(bmp|gif|png|jpg|jpeg)$/\.eps/i;
 4605:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4606:     &Apache::lonxml::debug("Filelocation gives: $src");
 4607:     if (! -e $src) {
 4608: 	&Apache::lonxml::debug("$src does not exist");
 4609: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
 4610: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 4611: 	    #if replication failed try to find ps file
 4612: 	    $src=~s/\.eps$/\.ps/;
 4613: 	    &Apache::lonxml::debug("Now looking for $src");
 4614: 	    #if no ps file try to replicate it.
 4615: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
 4616: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
 4617: 	    if ( (not -e $src) ||
 4618: 		($didrepcopy ne 'ok')) {
 4619: 		&Apache::lonxml::debug("Failed to find or replicate $src");
 4620: 
 4621: 		#if replication failed try to produce eps file dynamically
 4622: 		$src=~s/\.ps$/\.eps/;
 4623: 		my $temp_file;
 4624: 		open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4625: 		my $newsrc=$orig_src;
 4626: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
 4627: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
 4628: 		print FILE ("$newsrc\n");
 4629: 		close(FILE);
 4630: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4631: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4632: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
 4633: 		    $src =~ s/\.eps$/$sext.eps/;
 4634: 		}
 4635: 
 4636: 	    }
 4637: 
 4638: 	}
 4639:     } else {
 4640: 	# If the postscript file has spaces in its name,
 4641: 	# LaTeX will gratuitiously vomit.  Therefore
 4642: 	# queue such files for copy with " " replaced by "_".
 4643: 	# printout.pm will know them by their .ps  or .eps extensions.
 4644: 	my $newsrc = $orig_src;
 4645: 	$newsrc    =~  s|(.*)/res/|/home/httpd/html/res/|;
 4646: 	open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4647: 	print FILE "$src\n";
 4648: 	close FILE;
 4649: 	$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4650: 	$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4651:     }
 4652:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
 4653:     $path =~ s/ /\_/g;
 4654:     $file =~ s/ /\_/g;
 4655:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
 4656:     return ($path.'/',$file);
 4657: }
 4658: 
 4659: sub eps_generation {
 4660:     my ($src,$file,$width_param) = @_;	     
 4661:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 4662:     my $temp_file = Apache::File->new('>>'.$filename); 
 4663:     print $temp_file "$src\n";
 4664:     my $newsrc = $src;
 4665:     $newsrc =~ s/(\.bmp|\.gif|\.jpg|\.jpeg)$/\.eps/i;
 4666:     $newsrc=~s{/home/httpd/html/res}{};
 4667:     $newsrc=~s{/home/($LONCAPA::username_re)/public_html/}{/$1/};
 4668:     $newsrc=~s{/\./}{/};
 4669:     $newsrc=~s{/([^/]+)\.(ps|eps)}{/};
 4670:     if ($newsrc=~m{/home/httpd/lonUsers/}) {
 4671: 	$newsrc=~s{/home/httpd/lonUsers}{};
 4672: 	$newsrc=~s{/($LONCAPA::domain_re)/./././}{/$1/};
 4673:     }
 4674:     if ($newsrc=~m{/userfiles/}) {
 4675: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4676:     } else {
 4677: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4678:     }
 4679: }
 4680: 
 4681: sub file_path {     
 4682:     my $src=shift;
 4683:     my ($file,$path); 
 4684:     if ($src =~ m!(.*)/([^/]*)$!) {
 4685: 	$file = $2; 
 4686: 	$path = $1.'/'; 
 4687:     } 
 4688:     return $file,$path;
 4689: }
 4690: 
 4691: 
 4692: sub recalc {
 4693:     my $argument = shift;
 4694:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 4695:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 4696:     my $value=$1;
 4697:     my $units=$2;
 4698:     if ($units eq 'cm') {
 4699: 	$value*=10;
 4700:     } elsif ($units eq 'in') {
 4701: 	$value*=25.4;
 4702:     } elsif ($units eq 'pc') {
 4703: 	$value*=(25.4*12/72.27);
 4704:     } elsif ($units eq 'pt') {
 4705: 	$value*=(25.4/72.27);
 4706:     }
 4707:     return $value.' mm';
 4708: }
 4709: 
 4710: sub LATEX_length {
 4711:     my $garbage=shift;
 4712:     $garbage=~s/^\s+$//;
 4713:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 4714:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 4715:     $garbage=~s/(\s)+/$1/;#only one space
 4716:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 4717:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 4718:     $garbage=~s/([^\\])\$/$1/g;#$
 4719:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 4720:    $garbage=~s/(\\alpha|\\beta|\\gamma|\\delta|\\epsilon|\\verepsilon|\\zeta|\\eta|\\theta|\\vartheta|\\iota|\\kappa|\\lambda|\\mu|\\nu|\\xi|\\pi|\\varpi|\\rho|\\varrho|\\sigma|\\varsigma|\\tau|\\upsilon|\\phi|\\varphi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)/1/g;
 4721:     $garbage=~s/(\\pm|\\mp|\\times|\\div|\\cdot|\\ast|\\star|\\dagger|\\ddagger|\\amalg|\\cap|\\cup|\\uplus|\\sqcap|\\sqcup|\\vee|\\wedge|\\oplus|\\ominus|\\otimes|\\circ|\\bullet|\\diamond|\\lhd|\\rhd|\\unlhd|\\unrhd|\\oslash|\\odot|\\bigcirc|\\Box|\\Diamond|\\bigtriangleup|\\bigtriangledown|\\triangleleft|\\triangleright|\\setminus|\\wr)/1/g;
 4722:     $garbage=~s/(\\le|\\ll|\\leq|\\ge|\\geq|\\gg|\\neq|\\doreq|\\sim|\\simeq|\\subset|\\subseteq|\\sqsubset|\\sqsubseteq|\\in|\\vdash|\\models|\\supset|\\supseteq|\\sqsupset|\\sqsupseteq|\\ni|\\dash|\\perp|\\approx|\\cong|\\equiv|\\propto|\\prec|\\preceq|\\parallel|\\asymp|\\smile|\\frown|\\bowtie|\\succ|\\succeq|\\mid)/1/g;
 4723:     $garbage=~s/(\\not<|\\\\not\\le|\\not\\prec|\\not\\preceq|\\not\\subset|\\not\\subseteq|\\not\\sqsubseteq|\\not\\in|\\not>|\\not\\ge|\\not\\succ|\\notsucceq|\\not\\supset|\\notsupseteq|\\not\\sqsupseteq|\\notin|\\not=|\\not\\equiv|\\not\\sim|\\not\\simeq|\\not\\approx|\\not\\cong|\\not\\asymp)/1/g;
 4724:     $garbage=~s/(\\leftarrow|\\gets|\\Leftarrow|\\rightarrow|\\to|\\Rightarrow|\\leftrightarrow|\\Leftrightarrow|\\mapsto|\\hookleftarrow|\\leftharpoonup|\\leftkarpoondown|\\rightleftharpoons|\\longleftarrow|\\Longleftarrow|\\longrightarrow|\\Longrightarrow|\\longleftrightarrow|\\Longleftrightarrow|\\longmapsto|\\hookrightarrow|\\rightharpoonup|\\rightharpoondown|\\uparrow|\\Uparrow|\\downarrow|\\Downarrow|\\updownarrow|\\Updownarrow|\\nearrow|\\searrow|\\swarrow|\\nwarrow)/11/g;
 4725:     $garbage=~s/(\\aleph|\\hbar|\\imath|\\jmath|\\ell|\\wp|\\Re|\\Im|\\mho|\\prime|\\emptyset|\\nabla|\\surd|\\partial|\\top|\\bot|\\vdash|\\dashv|\\forall|\\exists|\\neg|\\flat|\\natural|\\sharp|\\\||\\angle|\\backslash|\\Box|\\Diamond|\\triangle|\\clubsuit|\\diamondsuit|\\heartsuit|\\spadesuit|\\Join|\\infty)/11/g;
 4726:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 4727:     #remove some other LaTeX command
 4728:     $garbage=~s|\\(\w+)\\|\\|g;	 
 4729:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 4730:     $garbage=~s|\+|11|g;
 4731:     my  $value=length($garbage);
 4732:     return $value;
 4733: }
 4734: 
 4735: 
 4736: sub align_latex_image {
 4737:     my ($align, $latex_rendering, $image, $width, $height) = @_;
 4738:     my $currentstring;        # The 1/2 wrapped image.
 4739:     my $closure;              # The closure of the wrappage.
 4740: 
 4741:     # if it's none just return it back
 4742:     if ($latex_rendering eq 'none') {
 4743: 	return ($image,'');
 4744:     }
 4745: 
 4746:     #    If there's an alignment specification we need to honor it here.
 4747:     #    For the horizontal alignments, we will also honor the
 4748:     #    value of the latex specfication.  The default is parbox,
 4749:     #    and that's used for illegal values too.  
 4750:     #    
 4751:     #    Even though we set a default alignment value, the user
 4752:     #    could have given us an illegal value.  In that case we
 4753:     #    just use the default alignment of bottom..
 4754:     $currentstring = '';
 4755:     if      ($align eq "top")    {
 4756: 	$currentstring .= '\raisebox{-'.$height.'mm}{'.$image;
 4757: 	$closure = '}';
 4758:     } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
 4759: 	my $offset = $height/2;
 4760: 	$currentstring .= '\raisebox{-'.$offset.'mm}{'.$image;
 4761: 	$closure       = '}';
 4762:     } elsif ($align eq "left")   { 
 4763: 	if ($latex_rendering eq "parpic") { 
 4764: 	    $currentstring .= '\parpic[l]{'.$image;
 4765: 	    $closure       = '}';
 4766: 	} elsif ($latex_rendering eq "parbox") {
 4767: 	    $currentstring .= '\begin{minipage}[l]{'.$width.'mm}'
 4768: 		.$image;
 4769: 	    $closure = '\end{minipage}';
 4770: 	} elsif ($latex_rendering eq "wrapfigure"
 4771: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4772: 	    $currentstring .= 
 4773: 		'\begin{wrapfigure}{l}{'.$width.'mm}'
 4774: 		.'\scalebox{1.0}{'.$image;
 4775: 	    $closure = '}\end{wrapfigure}';
 4776: 	}
 4777:     } elsif ($align eq "right")  {   
 4778: 	if ($latex_rendering eq "parpic") {
 4779: 	    $currentstring .= '\parpic[r]{'.$image;
 4780: 	    $closure = '}';
 4781: 	} elsif ($latex_rendering eq "parbox") {
 4782: 	    $currentstring .=  '\begin{minipage}[r]{'.$width.'mm}'
 4783: 		.$image;
 4784: 	    $closure = '\end{minipage}';
 4785: 	} elsif ($latex_rendering eq "wrapfigure"
 4786: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4787: 	    $currentstring .= 
 4788: 		'\begin{wrapfigure}{r}{'.$width.'mm}'
 4789: 		.'\scalebox{1.0}{'.$image;
 4790: 	    $closure = '}\end{wrapfigure}';
 4791: 	}
 4792:     } else {		# Bottom is also default.
 4793: 	# $currentstring = '\raisebox{'.$height.'mm}{'.$image.'}';
 4794: 	$currentstring .= "{$image";
 4795: 	$closure       = '}';
 4796:     }
 4797:     return ($currentstring, $closure);
 4798: }
 4799: 
 4800: 
 4801: sub is_inside_of {
 4802:     my ($tagstack, $tag) = @_;
 4803:     my @stack = @$tagstack;
 4804:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
 4805: 	if ($stack[$i] eq $tag) {
 4806: 	    return 1;
 4807: 	}
 4808:     }
 4809:     return 0;
 4810: }
 4811: 
 4812: 
 4813: #
 4814: #   This sub provides the typical LaTeX prefix matter for tex output:
 4815: #
 4816: sub latex_header {
 4817:     my ($mode) = @_;
 4818:     my $currentstring = '';
 4819: 
 4820:     $currentstring .= 
 4821: 	"\n% &Apache::lonxml::londefdef \n" .
 4822: 	'\documentclass[letterpaper,twoside]{article}\raggedbottom';
 4823:     if (($env{'form.latex_type'}=~'batchmode') ||
 4824: 	(!$env{'request.role.adv'}) || 
 4825: 	($mode eq 'batchmode')) {$currentstring .='\batchmode';} 
 4826:     $currentstring .= '\newcommand{\keephidden}[1]{}'.
 4827: 	'\renewcommand{\deg}{$^{\circ}$}'.
 4828: 	'\usepackage{multirow}'.
 4829: 	'\usepackage{longtable}'.
 4830: 	'\usepackage{textcomp}'.
 4831: 	'\usepackage{makeidx}'.
 4832: 	'\usepackage[dvips]{graphicx}'.
 4833: 	'\usepackage{wrapfig}'.
 4834: 	'\usepackage{picins}'.
 4835: 	'\usepackage[T1]{fontenc}'."\n".
 4836: 	'\usepackage{lmodern}'."\n".
 4837: 	'\usepackage[postscript]{ucs}'."\n".
 4838: 	'\usepackage[utf8x]{inputenc}'."\n".
 4839: 	'\usepackage{pifont}' ."\n".
 4840: 	'\usepackage{latexsym}'."\n".
 4841: 	'\usepackage{epsfig}'.
 4842: 	"\\usepackage{xtab}\n".
 4843: 	"\\usepackage{tabularx}\n".
 4844: 	"\\usepackage{booktabs}\n".
 4845: 	"\\usepackage{array}\n".
 4846: 	"\\usepackage{colortbl}\n".
 4847: 	"\\usepackage{xcolor}\n".
 4848: 	'\usepackage{calc}'.
 4849: 	'\usepackage{amsmath}'.
 4850: 	'\usepackage{amssymb}'.
 4851: 	'\usepackage{amsfonts}'.
 4852: 	'\usepackage{amsthm}'.
 4853: 	'\usepackage{amscd}'
 4854:         .'\usepackage{picins}\usepackage{calc}'."\n". # From lonprintout.pm
 4855: 	'\usepackage[T1]{fontenc}'."\n".
 4856: 	'\usepackage{lmodern}'."\n".
 4857: 	'\usepackage[postscript]{ucs}'."\n".
 4858: 	'\usepackage[utf8x]{inputenc}'."\n".
 4859: 	'\usepackage{pifont}'  . "\n";
 4860: 	
 4861:     if($env{'form.pdfFormFields'} eq 'yes') {
 4862: 	$currentstring .= '\usepackage{hyperref}'.
 4863: 	    '\usepackage{eforms}'.
 4864: 	    '\usepackage{tabularx}';
 4865:     } 
 4866:     
 4867:         $currentstring .= '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'.
 4868:                           '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large\textbf{Index}} \newline \setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}';
 4869:     $currentstring .= '\begin{document}';
 4870:     
 4871:     return $currentstring;
 4872: 
 4873: }
 4874: 
 4875: =pod
 4876: 
 4877: =head1 NAME
 4878: 
 4879: Apache::londefdef.pm
 4880: 
 4881: =head1 SYNOPSIS
 4882: 
 4883: Tags Default Definition Module
 4884: 
 4885: This is part of the LearningOnline Network with CAPA project
 4886: described at http://www.lon-capa.org.
 4887: 
 4888: 
 4889: =head1 NOTABLE SUBROUTINES
 4890: 
 4891: =over
 4892: 
 4893: =item start_hideweboutput()
 4894: 
 4895: =item end_hideweboutput()
 4896: 
 4897: =item image_replication()
 4898: 
 4899: =item resize_image()
 4900: 
 4901: 	Get correct sizing parameter for an image given
 4902: 	it's initial ht. and wid.  This allows sizing of
 4903: 	images that are generated on-the-fly (e.g. gnuplot)
 4904: 	as well as serving as a utility for image_size.
 4905:  
 4906: 	Parameter:
 4907:         height_param
 4908:         width_param    - Initial picture dimensions.
 4909:         scaling        - A scale factor.
 4910:         parstack,      - the current stack of tag attributes 
 4911:                          from the xml parser
 4912:         safeeval,      - pointer to the safespace
 4913:         depth,         - from what level in the stack to look for attributes
 4914:                          (assumes -1 if unspecified)
 4915:         cis            - look for attrubutes case insensitively
 4916:                          (assumes false)
 4917: 
 4918: 	Returns:
 4919: 		height, width   - new dimensions.
 4920: 
 4921: =item image_size()
 4922: 
 4923: =item image_width()
 4924: 
 4925: =item image_height()
 4926: 
 4927: =item get_eps_image()
 4928: 
 4929: =item eps_generation()
 4930: 
 4931: =item file_path()
 4932: 
 4933: =item recalc()
 4934: 
 4935: 	Converts a measurement in to mm from any of 
 4936: 	the other valid LaTeX units of measure.
 4937: 	If the units of measure are missing from the 
 4938: 	parameter, it is assumed to be in and returned
 4939: 	with mm units of measure
 4940: 
 4941: =item LATEX_length()
 4942: 
 4943: =item align_latex_image()
 4944: 
 4945:   	Wrap image 'stuff' inside of the LaTeX required to implement 
 4946:    	alignment:
 4947:      	align_tex_image(align, latex_rendering, image)
 4948:    	Where:
 4949:      	align   - The HTML alignment specification.
 4950:      	latex_rendering - rendering hint for latex.
 4951:      	image   - The LaTeX needed to insert the image itsef.
 4952:      	width,height - dimensions of the image.
 4953:  	Returns:
 4954:     	The 1/2 wrapped image and the stuff required to close the
 4955:     	wrappage.  This allows e.g. randomlabel to insert more stuff
 4956:     	into the closure.
 4957: 
 4958: 
 4959: =item is_inside_of($tagstack, $tag)
 4960:    	This sub returns true if the current state of Xml processing is inside of the tag.   
 4961: 	Parameters:
 4962:     	tagstack   - The tagstack from the parser.
 4963:     	tag        - The tag (without the <>'s.).
 4964: 	Sample usage:
 4965:     	if (is_inside_of($tagstack "table")) {
 4966:      	   I'm in a table....
 4967:      	}
 4968: 
 4969: 
 4970: 
 4971: =back
 4972: 
 4973: =cut
 4974: 
 4975: 
 4976: 1;
 4977: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>