You might find it more robust to parse the HTML there or at least make sure to grab only the LaTeX with a regular expression.