Escape special characters in to_latex() output

Updated commit to properly replace backslash, tilde, and caret with latex commands.