【问题标题】:AUCTeX and master file by means of *.latexmain file, such as in vim-latexAUCTeX 和 master 文件通过 *.latexmain 文件,例如在 vim-latex 中
【发布时间】:2015-04-19 10:55:04
【问题描述】:

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

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

您可以在下面找到用于查找主文件名称的代码(这是我的第一个 elisp 代码,因此非常欢迎任何关于如何改进它的 cmets)

(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”。

【问题讨论】:

    标签: vim emacs latex elisp auctex


    【解决方案1】:

    我终于找到了一些答案。下面的代码解决了一些问题(可以放在 .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 文件中以乳胶模式工作的 ffap,我们使用以下命令:

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

    【讨论】:

    • 谢谢!这正是我从 vim 切换到 spacemacs 后所寻找的!
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多