2

Is there a way to use more than one syntax highlighting schemes in one file?

Specifically, I'm interested in having part of a LaTeX code highlighted according to my python syntax highlighting rules:

This is an example of embedding python inside \LaTeX.

\begin{pycode}
import random
n = random.randint(10)
\end{pycode}

Here's a random integer: $N=\pyc{print(n)}$

In the above example, the code will be generally highlighted according to the syntax file latex.vim, but inside the pycode environment and the pyc command, it will be highlightex according to python.vim.

  • 1
    This is possible, but requires some experience to implement. It should work in my plugin vimtex, which also provides a lot of other useful features for LaTeX editing. – Karl Yngve Lervåg Aug 7 at 16:34
0

My SyntaxRange plugin can highlight certain ranges of lines with a different, embedded syntax.

You can either manually define those sections interactively:

:1;/^\\begin{pycode}$/,/^\\end{pycode}$/SyntaxInclude python

Or automate this in ~/.vim/after/ftplugin/tex/python_include.vim:

call SyntaxRange#IncludeEx('start="^\\begin{pycode}$" end="^\\end{pycode}$"', 'python')
  • This is really cool! Many, many thanks! Two comments: 1. It seems like the correct call is :1;/^\\begin{pycode\}$/,/^\\end{pycode\}$/SyntaxInclude python (i.e. escaping the last }). 2. Right now it only applies to the first occurrence of \begin{pycode} ... \end{pycode}. Is there a way to apply it to all occurrences? – pelegs Nov 9 at 21:19
  • Escaping the } should not be necessary. Yes, the first command only processes a single range. To apply to multiple, use :global – Ingo Karkat Nov 9 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.