I just ran into the problem of not knowing how to set g:Tex_MainFileExpression
as well. The documentation and the example were not clear to me either. Turns out, the source code defines a function Tex_GetMainFileName
, which sets the variable modifier
from its arguments before executing g:Tex_MainFileExpression
(see source code here). Therefore, g:Tex_MainFileExpression
needs to be a function that has the argument modifier
(not called differently!). The vim-latex documentation says, that modifier is a filetype-modifier, therefore your function needs to return fnamemodify(filename, modifier)
. So it has to look like this:
let g:Tex_MainFileExpression = 'MainFile(modifier)'
function! MainFile(fmod)
" Determine the full path to your main latex file that you want to compile.
" Store it e.g. in the variable `path`:
" let path = some/path/to/main.tex
" Apply `modifier` to your `path` variable
return fnamemodify(path, a:fmod)
endif
Example
I used this in a project where I have a two main latex files, one for the main document and one for supplementary material. The project structure looks like this:
project/
main.tex
sup.tex
.local-vimrc
main-source/
input1.tex
input2.tex
sup-source/
input1.tex
input2.tex
I load a .local-vimrc
file (using the plugin MarcWeber/vim-addon-local-vimrc), where I set g:Tex_MainFileExpression
such that <leader>ll
compiles main.tex
if the file in the current buffer is located in the folder main-source
and compiles sup.tex
if it is in the folder sup-source
. Below is my .local-vimrc
file. I have very little experience with vimscript, so this is probably a littly hacky, but it might help to get an idea on how to use g:Tex_MainFileExpression
. Also, I have modified it to be less messy and not tested the following code explicitly.
let g:Tex_MainFileExpression = 'g:My_MainTexFile(modifier)'
function! g:My_MainTexFile(fmod)
" Get absolute (link resolved) paths to this script and the open buffer
let l:path_to_script = fnamemodify(resolve(expand('<sfile>:p')), ':h')
let l:path_to_buffer = fnamemodify(resolve(expand('%:p')), ':h')
" Check if the buffer file is a subdirectory of `main-source` or `sup-source`
" stridx(a, b) returns -1 only if b is not substring of a
if stridx(l:path_to_buffer, 'main-source') != -1
let l:name = 'main.tex'
elseif stridx(l:path_to_buffer, 'sup-source') != -1
let l:name = 'sup.tex'
else
echom "Don't know what's the root tex file. '".@%."' is not in 'main-source/' or 'sup-source/' directory."
return ''
endif
" Concatenate this script path with main latex file name
" NOTE: this assumes that this script is located in the same folder as the
" main latex files 'main.tex' and 'sup.tex'
let l:path = l:path_to_script.'/'.l:name
return fnamemodify(l:abs_path_main, a:fmod)
endfunction