仅在没有错误的情况下编译 TeX 源并启动 PDF 的 Vim 脚本

2024-02-25

我正在改用 Vim 作为我的 LaTeX 编辑环境。我希望能够从 Vim 中解析源文件,并在编译成功时启动外部查看。

我了解 Vim-Latex 套件,但是,如果可能的话,我宁愿避免使用它:它相当重量级,劫持了lot我的钥匙,并且用大量文件弄乱了我的 vim 运行时。

这是我现在所拥有的:

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 http://www.pps.jussieu.fr/~beffara/soft/rubber/,LaTeX 的包装器,通常“正常工作”,并提供非常干净的输出/错误。如果在系统上找到了 Rubber 并且在当前目录中找不到 Makefile,我下面介绍的解决方案会优先使用 Rubber。如果找到 Makefile,它将使用该文件。如果没有Makefile且未安装Rubber,则使用pdflatex。在所有情况下,如果源代码无法编译,(已过滤和解析的)错误将发送到 QuickFix 缓冲区,并自动打开 QuickFix 窗口。如果编译成功,则会写入一条短信,如果用户请求,则会打开 PDF 进行查看。

在我自己的安装中,我已经解除了(出色的)“SetLatexEfm()”函数来自Vim乳胶 http://vim-latex.sourceforge.net/解析和过滤 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 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}数组变量来获取正确的退出代码(借自这个答案 https://stackoverflow.com/questions/3077513/getting-the-linux-error-code-from-make-in-vim/3077763#3077763到另一个问题)。

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

否则,您可以尝试:

:set shellpipe=\>
:make %

这一套v:shell_error当它失败时,但我不确定这是否会扰乱“转到错误行号”功能(如果有)。

要查看变量设置的内容:

:set shellpipe?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅在没有错误的情况下编译 TeX 源并启动 PDF 的 Vim 脚本 的相关文章

  • 如何保存和编辑电缆打印的内容?

    这是后续如何以最小的格式将数据框导出到乳胶 https stackoverflow com questions 45929263 how to export a dataframe to latex with some minimal fo
  • 在插入模式下移至行首

    我知道我可以使用 Home in insert mode Esc i to exit insert mode and enter it again effectively going to the beginning of line But
  • LaTeX:从第二行缩进

    我想从第二行缩进 我想用 LaTeX 写这样的东西 Lorem ipsum dolor sit amet consectetur adipiscing elit Proin eu tempor velit Fusce accumsan ul
  • 在Vim中,如何删除单词的后缀?

    在vim中 在正常模式下 如果光标位于单词中 而不是最后一个字母 de从光标位置删除单词的后缀 如果光标位于最后一个字母上 x也这样做 同时de会跳到下一个单词的末尾 您将使用什么命令在这两种情况下都有效 无论最后一个字母与否 目的是将命令
  • 在 Vim 中搜索并替换为递增值

    假设我写了一个简单的 CSS 规则 如下所示 star 10 background url stars png no repeat 0 0 而我需要10个 所以我复制了9次 star 10 background url stars png
  • 在 R Markdown 文件中嵌入 pdf 并调整分页

    我即将完成博士学位 我需要在 R Markdown 文本中间的某个位置嵌入一些论文 pdf 格式 将 R Markdown 转换为 PDF 时 我希望将这些 PDF 论文嵌入到转换中 但是 我希望这些 PDF 论文也根据 Markdown
  • 在 Vim 中以反转模式突出显示匹配项

    如何在 Vim 中使用 match 命令突出显示与给定模式不匹配的文本 所以 我想要相反的 match myGroup foo 好吧 你可以这样做 match myBaseHighlight foo 2match myGroup where
  • VIM:在窗口顶部显示自定义参考栏

    我想设置一个 vim 环境来使用基本的 HTML 编辑 由其他人 为此 我想设置一个快速参考栏 显示在窗口顶部 例如
  • 如何去掉 LATEX 报告文档类中节编号中的零?

    所以我正在使用 Latex 编写报告 我使用的文档类是报告 documentclass a4paper 报告 但由于某种原因 节编号的编写方式是在其前面加上 0 例如 它看起来像 0 1 Introduction 0 2 Theory 0
  • Vim 关闭窗口而不关闭缓冲区

    如何在不删除缓冲区的情况下关闭窗口或取消分割 A window is a viewport on a buffer In vim to manage windows it is CTRL w the leading command that
  • 有没有办法让 LaTeX 将图形放置在同一页面中作为对该图形的引用?

    我正在使用 LaTeX 和图形环境 我非常熟悉该环境的参数 htbp 而且我通常也熟悉 LaTeX 默认情况下决定将每个图形放在哪里的策略 例如 将图形放置在页面的顶部或底部 我想知道是否有一个包 宏或一些我可以提供的命令 这样如果我有一个
  • Foldmethod=同时标记和语法?

    同一文件是否可以根据标记和语法使用折叠 Since foldmethod一次只能设置为一个 我认为您真正可以做到这一点的唯一方法是使用 set foldmethod expr并与foldexpr 即使如此 我也不确定它是否能够完全按照您的要
  • Pathogen 不加载插件

    病原体对我不起作用 我正在遵循 Adam Lowe 发布的提示here http www adamlowe me 2009 12 vim destroys all other rails editors html 还有更多 没有任何成功 我
  • 以 Vim 的 -o - 模式打开 Grep 输出中的文件

    如何将文件列表放入 Vim 的 o 模式 我有一个文件列表作为 Grep 的输出 我运行失败 1 grep il sid vim o 2 grep il sid xargs vim o 3 grep il sid xargs vim 4 v
  • 运行 tmux 时映射箭头键

    这些键映射在 tmux 中停止工作 在我的 vimrc 我有 nmap
  • vim 映射键不起作用

    我一直在尝试映射 ctrl 来在 vim 的插入模式下保存 它似乎永远不起作用 http vim wikia com wiki Map Ctrl S to save current or new files http vim wikia c
  • vim 退出时恢复 shell

    我刚刚在我的新计算机上安装了 Arch 但我不知道需要向 vimrc 添加什么命令 以便它在退出时恢复 shell 内容 在调用 vim 之前 也就是说 我希望我的 shell 看起来像这样 whoami root who root tty
  • Vim 函数插入带有传递参数的静态文本

    背景 I m interested in writing a function that assigned to keyboard shortcut s when invoked would 采取用户参数 计算值反映80 string le
  • 使用 vim pythoncomplete 的 Python 文档字符串不显示我自己的类函数的换行符

    我在尝试使用时得到了一些意想不到的结果Python 全方位补全 http www vim org scripts script php script id 1542在我自己的类函数上 函数的文档字符串未正确设置换行符格式 如下图所示 当我从
  • VIM 始终使用选项卡式页面

    我想要一个可以放入 vimrc 文件中的命令 该命令将使 vim 始终以选项卡式页面模式打开 而无需传递 p在命令行上 有这样的命令吗 如果没有 是否有更好的方法来做到这一点 目前 我正在使用 alias vi vim p 在我的 bash

随机推荐