1

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

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:

\ProvidesPackage{pflabel}
  [2019/09/08 v1.0 Support for pflabel's]
\let\pflabel\label
\let\pflonglabel\label
\let\pfref\ref
\endinput

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:

(TeX-add-style-hook
 "pflabel"
 (lambda ()

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

   ;; Tell AUCTeX about the macros
   (TeX-auto-add-regexp
    '("\\\\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)
                    t)
       (add-to-list 'reftex-label-regexps
                    (concat "\\\\" mac "{\\(?1:[^\n\r%\\{}]+\\)}")
                    t)
       (unless (string-match-p
                "\\<pflabel\\>"
                (mapconcat #'identity reftex-label-regexps "\\|"))
         (reftex-compile-variables)))

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

     ;; 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"       "{"))
                              'reference)))
 LaTeX-dialect)

;;; 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.

\usepackage{pflabel}

Now restart Emacs and load your .tex file.

  • Wow, thank you!! I will try this as soon as I can! – Anthony Pulido Sep 11 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. – Anthony Pulido Sep 21 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? – Anthony Pulido Sep 21 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. – Arash Esbati Sep 22 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. – Arash Esbati Sep 29 at 9:56

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.