Vim脚本编译TeX源文件并在无错误时启动PDF

6
我正在将LaTeX编辑环境切换到Vim上。希望能够在Vim内部对源文件进行编译,并在成功编译后启动外部查看器。
我知道有Vim-Latex套件,但如果可能的话,我宁愿避免使用它:它非常复杂,会占用很多我的按键,并且会让vimruntime中出现大量文件。
以下是我目前的情况:
if exists('b:tex_build_mapped')
    finish
endif
" use maparg or mapcheck to see if key is free
command! -buffer -nargs=* BuildTex call BuildTex(0, <f-args>)
command! -buffer -nargs=* BuildAndViewTex call BuildTex(1, <f-args>)
noremap <buffer> <silent> <F9> <Esc>:call BuildTex(0)<CR>
noremap <buffer> <silent> <S-F9> <Esc>:call BuildTex(1)<CR>
let b:tex_build_mapped = 1

if exists('g:tex_build_loaded')
    finish
endif
let g:tex_build_loaded = 1

function! BuildTex(view_results, ...)
    write
    if filereadable("Makefile")
        " If Makefile is available in current working directory, run 'make' with arguments
        echo "(using Makefile)"
        let l:cmd = "!make ".join(a:000, ' ')
        echo l:cmd
        execute l:cmd
        if a:view_results && v:shell_error == 0
            call ViewTexResults()
        endif
    else
        let b:tex_flavor = 'pdflatex'
        compiler tex
        make %
        if a:view_results && v:shell_error == 0
            call ViewTexResults()
        endif
    endif
endfunction

function! ViewTexResults(...)
    if a:0 == 0
        let l:target = expand("%:p:r") . ".pdf"
    else
        let l:target = a:1
    endif
    if has('mac')
        execute "! open -a Preview ".l:target
    endif
endfunction

