vim - AUCTeX 和主文件通过 *.latexmain 文件,例如在 vim-latex 中

标签 vim emacs latex elisp auctex

我正在考虑从 vim + Latex-vim 模块切换到 emacs + escape + auctex 等模块来编辑 Latex 文件。我对 emacs 完全陌生。

我有很多为 vim-latex 编写的 Latex 项目,其中许多文件位于项目目录的不同级别。 在 vim-latex 中,主文件由文件 {master-file-name}.latexmain 定义,例如,如果主文件是 main.tex,那么在同一目录中就有 main.tex.latexmain。我希望在 emacs 中具有类似的行为(最好在必要时使用 emacs 变量方法来完成,例如,同一项目目录中的多个主文件)。

下面是查找主文件名称的代码(这是我的第一个 elisp 代码,因此非常欢迎任何有关如何改进它的评论)

(defun texmode-find-master-file ()
  "Finds the master file for TeX/LaTeX project by searching for '{file-name}.latexmain' in the rood directories"

  (let (foundFiles (currPath (expand-file-name "./")) result)
    (while (and (not foundFiles) (not (equal currPath "/")) )
      (setq foundFiles (directory-files currPath t ".*\.latexmain"))
      (setq currPath (expand-file-name (concat currPath "../")))
      )
    (setq result (file-name-sans-extension (car foundFiles)))
    (and (file-exists-p result) result)
    )
  )

但是,我无法找到可以插入此代码的位置来修改在 AUCTeX 中查找主文件的行为。但可能有更简单或更漂亮的方法吗?

相关问题是

  • 我应该修改什么才能从主路径目录而不是从主路径目录开始打开(通过 find-file-at-point 或类似方式)\input{tex/blabla2.tex}当前目录?例如,我有三个文件:projdir/main.tex、projdir/tex/blabla1.tex、projdir/tex/blabla2.tex;如果我正在编辑 blabla2.tex 并且有 \input{tex/blabla1.tex} 我怎样才能打开这个文件?
  • 如何强制 AUCTeX 定义与主文件中相同类型的文档,例如,如果主文件类型是“latex”,则同一项目目录中的所有文件也是“latex”(如果未明确指定),而不是“tex”,因为它是为我当前设置中的某些文件定义的。

最佳答案

我终于找到了一些答案。下面的代码解决了一些问题(可以放入 .emacs 文件中)。我们还更改默认目录,以强制 find-file-at-point (ffap) 从主文件目录开始查找文件。

ffap 函数似乎有一点奇怪的行为。当存在文件 dir/part-I.tex 和 dir/part-II.tex 以及像 \input{dir/part-I} 这样的输入时,函数 ffap 不想找到 dir/part -I.tex,而使用 \input{dir/part-II} 时效果很好。但这不是一个大问题。

最后我还是不知道如何根据主文件的类型来选择文件类型...

(defun TeX-find-master-file ()
  "Finds the master file for TeX/LaTeX project 
  by searching for '{file-name}.latexmain' in the good directories"
  (let (foundFiles (currPath (expand-file-name "./")) foundFile)
    (while (and (not foundFiles) (not (equal currPath "/")) )
       (setq foundFiles (directory-files currPath t ".*\.latexmain"))
       (setq currPath (expand-file-name (concat currPath "../"))))
    (and
      (setq foundFile (car foundFiles))
      ; removing .latexmain extension
      (setq foundFile (file-name-sans-extension foundFile)) 
      (file-exists-p foundFile)
      foundFile)))

(defun TeX-set-master-file (&optional ignore1 ignore2 ignore3)
  "Finds the master file by means of TeX-find-master-file 
  and set TeX-master to it value"
  (setq TeX-master (or (TeX-find-master-file) TeX-master)))

;Finally we change the func TeX-master-file from AUCTeX
;  in order to first apply the logic with *.latexmain
;  and only then to apply the standard logic of AUCTeX
(advice-add 'TeX-master-file :before #'TeX-set-master-file)

对于在 master-dir 文件中以 latex 模式工作的 ffap,我们使用以下命令:

(advice-add 'ffap-latex-mode :before-until #'(lambda (name) 
  (ffap-locate-file name 
                    '(".cls" ".sty" ".tex" "") 
                    (cons (file-name-directory TeX-master) 
                    '()))))

关于vim - AUCTeX 和主文件通过 *.latexmain 文件,例如在 vim-latex 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728875/

相关文章:

vim - 在 vim 中自动完成 javascript/DOM API?

emacs - 如何将 Latexmk 绑定(bind)到 Emacs 中的一个键并让它显示错误(如果有)

php - Symfony2 - 如何验证上传的文件是否为 tex 文件 (LaTex)

latex - 除第一页外,页眉上的章节编号错误

vim:定义仅在 C++ 注释之外有效的新命令

python - 使用多个参数启动 python pdb?

vim - 如何在 Vim 中将所有文本转换为小写

emacs - YASnippet -- 如何创建右键单击上下文菜单 -- popup-menu

emacs - Slime\Emacs comint 在启动 mpi 进程时挂起

latex - IPython 中的物理括号符号