Fix output of external functions (and their derivatives) in LaTeX.

(manually cherry picked from commit 1faae7bf4ceef3c2aa4ea25fc8eeb133fd25ea32)