Newlines should not be scaped when using setting usetex=True
The documentation mentions this behavior, but only as a passing note in the middle of the tutorial.
https://github.com/matplotlib/matplotlib/blob/f94fce68abe66f29e3045d0d4f20ccd0effa6767/galleries/users_explain/text/usetex.py#L87-L94
When passing slightly complex latex code to matplotlib, a user might be tempted to use multi-line strings, and the error that matplotlib produces is quite cryptic. For example,
ax.annotate(text=r"""\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}""", xy=(0.5,0.5), xycoords="figure fraction", xytext=(0,0))
produces
RuntimeError: latex was not able to process the following string: b'\\\\begin{tabular}{cc} '
I think it would be better to fix this behavior, but if not, it would be nice to give more visibility to it or include it in the troubleshooting at the end of the same page, at least.
I don't think we can really change this long-standing behavior due to backcompat concerns, but I agree that we should document this better (perhaps the error message "latex was not able..." can be simply supplemented with a comment regarding multiline inputs).
I guess we may be able to maintain the old behaviour but pass the entire string at once to latex by prepending \obeylines to the tex string, document that, and mention that newlines can be escaped by ending lines with a comment sign %.
Labeling as medium-hard because I think there's not too much design issues but fixing this still requires understanding both the tex (and text) rendering layer and fiddling with latex.
I tried to highlight source code in matplotlib. Pygments support LaTeX formatter, but almost all source codes contain new line, so I calcuated coordinate for each token then draw it.