File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.399: download - view: text, annotated - select for diffs
Tue Mar 31 10:12:45 2009 UTC (15 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
Factor out the creation of the LaTeX header code .. hope is to centralize this
across LonCAPA.

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

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