auctex-devel
[Top][All Lists]
Advanced

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

Re: [AUCTeX-devel] [problems with TeX-arg-ref]


From: Arash Esbati
Subject: Re: [AUCTeX-devel] [problems with TeX-arg-ref]
Date: Thu, 17 Nov 2016 09:29:59 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1

Uwe Brauer <address@hidden> writes:

>    > Hi Uwe,
>    > 2016-11-16 18:20 GMT+01:00 Uwe Brauer <address@hidden>:
>
>    > I suspect you may want to use something like the `LaTeX-label'
>    > function, or ultimately call
>
>    >     (funcall LaTeX-label-function arg
>
>    > if using `LaTeX-label' is not possible, but I still don't know exactly
>    > what you want to do.
>
> Ok, here is a simple situation. With all my reftex setting, I hit
>
>  C-c C-c
>  RET
>  equation
>  and voila I obtain automatically without and prompt
>
> \begin{equation}
>   \label{eq:exam-templ:1}
>   
> \end{equation}

I assume you mean `C-c C-e equation RET', but that's not the point.

> The prefix, eq: and exam-templ: are a result by my configuration, the
> number is generated automatically by reftex. This is the behavior I am
> looking for and the code
>
>     '("titledquestion" "Title" ["Points"] reftex-label)
>
> Does precisely what I want. But I was advised not using this function.
> Fine, but I want a solution so that RefTeX users will benefit from RefTeX
> automatic label system, and no RefTeX users will suffer from the lack of it.

It is all there, you just have to hook your environments into RefTeX
with `reftex-add-label-environments' and you're done.  The first 2
variables should go into `tex-style.el' once you're finished.  Append
environments where you want the automatic label insertion to the first
`(envs '("questions"))'.

--8<---------------cut here---------------start------------->8---
;;; Code:

(defcustom LaTeX-exam-reftex-quick-id-key ?x
  "Unique letter identifying exam class macros in RefTeX.

A character argument for quick identification when RefTeX inserts
new labels with `reftex-label'.  It must be unique.  It is
initialized to ?x."
  :group 'LaTeX-style
  :type 'character)

(defcustom LaTeX-exam-label "exm:"
  "Default prefix to labels in environments of exam class."
  :group 'LaTeX-style
  :type 'string)

(defvar LaTeX-exam-class-options '("answers" "addpoints")
  "Class options for the exam class.")

(defun LaTeX-exam-insert-item ()
  "Insert a new item in an environment from exam class.
Item inserted depends on the environment."
  (TeX-insert-macro
   (cond ((string= environment "questions")
          "question")
         ((string= environment "parts")
          "part")
         ((string= environment "subparts")
          "subpart")
         ((string= environment "subsubparts")
          "subsubpart")
         ;; Fallback
         (t "item"))))

(defun LaTeX-exam-insert-label (_optional &optional name type)
  "Indent the line and query/insert a label incl. the \"\\label\" macro.
Arguments NAME and TYPE are the same as for the function
`LaTeX-label'.  OPTIONAL is ignored."
  (indent-according-to-mode)
  (let ((currenv (LaTeX-current-environment)))
    (LaTeX-label (or name currenv) (or type 'environment))))
  
(TeX-add-style-hook
 "exam"
 (lambda ()

   ;; Make our label prefix available ...
   (let ((envs '("questions")))
     (dolist (env envs)
       ;; to AUCTeX
       (add-to-list 'LaTeX-label-alist
                    (cons env 'LaTeX-exam-label))
       ;; to RefTeX with `reftex-add-label-environments'
       (when (fboundp 'reftex-add-label-environments)
         (reftex-add-label-environments
          `((,env ,LaTeX-exam-reftex-quick-id-key ,LaTeX-exam-label
                  "~\\ref{%s}" nil
                  (regexp "[Qq]uestions?" "[Nn]umbers?")))))))
   
   (LaTeX-add-environments "solution"
                           "solutionorbox"
                           "solutionorlines"
                           "solutionordottedlines"
                           '("questions" LaTeX-env-item)
                           '("parts" LaTeX-env-item)
                           '("subparts" LaTeX-env-item)
                           '("subsubparts" LaTeX-env-item))

   ;; Tell AUCTeX about special environments:
   (let ((envs '("questions" "parts" "subparts" "subsubparts")))
     (dolist (env envs)
       (add-to-list 'LaTeX-item-list
                    (cons env 'LaTeX-exam-insert-item))))

   ;; Append us only once:
   (unless (and (string-match "question" LaTeX-item-regexp)
                (string-match "subsub" LaTeX-item-regexp))
      (set (make-local-variable 'LaTeX-item-regexp)
           (concat
            LaTeX-item-regexp
            "\\|"
            "\\(titled\\)?question\\b"
            "\\|"
            "\\(sub\\|subsub\\)?part\\b")))
  
   (TeX-add-symbols
    '("part" [ "Points" ] (TeX-arg-literal " "))
    '("subpart" [ "Points" ] (TeX-arg-literal " "))
    '("subsubpart" [ "Points" ] (TeX-arg-literal " "))
    '("question"  ["Points"] (TeX-arg-literal " "))
    '("titledquestion" "Title" ["Points"] LaTeX-exam-insert-label 
(TeX-arg-literal " "))
    '("addpoints" 0)
    ;; ...
    )

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("question"        "[")
                                ("titledquestion"  "{[")
                                ("subpart"         "[")
                                ("subsubpart"      "["))
                              'textual)))
 LaTeX-dialect)

;;; exam.el ends here
--8<---------------cut here---------------end--------------->8---

Best, Arash



reply via email to

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