--- loncom/xml/lontex.pm	2006/04/20 02:06:35	1.11
+++ loncom/xml/lontex.pm	2025/02/20 03:46:11	1.16
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # TeX Content Handler
 #
-# $Id: lontex.pm,v 1.11 2006/04/20 02:06:35 albertel Exp $
+# $Id: lontex.pm,v 1.16 2025/02/20 03:46:11 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -46,12 +46,14 @@ use Apache::File;
 use Apache::lontexconvert;
 use Apache::Constants qw(:common);
 use Apache::lonnet;
+use Apache::loncommon;
+use Apache::lonmsg;
+use Apache::lonlocal;
 use tth;
 
 # ================================================================ Main Handler
 
 sub footer {
-    my ($errorstring) = @_;
     my $xmlstring='';
     if ($env{'request.state'} eq 'construct') {
 	$xmlstring.='<address>'.
@@ -84,8 +86,18 @@ sub handler {
 # ------------------------------------------------------------------- Read file
 
   {
-    my $fh=Apache::File->new($r->filename);
-    @texcontents=<$fh>;
+      if (-e $r->filename) {
+          my $fh=Apache::File->new($r->filename);
+          @texcontents=<$fh>;
+          close($fh);
+      } else {
+            my $filename=(split('/',$r->filename))[-1];
+            my $error = &mt('Unable to find [_1]',
+                            '<span class="LC_filename">'.$filename.'</span>');
+            &Apache::loncommon::simple_error_page($r,'Not available',
+                                                  $error,{'no_auto_mt_msg' => 1});
+            return OK;
+      }
   }
 
   $texstring=join("\n",@texcontents);
@@ -96,8 +108,6 @@ sub handler {
 					  ['texengine','inhibitmenu']);
 
 
-  &Apache::lontexconvert::init_tth();
-
   $r->print(&Apache::loncommon::start_page(undef,undef,
 					   {'bgcolor'        => '#FFFFFF',
 					    'force_register' => 1,
@@ -105,8 +115,15 @@ sub handler {
 						($env{'form.inhibitmenu'} 
 						 eq 'yes'), }));
 
+  my $displaymode;
+  if (&Apache::lonnet::EXT('resource.0.texdisplay') =~ /^(tth|mathjax)$/i) {
+      $displaymode = $1;
+  }
+  if ($env{'form.texengine'} =~ /^(tth|mathjax)$/i) {
+      $displaymode = $1;
+  }
   $r->print(&Apache::lontexconvert::converted(\$texstring,
-					      $env{'form.texengine'}));
+					      $displaymode));
   $r->print(&footer());
 
   return OK;
@@ -115,6 +132,32 @@ sub handler {
 1;
 __END__
 
+=pod
+
+=head1 NAME
+
+Apache::lontex.pm
+
+=head1 SYNOPSIS
+
+Handler for tex files (somewhere in modules)
+
+This is part of the LearningOnline Network with CAPA project
+described at http://www.lon-capa.org.
+
+
+=head1 SUBROUTINES
+
+=over
+
+=item footer()
+
+Main Handler
+
+=back
+
+=cut
+