首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vim latex-suite了解MainFileExpression

vim latex-suite了解MainFileExpression
EN

Stack Overflow用户
提问于 2015-02-18 01:15:01
回答 1查看 145关注 0票数 0

我想动态改变latex-suite确定MainFile的方式。主文件通常是latex头文件,其中包括其他tex文件(如章节等)。使用MainFile,可以在某些章节文件上点击编译,这样latex-suite就会自动编译头文件。

使用g:Tex_MainFileExpression:http://vim-latex.sourceforge.net/documentation/latex-suite/latex-master-file.html应该可以做到这一点

但是,表达式根本没有文档记录,甚至示例( imo应该反映默认行为)也不起作用。

代码语言:javascript
复制
let g:Tex_MainFileExpression = 'MainFile(modifier)'
function! MainFile(fmod)
    if glob('*.latexmain') != ''
        return fnamemodify(glob('*.latexmain'), a:fmod)
    else
        return ''
    endif
endif

有人能简短地告诉我这应该是如何使用的吗?预期的返回表达式是什么?为什么这个例子不起作用?

背景:我在项目根目录中有一个latexmain文件。我还有一个figure子目录。对于该子目录,不应忽略根latex main,以便编译当前文件本身。

EN

回答 1

Stack Overflow用户

发布于 2020-03-26 02:06:44

原来,源代码定义了一个函数Tex_GetMainFileName,该函数在执行g:Tex_MainFileExpression之前通过其参数设置变量modifier (请参阅source code here)。因此,g:Tex_MainFileExpression需要是一个带有参数modifier的函数(没有不同的调用方式!)。vim-latex文档说,该修饰符是一个filetype-modifier,因此您的函数需要返回fnamemodify(filename, modifier)。所以它必须看起来像这样:

代码语言:javascript
复制
let g:Tex_MainFileExpression = 'MainFile(modifier)'
function! MainFile(fmod)
    " Determine the full path to your main latex file that you want to compile.
    " Store it e.g. in the variable `path`:
    "     let path = some/path/to/main.tex
    " Apply `modifier` to your `path` variable
    return fnamemodify(path, a:fmod)
endif

示例

项目结构如下所示:

代码语言:javascript
复制
project/
    main.tex
    sup.tex
    .local-vimrc
    main-source/
        input1.tex
        input2.tex
    sup-source/
        input1.tex
        input2.tex

我加载了一个.local-vimrc文件(使用插件MarcWeber/vim-addon-local-vimrc),其中我设置了g:Tex_MainFileExpression,以便如果当前缓冲区中的文件位于文件夹main-source中,则<leader>ll编译main.tex,如果它位于文件夹sup-source中,则编译sup.tex。下面是我的.local-vimrc文件。我几乎没有使用g:Tex_MainFileExpression的经验,所以这可能有点老生常谈,但它可能有助于了解如何使用vimscript。

代码语言:javascript
复制
let g:Tex_MainFileExpression = 'g:My_MainTexFile(modifier)'

function! g:My_MainTexFile(fmod)
  " Get absolute (link resolved) paths to this script and the open buffer
  let l:path_to_script = fnamemodify(resolve(expand('<sfile>:p')), ':h')
  let l:path_to_buffer = fnamemodify(resolve(expand('%:p')), ':h')

  " Check if the buffer file is a subdirectory of `main-source` or `sup-source`
  " stridx(a, b) returns -1 only if b is not substring of a
  if stridx(l:path_to_buffer, 'main-source') != -1
    let l:name = 'main.tex'
  elseif stridx(l:path_to_buffer, 'sup-source') != -1
    let l:name = 'sup.tex'
  else
    echom "Don't know what's the root tex file. '".@%."' is not in 'main-source/' or 'sup-source/' directory."
    return ''
  endif

  " Concatenate this script path with main latex file name
  " NOTE: this assumes that this script is located in the same folder as the
  "       main latex files 'main.tex' and 'sup.tex'
  let l:path = l:path_to_script.'/'.l:name
  return fnamemodify(l:abs_path_main, a:fmod)
endfunction
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28567148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档