0

Currently I have the line

:nnoremap <leader>c :w<CR>:!arara %<CR><CR>

in my $MYVIMRC. If I press <leader>c, it will save the current file and run the command arara on it. However I would like its behavior to be the following:

  1. Save the current file
  2. IF the first line has the form "%!TeX root = <something>"
  3. THEN run the command "arara <something>" instead
  4. ELSE run "arara <current file>"

For this it is required that vim reads the first line of the current file to process it in a command. I don't know how or if this is possible since I am rather new to vimscript.

The above behavior is rather standard in TeX editors and I would like to emulate it in vim.

In case it is important, I am using neovim rather than plain vim.

3
  • Did you check out following question on tex.stackexchange “arara: making a rule to compile the main file from a chapter file” ?
    – Hotschke
    Commented Oct 21, 2018 at 7:50
  • @Hotschke Yes, I had, but this is not exactly what I want. First of all, if I have a long compilation sequence (pdflatex-pdflatex-biber-pdflatex-...), I would have to copy and maintain it in each sub-file with the {files =[main.tex]} option. And then I would also like my solution to still work if I switch to other compilation programs like pure pdflatex instead of arara.
    – user17723
    Commented Oct 21, 2018 at 8:00
  • I see. Actually, I do not use arara but latexmk. IMHO this seems to be a design flaw of arara. Why did you choose arara over latexmk or rubber? And do you use vimtex?
    – Hotschke
    Commented Oct 21, 2018 at 8:06

1 Answer 1

0

Following command assumes that %!TeX root = <mainfile with path> is in the first line
(and it does not contain a second = in <mainfile with path>)

nnoremap <leader>c :w<CR>:!arara <C-r>=trim(split(getline(1), '=')[1])<CR><CR><CR>

Without extensive testing, following matches the first line against '\ctexroot' (\c means ignore case) and chooses accordingly the argument for arara:

function! Myarara()
    if match(getline(1), '\ctex root') >= 0
        echo system("arara ".trim(split(getline(1), '=')[1]))
    else
        echo system("arara ".@%)
    endif
endfunction
nnoremap <leader>c :w<CR>:call Myarara()<CR>

I personally would not be satisfied with this for following reasons:

  • no use of quickfix list and error parsing (if you are not interested in this, you could use something like fswatch or entr in the shell which means you are not blocked in vim, no need for a normal mode mapping, a simple :w is enough)
  • it blocks vim while running (compiling latex documents can take a while)

I would consider using one of vimtex, vim-dispatch, or asyncrun.vim.

2
  • Thank you very much. I will play around with this a little and maybe consider finally switching to some plugin.
    – user17723
    Commented Oct 21, 2018 at 9:09
  • I was suspecting that you are not in favour of plugins. I am not familar with the job api of neovim to improve the code so that you are not blocked when you compile your latex project. But it should be doable.
    – Hotschke
    Commented Oct 21, 2018 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.