auctex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [AUCTeX] Support for "\Label" and "\myref"


From: Arash Esbati
Subject: Re: [AUCTeX] Support for "\Label" and "\myref"
Date: Mon, 22 Aug 2016 19:56:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1

AW <address@hidden> writes:

> "\Label" and "\myref" simply should in Emacs be supported like "\label" and 
> "\ref". (I know, sounds like a chrismas wish :-)  )

In general, AUCTeX supports vanilla LaTeX.  Additional commands and
environments are supported via style files.  So you have to address this
Christmas wish to LaTeX3 first ;-)

> I use these commands for a kind of abbreviated links, see here (only if you 
> are interested): http://tex.stackexchange.com/a/325871/4736
>
> Is this something difficult? I found "reftex-label-alist", but adding 
>
> (setq reftex-label-alist
>    '(("\Label{*}"   nil  "%S"  "%s" t "" 1)
>      ("\myref{*}"   nil "%S"   "%s" t "" 1)))
>
> into my .emacs did not work -- there is no support like for \ref and \label.
>
> From the code I posted at stackexchange I will make a local package called
> "abbreviated-refs.sty". If this package is loaded, after a section command, 
> when usually Emacs offers to insert a \label, instead \Label should be 
> offered.

The final scenario is that \Label is only included after sectioning
commands?  Within AUCTeX, i.e. without Reftex, you probably could get
away with a style file `abbreviated-refs.el' like this (untested!).
Function `LaTeX-section-label' is altered to
`LaTeX-abbrev-refs-section-label' and it calls `LaTeX-abbrev-refs-label'
instead of `LaTeX-label':

--8<---------------cut here---------------start------------->8---
(defun LaTeX-abbrev-refs-label (name &optional type)
  "Insert a Label for NAME at point.
The optional TYPE argument can be either environment or section:
in the former case this function looks up `LaTeX-label-alist' to
choose which prefix to use for the label, in the latter case
`LaTeX-section-label' will be looked up instead.  If TYPE is nil,
you will be always prompted for a label, with an empty default
prefix.

If `LaTeX-label-function' is a valid function, LaTeX label will
transfer the job to this function.

The inserted label is returned, nil if it is empty."
  (let ((TeX-read-label-prefix
         (cond
          ((eq type 'environment)
           (cdr (assoc name LaTeX-label-alist)))
          ((eq type 'section)
           (if (assoc name LaTeX-section-list)
               (if (stringp LaTeX-section-label)
                   LaTeX-section-label
                 (and (listp LaTeX-section-label)
                      (cdr (assoc name LaTeX-section-label))))
             ""))
          ((null type)
           "")
          (t
           nil)))
        label)
    (when (symbolp TeX-read-label-prefix)
      (setq TeX-read-label-prefix (symbol-value TeX-read-label-prefix)))
    (when TeX-read-label-prefix
      (if (and (boundp 'LaTeX-label-function)
               LaTeX-label-function
               (fboundp LaTeX-label-function))
          (setq label (funcall LaTeX-label-function name))
        ;; Use completing-read as we do with `C-c C-m \label RET'
        (setq label (TeX-read-label t "What label" t))
        ;; No label or empty string entered?
        (if (or (string= TeX-read-label-prefix label)
                (string= "" label))
            (setq label nil)
          ;; Modification start: Insert "Label" instead of "label"
          (insert TeX-esc "Label" TeX-grop label TeX-grcl)) ; Modification end
        (if label
            (progn
              (LaTeX-add-labels label)
              label)
          nil)))))

(defun LaTeX-abbrev-refs-section-label ()
  "Hook to insert a label after the sectioning command.
Insert this hook into `LaTeX-section-hook' to prompt for a label to be
inserted after the sectioning command.

The behaviour of this hook is controlled by variable `LaTeX-section-label'."
  (and (LaTeX-abbrev-refs-label name 'section)
       (LaTeX-newline)))

(TeX-add-style-hook
 "abbreviated-refs"
 (lambda ()

   (TeX-add-symbols
    '("Label" TeX-arg-define-label)
    '("myref" TeX-arg-ref))

   ;; Alter LaTeX-section-hook: Replace `LaTeX-section-label' w/
   ;; `LaTeX-abbrev-refs-section-label':
   (make-local-variable 'LaTeX-section-hook)
   (setq LaTeX-section-hook
         '(LaTeX-section-heading
           LaTeX-section-title
           LaTeX-section-section
           LaTeX-abbrev-refs-section-label))

   ;; Cater Reftex support:
   (when (boundp 'reftex-label-alist)
     (add-to-list 'reftex-label-alist
                  '("\\Label{*}" nil nil nil nil) t))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("Label" "{")
                                ("myref" "{"))
                              'reference)))
 LaTeX-dialect)
--8<---------------cut here---------------end--------------->8---

With Reftex, the job is done by `reftex-label' and the modification
above does not work anymore.  Altering `reftex-label' is something you
want to do with the next flood ;-)  My suggestion would be to hack
`LaTeX-abbrev-refs-section-label' to search back and replace \label with
\Label or to do it on TeX level with a flag that \label expands to
\Label after \section and to original \label otherwise.

> Unfortunately this is way above my skills to code lisp (it took a
> whole day to write the code for the abbreviated links in some strange
> new kind of LateX3).
>
> Maybe someone can write some lines on a rainy day?

The forecast says no rain for the rest of the week ;-)

Best, Arash



reply via email to

[Prev in Thread] Current Thread [Next in Thread]