Discussion:
[AUCTeX] Support for "\Label" and "\myref"
AW
2016-08-22 12:24:58 UTC
Permalink
Hi,

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

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.

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?

Regards,

Alexander
Arash Esbati
2016-08-22 17:56:42 UTC
Permalink
Post by AW
"\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 ;-)
Post by AW
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.
Post by AW
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
AW
2016-08-22 21:25:04 UTC
Permalink
Hi Arash !
...
Post by Arash Esbati
Post by AW
I use these commands for a kind of abbreviated links, see here (only if you
are interested): http://tex.stackexchange.com/a/325871/4736
...
Post by Arash Esbati
The final scenario is that \Label is only included after sectioning
commands?
Yes!
Post by Arash Esbati
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'
OK, thank you very much!!!

I simply put the tex-code I wrote into the file abbrev-ref.sty, which I attach
here.
Then I copied your code into abbrev-ref.el and saved it to the same place, a
temp directory.

In this directory I opened a new *.tex file and tried to write an article. But
after inserting a section, I just get the usual prompt and "\label".

However, I often use reftex to get e.g. a table of contents in Emacs (C-c = ).

Difficult.
Post by Arash Esbati
--8<---------------cut here---------------start------------->8---
... nice code...
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 ;-)
Reftex seems to be able to deal with varioref. Maybe we can steal some ideas
there.
Post by Arash Esbati
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.
Öh, äh, yes. Whut?
Post by Arash Esbati
Post by AW
Maybe someone can write some lines on a rainy day?
The forecast says no rain for the rest of the week ;-)
Yes, I'm on vacation in Netherlands and after some rainy days tomorrow the
nice weather is appreciated really!
Post by Arash Esbati
Best, Arash
Kind regards,

Alexander
Arash Esbati
2016-08-23 07:20:26 UTC
Permalink
Post by AW
...
Post by Arash Esbati
Post by AW
I use these commands for a kind of abbreviated links, see here (only if you
are interested): http://tex.stackexchange.com/a/325871/4736
...
Post by Arash Esbati
The final scenario is that \Label is only included after sectioning
commands?
Yes!
Ok, thanks.
Post by AW
Post by Arash Esbati
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'
OK, thank you very much!!!
I simply put the tex-code I wrote into the file abbrev-ref.sty, which I attach
here.
Then I copied your code into abbrev-ref.el and saved it to the same place, a
temp directory.
In this directory I opened a new *.tex file and tried to write an article. But
after inserting a section, I just get the usual prompt and "\label".
As I said, the code I sent only works when Reftex is disabled.
Post by AW
Reftex seems to be able to deal with varioref. Maybe we can steal some ideas
there.
Not really, varioref.sty defines a series of referencing commands and
not a new command like \Label to define new anchors. Or am I missing
something?
Post by AW
Post by Arash Esbati
My suggestion would be to hack
`LaTeX-abbrev-refs-section-label' to search back and replace \label with
\Label
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.

I suggest you set the variable `TeX-style-private' in your init file, e.g.

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

and save this code there as `abbrev-ref.el'. Restart Emacs and open
your tex file with \usepackage{abbrev-ref}, hit C-c C-n, and then insert
a section with C-c C-s.

--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 \\Label."
(and (LaTeX-label name 'section)
(LaTeX-newline))
(save-excursion
(re-search-backward (concat (regexp-quote TeX-esc) "label"))
(forward-char)
(delete-char 1)
(insert "L")))

(TeX-add-style-hook
"abbrev-ref"
(lambda ()

(TeX-add-symbols
'("Label" 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
'(LaTeX-section-heading
LaTeX-section-title
LaTeX-section-section
LaTeX-abbrev-ref-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)

;;; abbrev-ref.el ends here
--8<---------------cut here---------------end--------------->8---
Post by AW
Yes, I'm on vacation in Netherlands and after some rainy days tomorrow the
nice weather is appreciated really!
Enjoy it!

Best, Arash
AW
2016-08-24 13:56:20 UTC
Permalink
Hi Arash,
Post by 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.

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

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.

So: Thank you very much!

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

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?

Kind Regards,
Alexander
Post by Arash Esbati
I suggest you set the variable `TeX-style-private' in your init file, e.g.
(setq TeX-style-private (expand-file-name "~/.emacs.d/styles/private"))
and save this code there as `abbrev-ref.el'. Restart Emacs and open
your tex file with \usepackage{abbrev-ref}, hit C-c C-n, and then insert
a section with C-c C-s.
--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 \\Label."
(and (LaTeX-label name 'section)
(LaTeX-newline))
(save-excursion
(re-search-backward (concat (regexp-quote TeX-esc) "label"))
(forward-char)
(delete-char 1)
(insert "L")))
(TeX-add-style-hook
"abbrev-ref"
(lambda ()
(TeX-add-symbols
'("Label" TeX-arg-define-label)
'("myref" TeX-arg-ref))
;; Alter LaTeX-section-hook: Replace `LaTeX-section-label' w/
(make-local-variable 'LaTeX-section-hook)
(setq LaTeX-section-hook
'(LaTeX-section-heading
LaTeX-section-title
LaTeX-section-section
LaTeX-abbrev-ref-section-label))
(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)
;;; abbrev-ref.el ends here
--8<---------------cut here---------------end--------------->8---
Post by AW
Yes, I'm on vacation in Netherlands and after some rainy days tomorrow the
nice weather is appreciated really!
Enjoy it!
Best, Arash
-------------------------------------------------------------
Arash Esbati
2016-08-24 15:35:16 UTC
Permalink
Hi Alexander,
Post by AW
Post by 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' ;-)
Post by AW
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.
Post by AW
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)
(LaTeX-newline))
(let ((labelpos
(save-excursion
(re-search-backward (concat (regexp-quote TeX-esc) "label")
(save-excursion
(re-search-backward (concat (regexp-quote TeX-esc) name)))
t))))
(when labelpos
(save-excursion
(goto-char (1+ labelpos))
(insert "sec")))))

(TeX-add-style-hook
"abbrev-ref"
(lambda ()

(TeX-add-symbols
'("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
'(LaTeX-section-heading
LaTeX-section-title
LaTeX-section-section
LaTeX-abbrev-ref-section-label))

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

;;; abbrev-ref.el ends here
--8<---------------cut here---------------end--------------->8---
Post by AW
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'.
Post by AW
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
AW
2016-08-24 20:43:32 UTC
Permalink
Post by Arash Esbati
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'.
Hi,

I realised that KOMA-script already has "myref" as a variable, so I choose
"secref" instead, we now have "seclabel" and "secref".

Attached I send the adapted abbrev-ref.sty and abbrev-ref.sty.

AucTeX asks for seclabel after any section, function like \label.

\secref does not trigger any action by AucTeX.

It's late today, I can't write a minimal .emacs just with the necessary
commands to load AUCTeX and abbrev-ref.el. But I searched my .emacs and I
don't mess with reftex, as far as I can see.

Thank you for your helping hand,

Regards,

Alexander
AW
2016-08-31 15:44:16 UTC
Permalink
Post by AW
\secref does not trigger any action by AucTeX.
Works. No idea, why not before, I didn't change anything.

@Arash: Thaaank you!
--
Regards,

Alexander
Arash Esbati
2016-09-01 09:35:48 UTC
Permalink
Post by AW
Post by AW
\secref does not trigger any action by AucTeX.
Works. No idea, why not before, I didn't change anything.
@Arash: Thaaank you!
You're welcome.

Best, Arash

Loading...