微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

latex – 用于编译TeX源的Vim脚本,仅在没有错误的情况下启动PDF

我正在切换到使用Vim进行我的LaTeX编辑环境.我希望能够在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.任何有关如何检测编译是否成功的建议或见解将不胜感激!谢谢!

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

基本上,最好的解决方案似乎是使用Rubber,一个围绕LaTeX的包装器,通常“正常工作”,并提供非常干净的输出/错误.我在下面提出的解决方案优先使用Rubber,如果它在系统中找到并且在当前目录中找不到Makefile.如果找到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://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.

解决方法

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

在我的系统(Ubuntu)上,shellpipe = 2>& 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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