7

How to make this configuration file work with use-package with regard to AucTeX, RefTeX and LaTeX. I usually write latex rahter than pure tex documents.

Current Working Emacs Configuration

(load "auctex.el" nil t t)
(setq-default TeX-master nil) ; by each new fie AUCTEX will ask for a master fie.
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-PDF-mode t)
(setq TeX-show-compilation nil)         ; if `t`, automatically shows compilation log
;; (setq-default TeX-engine 'xetex)     ; optional
(setq TeX-save-query nil)       ; don't prompt for saving the .tex file

;; add makeglossaries to TeX-run-command-list
(eval-after-load "tex" '(add-to-list 'TeX-command-list
                     '("Makeglossaries" "makeglossaries %s" TeX-run-command nil
                       (latex-mode)
                       :help "Run makeglossaries script, which will choose xindy or makeindex") t))

;; Font-lock for AUCTeX
;; Note: '«' and '»' is by pressing 'C-x 8 <' and 'C-x 8 >', respectively
(font-lock-add-keywords 'latex-mode (list (list "\\(«\\(.+?\\|\n\\)\\)\\(+?\\)\\(»\\)" '(1 'font-latex-string-face t) '(2 'font-latex-string-face t) '(3 'font-latex-string-face t))))

;; Add standard Sweave file extensions to the list of files recognized  by AUCTeX.
(setq TeX-file-extensions
      '("Rnw" "rnw" "Snw" "snw" "tex" "sty" "cls" "ltx" "texi" "texinfo" "dtx"))

(setq LaTeX-babel-hyphen nil) ; Disable language-specific hyphen insertion.
;; `"` expands into csquotes macros (for this to work babel must be loaded after csquotes).
(setq LaTeX-csquotes-close-quote "}"
     LaTeX-csquotes-open-quote "\\enquote{")

;; RefTeX
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTeX t)
(add-hook 'LaTeX-mode-hook (function (lambda() (bind-key "C-c C-r" 'reftex-query-replace-document))))
(add-hook 'LaTeX-mode-hook (function (lambda() (bind-key "C-c C-g" 'reftex-grep-document))))
(add-hook 'TeX-mode-hook (lambda () (reftex-isearch-minor-mode))) ; for AUCTeX

;; A user-defined function to delete the current macro in AUCTeX.
;; Note: keybinds won't be added to TeX-mode-hook if not kept at the end of the AUCTeX setup!
(defun TeX-remove-macro ()
  "Remove current macro and return `t'.  If no macro at point,
    return 'nil'."
  (interactive)
  (when (TeX-current-macro)
    (let ((bounds (TeX-find-macro-boundaries))
      (brace  (save-excursion
            (goto-char (1- (TeX-find-macro-end)))
            (TeX-find-opening-brace))))
      (delete-region (1- (cdr bounds)) (cdr bounds))
      (delete-region (car bounds) (1+ brace)))
    t))
(add-hook 'LaTeX-mode-hook (lambda () (bind-key "M-DEL" 'TeX-remove-macro)))

My Trial with use-package (not working properly)

(use-package reftex
  :commands turn-on-reftex
  :config (setq reftex-plug-into-AUCTeX t))

