/**
 * Callback function for preg_replace_callback (LaTeX replacement).
 * Returns replacement image for LaTeX code.
 * @param $matches (string) array containing matches: $matches[0] is the complete match, $matches[1] the match for the first subpattern enclosed in '(...)' (the LaTeX code)
 * @return string replacement HTML code string to include the equivalent LaTeX image.
 */
function F_latex_callback($matches)
{
    require_once '../../shared/code/tce_latexrender.php';
    $latex = new LatexRender();
    $latex_code = unhtmlentities($matches[1]);
    // get image URL
    $imgurl = $latex->getFormulaURL($latex_code);
    if ($imgurl) {
        // alternative text to image
        $alt_latex = '[LaTeX]' . "\n" . htmlentities($latex_code, ENT_QUOTES);
        $alt_latex = str_replace("\r", '
', $alt_latex);
        $alt_latex = str_replace("\n", '
', $alt_latex);
        // XHTML code for image
        $newtext = '<img src="' . $imgurl . '" alt="' . $alt_latex . '" class="tcecode" />';
    } else {
        $newtext = '[LaTeX: ERROR ' . $latex->getErrorCode() . ']';
    }
    return $newtext;
}