AUCTeX and master file by means of *.latexmain file, such as in vim

I am thinking to switch from vim + latex-vim module to emacs + evil + auctex,etc modules for editing latex files. I am completely new for emacs.

I have a lot of latex projects written for vim-latex with many files on different levels of a project directory. In vim-latex the master file is defined by a file {master-file-name}.latexmain, eg, if the main file is main.tex, then in the same directory there is main.tex.latexmain. I would like to have similar behavior in emacs (ideally accomplished with the emacs variable approach when it is necessarily, eg, several master files in the same project directory).

Below you find the code for finding the name of the master file (it is my first elisp code, so any comments about how it can be ameliorated are very welcome)

(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)
    )
  )

However, I was not able to find where I can insert this code to modify the behavior of finding master files in AUCTeX. But may be there is a simpler or more beautiful way?

The related questions are

  • What should I modify in order to open (by means of find-file-at-point or similar) input{tex/blabla2.tex} starting from the master path directory rather than the current directory? For example, I have three files: projdir/main.tex, projdir/tex/blabla1.tex, projdir/tex/blabla2.tex; and if I am editing blabla2.tex and there is input{tex/blabla1.tex} how can I open this file?
  • How to force AUCTeX to define the same type of document as it is in the master file, eg, if the main file type is 'latex', then all the files in the same project directory are also 'latex' (if not specified explicitly), not 'tex' as it is defined for some of files in my current settings.

  • I have finally found some answers. The code below solves some questions (can be put in .emacs files). We change also default directory in order to force find-file-at-point (ffap) to find the file starting from the master-file directory.

    It seems that there is a bit strange behavior for ffap function. When there are files dir/part-I.tex and dir/part-II.tex and an input like input{dir/part-I} , function ffap does not want to find dir/part-I.tex, while with input{dir/part-II} it works just fine. But it is not a big issue.

    Finally, I still do not know how to select the file type based on the type of the master file...

    (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)
    

    For ffap working in latex mode from master-dir file we the following command:

    (advice-add 'ffap-latex-mode :before-until #'(lambda (name) 
      (ffap-locate-file name 
                        '(".cls" ".sty" ".tex" "") 
                        (cons (file-name-directory TeX-master) 
                        '()))))
    
    链接地址: http://www.djcxy.com/p/33402.html

    上一篇: 在Emacs中文件局部变量

    下一篇: AUCTeX和主文件通过* .latexmain文件的方式,比如在vim中