# Which is the best editor for LuaTeX?

Many (La)TeX editors support the LuaTeX engine. However, which one offers the best support for writing LuaTeX? Which editor or plugin supports features such as syntax highlighting and auto-completion for both Lua and (La)TeX?

Common useful features of LaTeX focused IDEs such as integrated viewer, forward and inverse search, structure browser and spell checking are still important, though supplemented by some Lua syntax support.

-
There's a huge list of editors with Lua support here: Lua Editor Support. Crossing this with your favourite LaTeX editor shouldn't be too hard. Perhaps the question is do any of the editors support two language syntaxes at once? –  Alan Munn May 23 '11 at 20:47
@Alan: thanks for the link! A capable editor supporting two languages at the same time may do, though I'm mainly focused on LaTeX and I like forward and inverse search, integrated viewers and further features of classical LaTeX IDEs - combined with integrated Lua syntax support - I'll edit the question. –  Stefan Kottwitz May 23 '11 at 21:12
Not having an IDE and not being free: I really like textmate (macromates.com) because you can (like vim - see Aditya's answer) define regions where the editor switches from LaTeX to Lua syntax. –  topskip May 24 '11 at 5:09

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. 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



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.

-
This 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 but putting the following in the 'tex.vim' ft plugin file. ... it i too long in a comment, I put it in an answer –  Yan Zhou Aug 22 '11 at 11:03
@Yan Zhou: I include them in the syntax/context.vim rather than the ft plugin. For LaTeX, you can consider adapting the default syntax highlighting plugin. –  Aditya Aug 22 '11 at 16:45
I did try to include your code (modified for latex) in /after/syntax/tex.vim and that is where the problem happens. My method does not actually require they to be included in specific file, fttype file or even .vimrc will do. –  Yan Zhou Aug 22 '11 at 17:05

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

-
I wonder if this will be addressed in future versions of AUCTeX? –  BeSlayed May 29 '11 at 1:36
Good question. I opened a thread on the auctex-mailinglist, see here: mail-archive.com/auctex@gnu.org , title: »Lua commands in TeX-buffers: highlighting by using indirect buffers?« We will see if more people are interested. A. –  Keks Dose May 29 '11 at 19:29
I don't know the status of the mailing lists, but I've actually been able to solve this one: tex.stackexchange.com/a/128724/17423 –  Sean Allred Aug 18 '13 at 2:01

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.

-