Example #1
0
/** converts latex chars to HTML entities
 * it uses a naive algortihm
 * I still look for a comprehensive translation table from late chars to html
 * just have this http://isdc.unige.ch/Newsletter/help.html
 */
function latex2html($line)
{
    $line = preg_replace('/([^\\\\])~/', '\\1 ', $line);
    // performance increases with this test
    // bug found by Serge Barral: what happens if we have curly braces only (typically to ensure case in Latex)
    // added && strpos($line,'{')===false
    if (strpos($line, '\\') === false && strpos($line, '{') === false) {
        return $line;
    }
    // handling \url{....}
    // often used in howpublished for @misc
    $line = preg_replace('/\\\\url\\{(.*)\\}/U', '<a href="\\1">\\1</a>', $line);
    $line = char2html($line, "'", 'a', "acute");
    $line = char2html($line, "'", 'e', "acute");
    $line = char2html($line, "'", 'i', "acute");
    $line = char2html($line, "'", 'o', "acute");
    $line = char2html($line, "'", 'u', "acute");
    $line = char2html($line, "'", 'y', "acute");
    $line = char2html($line, "'", 'n', "acute");
    $line = char2html($line, '`', 'a', "grave");
    $line = char2html($line, '`', 'e', "grave");
    $line = char2html($line, '`', 'i', "grave");
    $line = char2html($line, '`', 'o', "grave");
    $line = char2html($line, '`', 'u', "grave");
    $line = char2html($line, '~', 'a', "tilde");
    $line = char2html($line, '~', 'n', "tilde");
    $line = char2html($line, '~', 'o', "tilde");
    $line = char2html($line, '"', 'a', "uml");
    $line = char2html($line, '"', 'e', "uml");
    $line = char2html($line, '"', 'i', "uml");
    $line = char2html($line, '"', 'o', "uml");
    $line = char2html($line, '"', 'u', "uml");
    $line = char2html($line, '"', 'y', "uml");
    $line = char2html($line, '^', 'a', "circ");
    $line = char2html($line, '^', 'e', "circ");
    $line = char2html($line, '^', 'i', "circ");
    $line = char2html($line, '^', 'o', "circ");
    $line = char2html($line, '^', 'u', "circ");
    $line = char2html($line, '.', 'a', "ring");
    $line = char2html($line, 'c', 'c', "cedil");
    $line = str_replace('\\ae', '&aelig;', $line);
    $line = str_replace('\\ss', '&szlig;', $line);
    $line = str_replace('\\o', '&oslash;', $line);
    $line = str_replace('\\O', '&Oslash;', $line);
    $line = str_replace('\\&', '&amp;', $line);
    // clean out extra tex curly brackets, usually used for preserving capitals
    $line = str_replace('}', '', $line);
    $line = str_replace('{', '', $line);
    return $line;
}
 /** converts latex chars to HTML entities.
 (I still look for a comprehensive translation table from late chars to html, better than [[http://isdc.unige.ch/Newsletter/help.html]])
  */
 function latex2html($line)
 {
     $line = preg_replace('/([^\\\\])~/', '\\1&nbsp;', $line);
     $line = str_replace('---', '&mdash;', $line);
     $line = str_replace('--', '&ndash;', $line);
     // performance increases with this test
     // bug found by Serge Barral: what happens if we have curly braces only (typically to ensure case in Latex)
     // added && strpos($line,'{')===false
     if (strpos($line, '\\') === false && strpos($line, '{') === false) {
         return $line;
     }
     $maths = array();
     $index = 0;
     // first we escape the math env
     preg_match_all('/\\$.*?\\$/', $line, $matches);
     foreach ($matches[0] as $k) {
         $maths[] = $k;
         $line = str_replace($k, '__MATH' . $index . '__', $line);
         $index++;
     }
     // we should better replace this before the others
     // in order not to mix with the HTML entities coming after (just in case)
     $line = str_replace('\\&', '&amp;', $line);
     $line = str_replace('\\_', '_', $line);
     $line = str_replace('\\%', '%', $line);
     // handling \url{....}
     // often used in howpublished for @misc
     $line = preg_replace('/\\\\url\\{(.*)\\}/U', '<a href="\\1">\\1</a>', $line);
     // Friday, April 01 2011
     // added support for accented i
     // for instance \`\i
     // see http://en.wikibooks.org/wiki/LaTeX/Accents
     // " the letters "i" and "j" require special treatment when they are given accents because it is often desirable to replace the dot with the accent. For this purpose, the commands \i and \j can be used to produce dotless letters."
     $line = preg_replace('/\\\\([ij])/i', '\\1', $line);
     $line = char2html($line, "'", 'a', "acute");
     $line = char2html($line, "'", 'e', "acute");
     $line = char2html($line, "'", 'i', "acute");
     $line = char2html($line, "'", 'o', "acute");
     $line = char2html($line, "'", 'u', "acute");
     $line = char2html($line, "'", 'y', "acute");
     $line = char2html($line, "'", 'n', "acute");
     $line = char2html($line, '`', 'a', "grave");
     $line = char2html($line, '`', 'e', "grave");
     $line = char2html($line, '`', 'i', "grave");
     $line = char2html($line, '`', 'o', "grave");
     $line = char2html($line, '`', 'u', "grave");
     $line = char2html($line, '~', 'a', "tilde");
     $line = char2html($line, '~', 'n', "tilde");
     $line = char2html($line, '~', 'o', "tilde");
     $line = char2html($line, '"', 'a', "uml");
     $line = char2html($line, '"', 'e', "uml");
     $line = char2html($line, '"', 'i', "uml");
     $line = char2html($line, '"', 'o', "uml");
     $line = char2html($line, '"', 'u', "uml");
     $line = char2html($line, '"', 'y', "uml");
     $line = char2html($line, '"', 's', "zlig");
     $line = char2html($line, '^', 'a', "circ");
     $line = char2html($line, '^', 'e', "circ");
     $line = char2html($line, '^', 'i', "circ");
     $line = char2html($line, '^', 'o', "circ");
     $line = char2html($line, '^', 'u', "circ");
     $line = char2html($line, 'r', 'a', "ring");
     $line = char2html($line, 'c', 'c', "cedil");
     $line = char2html($line, 'v', 's', "caron");
     $line = str_replace('\\ae', '&aelig;', $line);
     $line = str_replace('\\ss', '&szlig;', $line);
     $line = str_replace('\\o', '&oslash;', $line);
     $line = str_replace('\\O', '&Oslash;', $line);
     $line = str_replace('\\aa', '&aring;', $line);
     $line = str_replace('\\AA', '&Aring;', $line);
     $line = str_replace('\\l', '&#322', $line);
     $line = str_replace('\\L', '&#321', $line);
     $line = str_replace('\\k{a}', '&#261', $line);
     $line = str_replace('\\\'{c}', '&#263', $line);
     // clean out extra tex curly brackets, usually used for preserving capitals
     $line = str_replace('}', '', $line);
     $line = str_replace('{', '', $line);
     // we restore the math env
     for ($i = 0; $i < count($maths); $i++) {
         $line = str_replace('__MATH' . $i . '__', $maths[$i], $line);
     }
     return $line;
 }
