;;; tex-output.el --- description -*- lexical-binding: t; -*- (defcustom TeX-output-dir nil "*The place where the output files will be generated. If this variable is nil, AUCTeX will assume that the output dir is the directory of TeX-master. It is suggested that you use the File Variables (see the info node in the Emacs manual) to set this variable permanently for each file." :group 'TeX-command :group 'TeX-parse :type '(string :format "%v")) (make-variable-buffer-local 'TeX-output-dir) (put 'TeX-output-dir 'safe-local-variable 'stringp-or-null-p) (defun TeX-master-output-file (&optional extension) "Return the value of `TeX-output-dir' in the master-file, opening it if necessary. if the optional argument is non-nil it is appended as an extension to the output file" (interactive) (if (eq extension t) (setq extension TeX-output-extension)) (let ((file (TeX-master-file t)) name) (with-current-buffer (or (find-buffer-visiting file) (find-file-noselect file)) (when TeX-output-dir ;; Expand file name in case the master directory is different from the ;; current directory (setq name (concat (expand-file-name TeX-output-dir) (TeX-master-file))))) (if name (if extension (concat name "." extension) name) (TeX-master-file extension)))) (defun TeX-clean-extensions-regexp (&optional arg) "Return a regexp to match extensions that should be cleaned by TeX-clean. If the optional argument ARG is non-nil then output files are included" (let* ((mode-prefix (TeX-mode-prefix)) (suffixes (append (symbol-value (intern (concat mode-prefix "-clean-intermediate-suffixes"))) (when arg (symbol-value (intern (concat mode-prefix "-clean-output-suffixes")))))) (regexp (concat (mapconcat 'identity suffixes "\\|") "\\|" (regexp-quote (TeX-region-file t t))))) regexp)) (define-advice TeX-active-master (:around (TeX-master-file-fn &optional extension &rest args) TeX-active-master-adv) (if (or (null extension) (equal extension t) (not (string-match-p (TeX-clean-extensions-regexp t) (concat "." extension)))) (apply TeX-master-file-fn (cons extension args)) (TeX-master-output-file extension))) ;;;;;;;;;;; Example hook (defun set-latex-build-dir () ;; Check if the current file is its own master (setq TeX-output-dir "build/") (make-directory TeX-output-dir t) ;; Ensure directory exists (setq TeX-command-extra-options (concat TeX-command-extra-options " --output-directory=\"" TeX-output-dir "\""))) (add-hook 'LaTeX-mode-hook 'set-latex-build-dir)