Which is the best editor for LuaTeX?

Vim supports embedding syntax highlighting of one language in another. I use that to support both MP and Lua syntax highlighting in ConTeXt regions. The basic trick is do define a syntax region as follows:

unlet b:current_syntax
syn include @LUA syntax/lua.vim

syn region luatex matchgroup=contextIdentifier
      \ start='\\startluacode'
      \ end='\\stopluacode'
      \ contains=@LUA

Then everything inside \startluacode ... \stopluacode will have Lua syntax highlighting. Here is a screenshot showing code from one of my modules (not that the lua comments and the function os.remove inside the \startluacode environment are syntax highlighted)

enter image description here

For completeness, here is a screenshot showing highlighted Metapost code inside a tex file

enter image description here

The same idea will also work in LaTeX, as follows:

unlet b:current_syntax
syn include @LUA syntax/lua.vim

syn region luatexSnip matchgroup=Snip
    \ start='\\begin{\z(luacode\|luacode*\)}'
    \ end='\\end{\z1}'
    \ contains=@LUA

syn region luatexSnip matchgroup=Snip
    \ start='\\\(directlua\|luadirect\){'
    \ end='}'
    \ contains=@LUA


highlight link Snip SpecialComment

Vim also supports auto-completion, but I don't use that feature so I don't know if language dependent auto-completion is possible or not.

Other requirements, like forward/backward search, spell checking, structure browser, are straight forward, but even then, vim is not what you will call an IDE.


Emacs as well offers syntax highlighting for Lua and LaTeX. I do not know whether there is something like "embedded syntax highlighting" for lua-chunks inside a *.tex-file. Usually you do that by opening the file into an indirect buffer: in the base buffer you get LaTeX-highlighting, in the indirect buffer you change the major mode to lua-mode.

But: this doesn't work with lua-mode yet, at least for me. Independently from changing the mode to lua-mode, Emacs keeps highlighting LaTeX-keywords in the indirect buffer.

Emacs with AucTEX is great for LaTeX, the lua-mode is by far less convenient.

Alexander


Aditya's answer does not work for me anyway. I use LaTeX and it may be the reason. Anyway, inspired by your answer I got it works for LuaLaTeX by putting the following in the 'tex.vim' ft plugin file.

autocmd BufWinEnter * if exists("b:current_syntax") && b:current_syntax == "tex"
autocmd BufWinEnter * unlet b:current_syntax
autocmd BufWinEnter * syntax include @TEX syntax/tex.vim
autocmd BufWinEnter * unlet b:current_syntax
autocmd BufWinEnter * syntax include @LUA syntax/lua.vim
autocmd BufWinEnter * syntax region luatex matchgroup=Snip start="%--beginlua--" end="%--endlua--" containedin=@TEX contains=@LUA
autocmd BufWinEnter * syntax region luatex matchgroup=Snip start="--beginlua--" end="--endlua--" containedin=@TEX contains=@LUA
autocmd BufWinEnter * highlight link Snip SpecialComment
autocmd BufWinEnter * let b:current_syntax="luatex"
autocmd BufWinEnter * endif

It should worked by simply put these codes without autocmd BufWinEnter in to the after/syntax/tex.vim file. But in my case that causes some problems and got vim halted. So I use the au to get them executed at the very end of file loading process.

After doing these, everything marked by --beginlua-- and --endlua-- are highlighted as lua code. One can use the version with % in \directlua or luaexec where TeX line comment is allowed but not lua line comment. And the version without % in the \begin{luacode}, etc.

Tags:

Editors

Luatex