(use-package tex
  :ensure auctex
  :init
  (progn
    ;; A function to delete the current macro in AUCTeX.
    ;; Note: keybinds won't be added to TeX-mode-hook if not kept at the end of the AUCTeX setup!
    (defun TeX-remove-macro ()
      "Remove current macro and return TRUE, If no macro at point, return Nil."
      (interactive)
      (when (TeX-current-macro)
    (let ((bounds (TeX-find-macro-boundaries))
          (brace  (save-excursion
            (goto-char (1- (TeX-find-macro-end)))
            (TeX-find-opening-brace))))
      (delete-region (1- (cdr bounds)) (cdr bounds))
      (delete-region (car bounds) (1+ brace)))
    t))
    )
  :bind (:map LaTeX-mode-map
          ("M-<delete>" . TeX-remove-macro)
          ("C-c C-r" . reftex-query-replace-document)
          ("C-c C-g" . reftex-grep-document))
  :config
  (progn
    (add-to-list 'TeX-command-list
         '("Makeglossaries" "makeglossaries %s" TeX-run-command nil
           (latex-mode)
           :help "Run makeglossaries script, which will choose xindy or makeindex") t)
    (setq-default TeX-master nil) ; by each new fie AUCTEX will ask for a master fie.
    (setq TeX-auto-save t)
    (setq TeX-save-query nil)       ; don't prompt for saving the .tex file
    (setq TeX-parse-self t)
    (setq-default TeX-PDF-mode t)
    (setq TeX-show-compilation nil)         ; if `t`, automatically shows compilation log
    (setq-default TeX-engine 'xetex)     ; optional
    (setq LaTeX-babel-hyphen nil) ; Disable language-specific hyphen insertion.
    ;; `"` expands into csquotes macros (for this to work, babel pkg must be loaded after csquotes pkg).
    (setq LaTeX-csquotes-close-quote "}"
      LaTeX-csquotes-open-quote "\\enquote{")
    ;; Font-lock for AUCTeX
    ;; Note: '«' and '»' is by pressing 'C-x 8 <' and 'C-x 8 >', respectively
    (font-lock-add-keywords 'latex-mode (list (list "\\(«\\(.+?\\|\n\\)\\)\\(+?\\)\\(»\\)" '(1 'font-latex-string-face t) '(2 'font-latex-string-face t) '(3 'font-latex-string-face t))))
    ;; Add standard Sweave file extensions to the list of files recognized  by AUCTeX.
    (setq TeX-file-extensions
      '("Rnw" "rnw" "Snw" "snw" "tex" "sty" "cls" "ltx" "texi" "texinfo" "dtx"))
    (add-hook 'TeX-mode-hook (lambda () (reftex-isearch-minor-mode))) ; for AUCTeX
    )
  :mode ("\\.tex\\'" . latex-mode)
  )

Issues

When I open any .tex file it is no longer recognized by AucTeX, and no font-locking is there.

After some googling around, I found handful of Emacs dot file setups, but the variety of setups left me clueless as what to do in order to get my LaTeX configuration adjusted after use-package verse given the complexity of the different components and use cases (tex, latex, auctex, reftex, etc).

So given my working current configuration above, how should it be adjusted to fit the use-package verse?

Note

  • Other packages were working as expected with use-package, issues so far only with latex
  • GNU Emacs 25.2.2 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2017-05-06
  • use-package version: 20170710.1234
5

The essential part is to load latex instead of tex, see the first line.
(also I did some small cleanups)

(use-package latex
    :mode
    ("\\.tex\\'" . latex-mode)
    :bind
    (:map LaTeX-mode-map
          ("M-<delete>" . TeX-remove-macro)
          ("C-c C-r" . reftex-query-replace-document)
          ("C-c C-g" . reftex-grep-document))
    :init
    ;; A function to delete the current macro in AUCTeX.
    ;; Note: keybinds won't be added to TeX-mode-hook if not kept at the end of the AUCTeX setup!
    (defun TeX-remove-macro ()
        "Remove current macro and return TRUE, If no macro at point, return Nil."
        (interactive)
        (when (TeX-current-macro)
            (let ((bounds (TeX-find-macro-boundaries))
                  (brace  (save-excursion
                              (goto-char (1- (TeX-find-macro-end)))
                              (TeX-find-opening-brace))))
                (delete-region (1- (cdr bounds)) (cdr bounds))
                (delete-region (car bounds) (1+ brace)))
            t))
    :config
    (add-to-list 'TeX-command-list
                 '("Makeglossaries" "makeglossaries %s" TeX-run-command nil
                   (latex-mode)
                   :help "Run makeglossaries script, which will choose xindy or makeindex") t)

    (setq-default TeX-master nil ; by each new fie AUCTEX will ask for a master fie.
                  TeX-PDF-mode t
                  TeX-engine 'xetex)     ; optional

    (setq TeX-auto-save t
          TeX-save-query nil       ; don't prompt for saving the .tex file
          TeX-parse-self t
          TeX-show-compilation nil         ; if `t`, automatically shows compilation log
          LaTeX-babel-hyphen nil ; Disable language-specific hyphen insertion.
          ;; `"` expands into csquotes macros (for this to work, babel pkg must be loaded after csquotes pkg).
          LaTeX-csquotes-close-quote "}"
          LaTeX-csquotes-open-quote "\\enquote{"
          TeX-file-extensions '("Rnw" "rnw" "Snw" "snw" "tex" "sty" "cls" "ltx" "texi" "texinfo" "dtx"))

    ;; Font-lock for AuCTeX
    ;; Note: '«' and '»' is by pressing 'C-x 8 <' and 'C-x 8 >', respectively
    (font-lock-add-keywords 'latex-mode (list (list "\\(«\\(.+?\\|\n\\)\\)\\(+?\\)\\(»\\)" '(1 'font-latex-string-face t) '(2 'font-latex-string-face t) '(3 'font-latex-string-face t))))
    ;; Add standard Sweave file extensions to the list of files recognized  by AuCTeX.
    (add-hook 'TeX-mode-hook (lambda () (reftex-isearch-minor-mode)))
    )
  • 1
    I wonder which use case progn would be necessary to use? I noticed that you removed it already. Another question, is what difference would be if (defun...) macro were put in the :init block or the :preface block? How can someone tell which keyword to take here? – doctorate Jul 17 '17 at 12:28
  • 1
    I noticed also that you removed the (use-package reftex...) altogether, can you share you idea pls, why was that? – doctorate Jul 17 '17 at 12:32
  • @doctorate with progn it is possible to cluster multiple instructions and stuff them where only one is allowed, use-package is doing this for you already, so it is not needed. (use-package reftex ...) I just did not change it, so I did not write it again. – jue Jul 17 '17 at 17:38
  • @doctorate defun in :init or :preface: I think in your case it makes no difference, because with it, only a function is defined. It is defined before everything else, that way it is already defined when it is needed. – jue Jul 17 '17 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.