Improve LaTeX conversion
authorJoey Schulze <joey@infodrom.org>
Thu, 4 Oct 2012 20:39:47 +0000 (22:39 +0200)
committerJoey Schulze <joey@infodrom.org>
Thu, 4 Oct 2012 20:39:47 +0000 (22:39 +0200)
utils.php

index 694c1ee..f998ced 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -22,11 +22,26 @@ function rm_tempdir($dir)
   system(sprintf("rm -rf %s", $dir));
 }
 
+function latexspecialchars($string)
+{
+  $map = array(
+              "#"=>"\\#",
+              "$"=>"\\$",
+              "%"=>"\\%",
+              "&"=>"\\&",
+              "~"=>"\\~{}",
+              "_"=>"\\_",
+              "^"=>"\\^{}",
+              "\\"=>"\\textbackslash",
+              "{"=>"\\{",
+              "}"=>"\\}",
+              );
+  return preg_replace("/([\^\%~\\\\#\$%&_\{\}])/e", "\$map['$1']", $string);
+}
+
 function latex_encode($text)
 {
-  return str_replace(array("\\",'%','&'),
-                    array('\\','\%','\&'),
-                    utf8_decode($text));
+  return latexspecialchars(utf8_decode($text));
 }
 
 function ordersatz_archive($id, $title, $filename)