[Top][All Lists]

[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: Wed, 24 Aug 2016 17:35:16 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1

Hi Alexander,

AW <address@hidden> writes:

> Am Dienstag, 23. August 2016, 09:20:26 CEST schrieb Arash Esbati:
>> The code below would also work with Reftex.  It is the easiest solution
>> comes to my mind if you only want \Label after sectioning commands.  The
>> function `LaTeX-abbrev-ref-section-label' does the same job as
>> `LaTeX-section-label' and then replaces "l" with "L" in \label.
> Flabbergasted. You let the whole machinery do the work and then change just 
> the letter -- funny idea.

One can get become creative when it comes to avoid `reftex-label' ;-)

> I wrote some lines and tested it -- it works, I get \Label after sections. 

Thanks.  But the last implementation had a major flaw: the search was
unlimited and Emacs would have replaced at least one \label.  When used
after a starred sectioning command, where usually a \label is missing,
the code would have messed with your text.

> If I'm trying to put a label into running text, there is an error message:
> reftex-nth-arg-wrapper: Wrong type argument: number-or-marker-p, nil
> but I can live with this.

I'm not sure if that is a Reftex bug somehow.  Reftex seems to get upset
if you name the command \Label.  Can you leave with something like
\seclabel instead of \Label?  If you can, try this version.  The search
back is limited to the last sectioning command.

--8<---------------cut here---------------start------------->8---
(defun LaTeX-abbrev-ref-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'.

Also, search back and replace \\label with \\seclabel.  The
search is limited to the sectioning command just inserted in
order not to replace any other \\label commands if no \\label is
inserted, e.g. after \\section*."
  (and (LaTeX-label name 'section)
  (let ((labelpos
           (re-search-backward (concat (regexp-quote TeX-esc) "label")
                                 (re-search-backward (concat (regexp-quote 
TeX-esc) name)))
    (when labelpos
        (goto-char (1+ labelpos))
        (insert "sec")))))

 (lambda ()

    '("seclabel" TeX-arg-define-label)
    '("myref" TeX-arg-ref))

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

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

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

;;; abbrev-ref.el ends here
--8<---------------cut here---------------end--------------->8---

> I'm looking for the same kind of support for the new "\myref", which
> should be treated exactly like "\ref" by Emacs.

Can you elaborate that?  With the code above, I press `C-c C-m myref
RET' and go through the same procedure as with `ref'.

> You did a lot for me, so I hesitate to ask for more. What do you
> think, should I ask on tex.stackexchange and notify you about the
> question there?

You're welcome.  I suggest you stay on this list here; we are almost
done (I hope :-)

Best, Arash

reply via email to

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