Example #3
0
 /** converts latex chars to HTML entities.
 (I still look for a comprehensive translation table from late chars to html, better than [[http://isdc.unige.ch/Newsletter/help.html]])
  */
 function latex2html($line)
 {
     $line = preg_replace('/([^\\\\])~/', '\\1&nbsp;', $line);
     // performance increases with this test
     // bug found by Serge Barral: what happens if we have curly braces only (typically to ensure case in Latex)
     // added && strpos($line,'{')===false
     if (strpos($line, '\\') === false && strpos($line, '{') === false) {
         return $line;
     }
     // we should better replace this before the others
     // in order not to mix with the HTML entities coming after (just in case)
     $line = str_replace('\\&', '&amp;', $line);
     // handling \url{....}
     // often used in howpublished for @misc
     $line = preg_replace('/\\\\url\\{(.*)\\}/U', '<a href="\\1">\\1</a>', $line);
     // Friday, April 01 2011
     // added support for accented i
     // for instance \`\i
     // see http://en.wikibooks.org/wiki/LaTeX/Accents
     // " the letters "i" and "j" require special treatment when they are given accents because it is often desirable to replace the dot with the accent. For this purpose, the commands \i and \j can be used to produce dotless letters."
     $line = preg_replace('/\\\\([ij])/i', '\\1', $line);
     $line = char2html($line, "'", 'a', "acute");
     $line = char2html($line, "'", 'e', "acute");
     $line = char2html($line, "'", 'i', "acute");
     $line = char2html($line, "'", 'o', "acute");
     $line = char2html($line, "'", 'u', "acute");
     $line = char2html($line, "'", 'y', "acute");
     $line = char2html($line, "'", 'n', "acute");
     $line = char2html($line, '`', 'a', "grave");
     $line = char2html($line, '`', 'e', "grave");
     $line = char2html($line, '`', 'i', "grave");
     $line = char2html($line, '`', 'o', "grave");
     $line = char2html($line, '`', 'u', "grave");
     $line = char2html($line, '~', 'a', "tilde");
     $line = char2html($line, '~', 'n', "tilde");
     $line = char2html($line, '~', 'o', "tilde");
     $line = char2html($line, '"', 'a', "uml");
     $line = char2html($line, '"', 'e', "uml");
     $line = char2html($line, '"', 'i', "uml");
     $line = char2html($line, '"', 'o', "uml");
     $line = char2html($line, '"', 'u', "uml");
     $line = char2html($line, '"', 'y', "uml");
     $line = char2html($line, '^', 'a', "circ");
     $line = char2html($line, '^', 'e', "circ");
     $line = char2html($line, '^', 'i', "circ");
     $line = char2html($line, '^', 'o', "circ");
     $line = char2html($line, '^', 'u', "circ");
     $line = char2html($line, '.', 'a', "ring");
     $line = char2html($line, 'c', 'c', "cedil");
     $line = str_replace('\\ae', '&aelig;', $line);
     $line = str_replace('\\ss', '&szlig;', $line);
     $line = str_replace('\\o', '&oslash;', $line);
     $line = str_replace('\\O', '&Oslash;', $line);
     $line = str_replace('\\aa', '&aring;', $line);
     $line = str_replace('\\AA', '&Aring;', $line);
     // clean out extra tex curly brackets, usually used for preserving capitals
     $line = str_replace('}', '', $line);
     $line = str_replace('{', '', $line);
     return $line;
 }