【问题标题】:Vim script to compile TeX source and launch PDF only if no errors仅在没有错误时编译 TeX 源并启动 PDF 的 Vim 脚本
【发布时间】:2010-04-20 23:50:58
【问题描述】:

我在 LaTeX 编辑环境中改用 Vim。我希望能够从 Vim 中 tex 源文件,如果编译成功,则启动外部查看。

我知道 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没有设置,即使有编译错误。任何关于如何检测编译是否成功的建议或见解将不胜感激!谢谢!


在这里给出的答案之间,加上对其他方法的一些研究,我认为这已经得到了令人满意的解决。我在这里发布解决方案,以防其他人感兴趣。

基本上,最好的解决方案似乎是使用Rubber,这是一个围绕 LaTeX 的包装器,通常“正常工作”,并提供非常干净的输出/错误。我下面介绍的解决方案如果在系统上找到橡胶,并且在当前目录中没有找到 Makefile,则优先使用橡胶。如果找到了 Makefile,它会使用它。如果没有 Makefile 且未安装 Rubber,则使用 pdflatex。在所有情况下,如果源编译失败,(过滤和解析的)错误将被发送到 QuickFix 缓冲区,并且 QuickFix 窗口会自动打开。如果编译成功,则会写入一条短消息,如果用户请求,将打开 PDF 进行查看。

在我自己的安装中,我从Vim-Latex 中提升了 (excellent) "SetLatexEfm()" 函数来解析和过滤 tex 构建输出。但是,如果未找到此函数,则下面的函数默认设置一个错误消息格式,该格式工作得很好,以便在 QuickFix 窗口中识别和突出显示错误,尽管有很多杂物。

    function! BuildTex(view_results, ...)

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

        " save work
        silent write

        " From: http://stackoverflow.com/questions/2679475/vim-script-to-compile-tex-source-and-launch-pdf-only-if-no-errors
        " 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 获取。

【问题讨论】:

  • 这非常有用!

标签: latex pdflatex vim


【解决方案1】:

假设您进入 else-theres-no-makefile 部分,问题可能出在 shellpipe 变量上。

在我的系统 (Ubuntu) 上,shellpipe=2&gt;&amp;1| tee 和内置的 make 调用如果失败则不会设置 v:shell_error

| tee 的返回状态可能是 v:shell_error 设置的状态。

如果你的 shell 是 bash,你可以使用 ${PIPESTATUS} 数组变量来获取正确的退出代码(借用 this answer 到另一个问题)。

:set shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

否则你可以试试:

:set shellpipe=\>
:make %

这会在失败时设置v:shell_error,但我不确定这是否会干扰 go-to-error-line-number 功能(如果有的话)。

查看变量设置为:

:set shellpipe?

【讨论】:

  • Curt,谢谢。我会试一试。我可以看到这解决了错误,但正如你所说,它会弄乱 QuickFix 窗口列表。我很想将整个构建过程包装在一个 Python 脚本中,该脚本可以更智能地处理错误等。
【解决方案2】:

我知道这与 vim 无关,但我认为 latexmk 可以完成这项工作。

这是一个脚本(用 perl 编写),它编译乳胶文件并更新 pdf。最有用的未来是自动更新。保存文件后,“latexmk”编译它,如果您的 pdf 查看器支持它,则视图会更新。

latexmk -pdf -pvc

【讨论】:

  • 这看起来很有潜力。问题是,如果出现错误,脚本会停止并暂停输入——它需要 Ctrl-D 才能杀死它。它不允许在 QuickFix 窗口中进行错误跟踪。
  • 我看到另一个脚本会产生与 C 编译器相同的编译错误,所以也许我们可以运行它并在 QuickFix 窗口中捕获错误。它被称为Rubber
  • 谢谢。我实际上已经尝试过一段时间了。如果我记得,问题在于它也暂停并等待输入错误。我认为它可能有一个“忽略错误”标志,但这意味着,如果有旧的 pdf 可用,即使 TeX 源没有编译,它也会被打开。
  • 在你的 Latex 文档中添加一个 \nonstopmode 并且它不会暂停输入。如果你愿意,还有一个乳胶标志。 @Jeet
【解决方案3】:

如果latexmk 使用'-halt-on-error' 选项(或在不间断模式下)运行latex,编译将停止而不会暂停输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 2015-06-27
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2011-01-09
    • 2021-11-27
    相关资源
    最近更新 更多