I would like to add the label commands \pflabel and \pflonglabel, as well as their corresponding reference command \pfref to RefTeX. I would like them to work like \label/\ref do by default, but be additional options.

Would someone know how I might do this?

  • 1
    Are you also using AUCTeX? Sep 3, 2019 at 18:36
  • Yes, I am using AUCTeX! Sep 6, 2019 at 21:46

1 Answer 1


Since you're using AUCTeX as well, I suggest you put your TeX macros inside a package, e.g. pflabel.sty, which can look like this:

  [2019/09/08 v1.0 Support for pflabel's]

Save this file in a directory where LaTeX can find it.

Next, you need to write the corresponding support file pflabel.el for AUCTeX and RefTeX which can look like this:

 (lambda ()

    '("pflabel"     TeX-arg-define-label)
    '("pflonglabel" TeX-arg-define-label)
    '("pfref"       TeX-arg-ref))

   ;; Tell AUCTeX about the macros
    '("\\\\pf\\(?:long\\)?label{\\([^\n\r%\\{}]+\\)}" 1 LaTeX-auto-label))

   ;; Check if reftex-vars.el is loaded:
   (when (featurep 'reftex-vars)
     (make-local-variable 'reftex-label-alist)
     (make-local-variable 'reftex-label-regexps)
     (make-local-variable 'reftex-ref-style-alist)
     (dolist (mac '("pflabel" "pflonglabel"))
       (add-to-list 'reftex-label-alist
                    `(,(concat "\\" mac "{*}") nil nil nil nil)
       (add-to-list 'reftex-label-regexps
                    (concat "\\\\" mac "{\\(?1:[^\n\r%\\{}]+\\)}")
       (unless (string-match-p
                (mapconcat #'identity reftex-label-regexps "\\|"))

     ;; Add a new reference style
     (add-to-list 'reftex-ref-style-alist
                  '("PFlabel" "pflabel"
                    (("\\pfref" ?P)))

     ;; And activate it
     (and LaTeX-reftex-ref-style-auto-activate
          (fboundp 'reftex-ref-style-activate)
          (reftex-ref-style-activate "PFlabel")))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("pflabel"     "{")
                                ("pflonglabel" "{")
                                ("pfref"       "{"))

;;; pflabel.el ends here

Customize the variable TeX-style-private to a directory of your choice, e.g.:

(setq TeX-style-private (expand-file-name "~/.emacs.d/auctex-styles"))

And save pflabel.el in that directory. Make sure you have the following line in your init file:

(setq TeX-parse-self t)

and this line in your .tex file.


Now restart Emacs and load your .tex file.

  • Wow, thank you!! I will try this as soon as I can! Sep 11, 2019 at 21:25
  • Thanks again! And, my apologies for taking so long to reply. I tried your solution, but received an error: ERROR: Paragraph ended before \Hy@setref@link was complete. --- TeX said --- <to be read again> \par l.3069 --- HELP --- A blank line occurred in a command argument that shouldn't contain one. You probably forgot the right brace at the end of an argument. Sep 21, 2019 at 15:26
  • Just to be sure, I already have pflabel, pflonglabel, and pfref commands in my tex file that work and are independent from \label/\ref. I would like to add them to RefTeX so that it makes custom tags, etc., like it does for \label/\ref. What makes it necessary to define \pflabel, etc. as \label, \ref in the package? Sep 21, 2019 at 15:26
  • 1
    @AnthonyPulido - \usepackage{pflabel} is only needed to give AUCTeX the hint that it should load pflabel.el. You can move your LaTeX code in a .sty file, but you don't have to. You can fool AUCTeX by putting this in your preamble: \iffalse \usepackage{pflabel} \fi. IMO, the lisp code above should be in an AUCTeX style file and loaded upon request. Otherwise, you have to pollute your init file with it and that's more effort. Sep 22, 2019 at 20:26
  • 1
    @AnthonyPulido - Do you mean from AUCTeX/RefTeX POV? No, not that I'm aware of. AUCTeX/RefTeX can also handle macros like \eqref, \cref etc. Sep 29, 2019 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .