0

I want to dynamically change the way latex-suite determines the MainFile. The main file is usually the latex header file which includes other tex files (like chapters and so on). Using the MainFile it is possible to hit compile on some chapter-file so that latex-suite automatically compiles the header file instead.

This should be possible with g:Tex_MainFileExpression: http://vim-latex.sourceforge.net/documentation/latex-suite/latex-master-file.html

However, the expression is not documented at all and even the example (which imo should reflect the default behavior) does not work.

let g:Tex_MainFileExpression = 'MainFile(modifier)'
function! MainFile(fmod)
    if glob('*.latexmain') != ''
        return fnamemodify(glob('*.latexmain'), a:fmod)
    else
        return ''
    endif
endif

Can somebody please shortly point out to me how this is supposed to be used? What return expression is expected? Why does the example not work?

Background: I have a latexmain file in the project root. I also have a figure subdirectory. For this subdirectory the root latex main should not be ignored, so that the current file itself is compiled.

Rodney
  • 205
  • 2
  • 7
  • Why don't you open an issue at the project? After all, you've encountered non-existing or unclear documentation. – Ingo Karkat Feb 17 '15 at 18:10
  • Honestly I thought my problem of understanding is just due to my complete lack of experience with both vim's scripting system and latex-suite configuration. A more experienced user might quickly figure out how to use it properly. – Rodney Feb 17 '15 at 18:39

1 Answers1

0

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