# Markdown and Latex highlighting (font lock) in R Markdown file

I am using polymode to edit rmarkdown files. Exporting to e.g. pdf is super simple and works very well. R code chunks are also properly highlighted.

But:

I do quite often use latex code inside (r)markdown. Would it be possible to highlight the latex code as well?

I guessed I need something like latex as a second major mode or a markdown-latex-r-polymode but didn't get very far.

# Example

this is bold in markdown

\center{this centers text} in latex

The latex part should also be highlighted.

New contributor
ps_r is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• There is (setq markdown-enable-math t) for highlighting LaTeX equations in markdown code. But this will not highlight LaTeX command sequences like \center{...}. – Tobias Nov 8 at 14:12
• I don't think polymode does this. It assumes different languages will be set in different code blocks, or somehow delimited from each other. It doesn't do anything with code in one language inline with code in another. – Tyler Nov 8 at 14:36
• I had the same feeling about polymode reading the manual. I know of the setting highlighting equations but that, as you say, does not highlight anything else. – ps_r Nov 8 at 16:31
• Do you also use LaTeX environments like \begin{align*}...\end{align*}? Those are far more difficult to handle than other LaTeX command sequences. I think I have a solution for LaTeX command sequences but not for environments. – Tobias 2 days ago
• I usually only need sth like center for beamer presentations and there command sequence would be enough. What would be your solution? I also found a helpful comment suggesting to do this: (font-lock-add-keywords 'markdown-mode '(("\$$\\\\rightarrow\$$" . font-lock-keyword-face) Another way would be to use org-mode as there is an org-specific setting to highlight latex. – ps_r yesterday