Improve LaTeX pattern matching for compatibility