问题在于,即使有编译错误,也没有设置v:shell_error。如果您有任何建议或见解,如何检测编译是否成功将不胜感激!谢谢!
在这里给出的答案以及其他方法的研究中,我认为已经得到了令人满意的解决。我在此发布解决方案,以防其他人有兴趣。
基本上,最好的解决方案似乎是使用LaTeX的包装器Rubber,它通常“只需工作”,并提供非常干净的输出/错误。如果在系统上找到Rubber并且当前目录中没有Makefile,则首选下面介绍的解决方案使用Rubber。如果找到Makefile,则使用该文件。如果没有Makefile且未安装Rubber,则使用pdflatex。在所有情况下,如果源代码无法编译,则将(过滤和解析后的)错误发送到QuickFix缓冲区,并自动打开QuickFix窗口。如果成功编译,将写入一条简短的消息,并且如果用户请求,则会打开PDF进行查看。
在我的自己的安装中,我从Vim-Latex中提取了(优秀的)“SetLatexEfm()”函数来解析和过滤tex构建输出。但是,如果找不到此函数,则以下函数默认设置错误消息格式,该格式足以识别并在QuickFix窗口中突出显示错误,尽管有很多垃圾。
    function! BuildTex(view_results, ...)

        " record position
        let save_cursor = getpos(".")

        " save work
        silent write

        " From: https://dev59.com/y03Sa4cB1Zd3GeqPuE45
        " If your shell is bash, you can use the ${PIPESTATUS} array variable to get
        " the correct exit code (borrowed from this answer to another question).
        silent setlocal shell=bash
        silent setlocal shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

        let success = 1
        if filereadable("Makefile")
            " If Makefile is available in current working directory, run 'make' with arguments
            echon "compiling using Makefile ..."
            let l:makecmd = "make\\ ".join(a:000, '\\ ')
            silent execute "setlocal makeprg=" . l:makecmd
            try
                " This function is defined in the Vim-Latex package, 
                " and provides excellent parsing and filtering of the error messages
                " when running latex outside of the Rubber wrapper.
                call s:SetLatexEfm()
            catch /E117/
                set errorformat=%E!\ LaTeX\ %trror:\ %m,
                    \%E!\ %m,
                    \%+WLaTeX\ %.%#Warning:\ %.%#line\ %l%.%#,
                    \%+W%.%#\ at\ lines\ %l--%*\\d,
                    \%WLaTeX\ %.%#Warning:\ %m,
                    \%Cl.%l\ %m,
                    \%+C\ \ %m.,
                    \%+C%.%#-%.%#,
                    \%+C%.%#[]%.%#,
                    \%+C[]%.%#,
                    \%+C%.%#%[{}\\]%.%#,
                    \%+C<%.%#>%.%#,
                    \%C\ \ %m,
                    \%-GSee\ the\ LaTeX%m,
                    \%-GType\ \ H\ <return>%m,
                    \%-G\ ...%.%#,
                    \%-G%.%#\ (C)\ %.%#,
                    \%-G(see\ the\ transcript%.%#),
                    \%-G\\s%#,
                    \%+O(%f)%r,
                    \%+P(%f%r,
                    \%+P\ %\\=(%f%r,
                    \%+P%*[^()](%f%r,
                    \%+P[%\\d%[^()]%#(%f%r,
                    \%+Q)%r,
                    \%+Q%*[^()])%r,
                    \%+Q[%\\d%*[^()])%r
            endtry
            silent make
        else
            let l:special_tex_compiler = "rubber"
            if executable(l:special_tex_compiler)
                echon "compiling with Rubber ..."
                silent execute "setlocal makeprg=" . l:special_tex_compiler . "\\ -dfs\\ %"
                setlocal errorformat=%f:%l:\ %m
                silent make %
            else
                echon "compiling ..."
                let b:tex_flavor = 'pdflatex'
                compiler tex
                silent make %
            endif
        endif

        " set/report compile status
        if v:shell_error
            let l:success = 0
            " let l:wheight = winheight(bufnr("%")) / 2
            " execute "copen ".l:wheight
            copen
        else
            let l:success = 1
            cclose
            redraw
            echon "successfully compiled"
        endif

        " view results if successful compile
        if l:success && a:view_results
            call ViewTexResults()
        endif

        " restore position
        call setpos('.', save_cursor)

    endfunction

    function! ViewTexResults(...)
        if a:0 == 0
            let l:target = expand("%:p:r") . ".pdf"
        else
            let l:target = a:1
        endif
        if has('mac')
            silent execute "! open -a Preview ".l:target
            " obviously, you will need to write specific commands for other systems
            " left as an exercise for the reader ...
        endif
    endfunction

    command! -buffer -nargs=* BuildTex call BuildTex(0, <f-args>)
    command! -buffer -nargs=* BuildAndViewTex call BuildTex(1, <f-args>)
    noremap <buffer> <silent> <F9> <Esc>:call BuildTex(0)<CR>
    noremap <buffer> <silent> <S-F9> <Esc>:call BuildTex(1)<CR>

更新:我已经将这个脚本打包发布为 Vim 文件类型插件,可以在http://www.vim.org/scripts/script.php?script_id=3230下载。

这非常有帮助! - James Mitchell
3个回答

3
假设您正在遇到“否则就没有makefile”部分的问题,问题可能出在shellpipe变量上。
在我的系统(Ubuntu)上,shellpipe=2>&1| tee,内置的make调用如果失败,则不会设置v:shell_error| tee命令的返回状态可能是导致v:shell_error被设置的原因。
如果您的shell是bash,您可以使用${PIPESTATUS}数组变量来获取正确的退出代码(摘自另一个问题的这个答案)。
:set shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

否则,您可以尝试以下方法:
:set shellpipe=\>
:make %

这会在失败时设置v:shell_error,但我不确定是否会影响到跳转到错误行号的功能(如果有的话)。
要查看变量设置为什么:
:set shellpipe?

Curt,谢谢你。我会试一下的。我可以看到这将解决拾取错误的问题,但是,正如你所说,它会破坏QuickFix窗口列表。我很想用Python脚本来包装整个构建过程,以便更加智能地处理错误等。 - Jeet

2

我知道这与vim无关,但我认为latexmk可以胜任此工作。

它是一个Perl脚本,用于编译latex文件并更新pdf。最有用的功能是自动更新功能。只要您保存文件,“latexmk”就会编译它,如果您的pdf查看器支持它,则会更新视图。

latexmk -pdf -pvc


这看起来很有潜力。问题在于,如果出现错误,脚本会停止并暂停等待输入 --- 需要按Ctrl-D来终止它。它不允许在QuickFix窗口中进行错误跟踪。 - Jeet
我看到另一个脚本生成了与C编译器相同的编译错误,所以也许我们可以运行它并在QuickFix窗口中捕获错误。它被称为Rubber - hleb
谢谢。我之前尝试过这个方法。如果我没记错的话,问题在于它也会在错误时暂停并等待输入。我想它可能有一个“忽略错误”的标志,但这意味着即使TeX源代码未编译,如果有旧的PDF可用,它也会被打开。 - Jeet
将\nonstopmode添加到您的latex文档中,它将不会暂停输入。如果需要,latex还有一个标志。@Jeet - Theo Belaire

0
如果latexmk使用“-halt-on-error”选项(或在非停止模式下)运行latex,则编译将在没有暂停输入的情况下停止。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接