Discussion:
[AUCTeX] Indent custom env like itemize/enumerate environment
Thorsten Grothe
2017-06-05 14:55:52 UTC
Permalink
Hello,

I'm working with a custom env which looks like this:

\begin{multi}[]{}
bla bla

\item bla
\item bla
\item* bla
\end{multi}

When I indent it with LaTeX-fill-environment it looks like this:

\begin{multi}[]{}
bla bla

\item bla --> wrong indention here!
\item bla --> wrong indention here!
\item* bla --> wrong indention here!
\end{multi}

I guess it's because auctex does not know that this env belongs to
itemize/enumerate envs and cannot fill it accordingly. How can I fix
this and add \begin{multi} ... \end{multi} so it get's properly indented
like itemize/enumerate lists?

Thanks!

Regards

Thorsten Grothe
Arash Esbati
2017-06-05 19:03:23 UTC
Permalink
Post by Thorsten Grothe
\begin{multi}[]{}
bla bla
\item bla
\item bla
\item* bla
\end{multi}
\begin{multi}[]{}
bla bla
\item bla --> wrong indention here!
\item bla --> wrong indention here!
\item* bla --> wrong indention here!
\end{multi}
I guess it's because auctex does not know that this env belongs to
itemize/enumerate envs and cannot fill it accordingly. How can I fix
this and add \begin{multi} ... \end{multi} so it get's properly indented
like itemize/enumerate lists?
Hi Thorsten,

you can tell AUCTeX about your environment with these 2 lines:

(LaTeX-add-environments '("multi" LaTeX-env-item))
(add-to-list 'LaTeX-item-list '("multi" . LaTeX-item-argument))

The question is where to put these 2 lines, there are some options (and
.emacs is not my first choice). How do you define this environment?

Best, Arash
Thorsten Grothe
2017-06-05 21:11:57 UTC
Permalink
Hi Arash,

* Arash Esbati: 05.06.2017 (19:03):

[...]
Post by Arash Esbati
(LaTeX-add-environments '("multi" LaTeX-env-item))
(add-to-list 'LaTeX-item-list '("multi" . LaTeX-item-argument))
The question is where to put these 2 lines, there are some options (and
.emacs is not my first choice). How do you define this environment?
I did not define it myself, it's from the moodle package, see here

https://www.ctan.org/pkg/moodle?lang=de

I need to do some moodle tests for school....

Regards
Thorsten
Thorsten Grothe
2017-06-06 06:54:48 UTC
Permalink
Hi Arash again,

* Arash Esbati: 05.06.2017 (19:03):

[...]
Post by Arash Esbati
(LaTeX-add-environments '("multi" LaTeX-env-item))
(add-to-list 'LaTeX-item-list '("multi" . LaTeX-item-argument))
The question is where to put these 2 lines, there are some options (and
.emacs is not my first choice). How do you define this environment?
I putted this two line in my .emacs and yes that's no good idea, I got
an error:

Symbol’s function definition is void: LaTeX-add-environments


Regards

Thorsten Grothe
Arash Esbati
2017-06-06 20:51:45 UTC
Permalink
Post by Thorsten Grothe
[...]
Post by Arash Esbati
(LaTeX-add-environments '("multi" LaTeX-env-item))
(add-to-list 'LaTeX-item-list '("multi" . LaTeX-item-argument))
The question is where to put these 2 lines, there are some options (and
.emacs is not my first choice). How do you define this environment?
I putted this two line in my .emacs and yes that's no good idea, I got
Symbol’s function definition is void: LaTeX-add-environments
Hi Thorsten,

yes, this is the expected behavior: If you solely put those lines into
your init file, Emacs barks since `LaTeX-add-environments' is defined
after latex.el is loaded.
Post by Thorsten Grothe
I did not define it myself, it's from the moodle package, see here
https://www.ctan.org/pkg/moodle?lang=de
Thanks. In this case, my suggestion above doesn't help either. I'm
afraid you have to write a style file `moodle.el'. I think you take
most parts from `tcolorboxlib-raster.el'[1] and adapt it for moodle. Do
want to give it a roll? You can then submit it to AUCTeX once you're
done. The same goes for xsim package, of course :-)

Best, Arash

Footnotes:
[1] http://git.savannah.gnu.org/cgit/auctex.git/tree/style/tcolorboxlib-raster.el
Thorsten Grothe
2017-06-06 21:07:28 UTC
Permalink
Hi Arash,

* Arash Esbati: 06.06.2017 (20:51):

[...]
Post by Arash Esbati
Thanks. In this case, my suggestion above doesn't help either. I'm
afraid you have to write a style file `moodle.el'. I think you take
most parts from `tcolorboxlib-raster.el'[1] and adapt it for moodle. Do
want to give it a roll? You can then submit it to AUCTeX once you're
done. The same goes for xsim package, of course :-)
well, if I can help to support auctex, I will of course try to do this :-)
My knowledge of writing style files is very limited but I will try
it for moodle, if I need some help I will ask to the list or to you, if
that's ok :-)


Regards

Thorsten Grothe
Arash Esbati
2017-06-08 19:47:14 UTC
Permalink
Post by Thorsten Grothe
well, if I can help to support auctex, I will of course try to do this :-)
That's the spirit! :-)
Post by Thorsten Grothe
My knowledge of writing style files is very limited but I will try it
for moodle, if I need some help I will ask to the list or to you, if
that's ok :-)
Great, just start playing around with your style and send any questions
you have to <auctex-***@gnu.org>. And if you want to submit your code
to AUCTeX, you have assign the copyright to the FSF. You can get the
process started as described here:

http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=doc/Copyright/request-assign.future;hb=HEAD

Best, Arash
Arash Esbati
2017-06-10 14:36:39 UTC
Permalink
Post by Thorsten Grothe
My knowledge of writing style files is very limited but I will try it
for moodle, if I need some help I will ask to the list or to you, if
that's ok :-)
Following up myself, I checked the documentation of moodle and I admit
that I didn't understand how it should work, but a style file can look
like this. The "cloze" environment is missing, but this should be a
starting point. N.B. I just wrote it, I actually didn't test it.

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

(defvar LaTeX-moodle-key-val-options
'(("points")
("default grade")
("penalty")
("fraction")
("feedback"))
"Key=value options for moodle macros and environments.")

(defun LaTeX-moodle-question-env-with-args (env)
"Insert ENV provided by moodle.sty incl. arguments and first \\item."
(LaTeX-insert-environment
env
(let ((opts (TeX-read-key-val
t
(cond (;; 3.3.1 Multiple Choice
(string= env "multi")
(append '(("shuffle" ("true" "false"))
("numbering" ("alph" "Alph" "arabic"
"roman" "Roman" "none"))
("single" ("true" "false"))
("multiple" ("true" "false")))
LaTeX-moodle-key-val-options))
;; 3.3.3 Short Answer
((string= env "shortanswer")
(append '(("case sensitive" ("true" "false"))
("usecase" ("true" "false")))
LaTeX-moodle-key-val-options))
;; 3.3.4 Essay Questions
((string= env "essay")
(append '(("response required" ("true" "false"))
("response format" ("html" "file"
"html+file"
"text" "monospaced"))
("response field lines")
("attachments allowed" ("0" "1" "2" "3"
"unlimited"))
("attachments required" ("0" "1" "2" "3"))
("response template"))
LaTeX-moodle-key-val-options))
;; 3.4 Matching Questions
((string= env "matching")
(append '(("shuffle" ("true" "false"))
("drag and drop" ("true" "false"))
("dd" ("true" "false")))
LaTeX-moodle-key-val-options))
(t LaTeX-moodle-key-val-options))))
(qname (TeX-read-string (TeX-argument-prompt nil nil "Question name"))))
(when (and opts (not (string-equal opts "")))
(format "[%s]" opts))
(format "{%s}" qname)))
(if (TeX-active-mark)
(progn
(LaTeX-find-matching-begin)
(end-of-line 1))
(end-of-line 0))
(delete-char 1)
(when (looking-at (concat "^[ \t]+$\\|"
"^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
(delete-region (point) (line-end-position)))
(delete-horizontal-space)
;; Deactivate the mark here in order to prevent `TeX-parse-macro'
;; from swapping point and mark and the \item ending up right after
;; \begin{...}.
(TeX-deactivate-mark)
;; Query and insert the question text.
(let ((qtext (TeX-read-string (TeX-argument-prompt nil nil "Question Text"))))
(when (and qtext (not (string= qtext "")))
(newline)
(indent-according-to-mode)
(insert qtext)
(LaTeX-fill-paragraph)))
(LaTeX-insert-item)
;; The inserted \item may have outdented the first line to the
;; right. Fill it, if appropriate.
(when (and (not (looking-at "$"))
(not (assoc env LaTeX-indent-environment-list))
(> (- (line-end-position) (line-beginning-position))
(current-fill-column)))
(LaTeX-fill-paragraph nil)))

(defun LaTeX-moodle-item-argument ()
"Insert an \\item with optional argument in environments of moodle package."
(let ((TeX-insert-macro-default-style 'mandatory-args-only))
(TeX-insert-macro "item"))
(let ((opts (TeX-read-key-val t LaTeX-moodle-key-val-options)))
(when (and opts (not (string= opts "")))
(delete-horizontal-space)
(insert LaTeX-optop opts LaTeX-optcl)))
(just-one-space)
(when (string= "matching" (LaTeX-current-environment))
(save-excursion
(insert TeX-esc "answer")
(just-one-space))))

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

(LaTeX-add-environments
;; 3.2 Quiz and Question Environments
'("quiz"
(lambda (environment)
(LaTeX-insert-environment
environment
(let ((opts (TeX-read-key-val t LaTeX-moodle-key-val-options))
(bank (TeX-read-string (TeX-argument-prompt nil nil "Question bank"))))
(concat
(when (and opts (not (string= opts "")))
(format "[%s]" opts))
(format "{%s}" bank)))))))

(TeX-add-symbols
'("moodleset" (TeX-arg-key-val LaTeX-moodle-key-val-options)))

;; Make environments available to AUCTeX:
(dolist (env '("multi" "numerical" "shortanswer" "essay" "matching"))
(LaTeX-add-environments `(,env LaTeX-moodle-question-env-with-args))
(add-to-list 'LaTeX-item-list `(,env . LaTeX-moodle-item-argument) t))

;; Fontification
(when (and (featurep 'font-latex)
(eq TeX-install-font-lock 'font-latex-setup))
(font-latex-add-keywords '(("moodleset" "{"))
'function)
(font-latex-add-keywords '(("answer" "")
;; Cater for a fontified starred \item
("item" "*["))
'textual)))
LaTeX-dialect)

(defvar LaTeX-moodle-package-options
'("draft")
"Package options for the moodle package.")

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

Best, Arash
Thorsten Grothe
2017-06-11 06:51:24 UTC
Permalink
HI Arash,
sorry for the late response, I'm out of office at the moment..
Following up myself, I checked the documentation of moodle nd I admit
that I didn't understand how it should work, but a style file can look
like this. The "cloze" environment is missing, but this should be a
starting point. N.B. I just wrote it, I actually didn't test it.
[...]

thank you very much for doing my job :-) I will test your code and
report anny issues to you, this can take a couple of days because I have
a lot of work at the moment...

Regards

Thorsten Grothe
Arash Esbati
2017-06-11 10:01:22 UTC
Permalink
Post by Thorsten Grothe
thank you very much for doing my job :-)
You're welcome :-)
Post by Thorsten Grothe
I will test your code and report anny issues to you, this can take a
couple of days because I have a lot of work at the moment...
Thanks, but please test the code below. There was a `concat' missing in
`LaTeX-moodle-question-env-with-args'. I also modified
`LaTeX-moodle-item-argument' in order to offer an asterisk in `multi'
environment.

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

(defvar LaTeX-moodle-key-val-options
'(("points")
("default grade")
("penalty")
("fraction")
("feedback"))
"Key=value options for moodle macros and environments.")

(defun LaTeX-moodle-question-env-with-args (env)
"Insert ENV provided by moodle.sty incl. arguments and first \\item."
(LaTeX-insert-environment
env
(let ((opts (TeX-read-key-val
t
(cond (;; 3.3.1 Multiple Choice
(string= env "multi")
(append '(("shuffle" ("true" "false"))
("numbering" ("alph" "Alph" "arabic"
"roman" "Roman" "none"))
("single" ("true" "false"))
("multiple" ("true" "false")))
LaTeX-moodle-key-val-options))
;; 3.3.3 Short Answer
((string= env "shortanswer")
(append '(("case sensitive" ("true" "false"))
("usecase" ("true" "false")))
LaTeX-moodle-key-val-options))
;; 3.3.4 Essay Questions
((string= env "essay")
(append '(("response required" ("true" "false"))
("response format" ("html" "file"
"html+file"
"text" "monospaced"))
("response field lines")
("attachments allowed" ("0" "1" "2" "3"
"unlimited"))
("attachments required" ("0" "1" "2" "3"))
("response template"))
LaTeX-moodle-key-val-options))
;; 3.4 Matching Questions
((string= env "matching")
(append '(("shuffle" ("true" "false"))
("drag and drop" ("true" "false"))
("dd" ("true" "false")))
LaTeX-moodle-key-val-options))
(t LaTeX-moodle-key-val-options))))
(qname (TeX-read-string (TeX-argument-prompt nil nil "Question name"))))
(concat
(when (and opts (not (string= opts "")))
(format "[%s]" opts))
(format "{%s}" qname))))
(if (TeX-active-mark)
(progn
(LaTeX-find-matching-begin)
(end-of-line 1))
(end-of-line 0))
(delete-char 1)
(when (looking-at (concat "^[ \t]+$\\|"
"^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
(delete-region (point) (line-end-position)))
(delete-horizontal-space)
;; Deactivate the mark here in order to prevent `TeX-parse-macro'
;; from swapping point and mark and the \item ending up right after
;; \begin{...}.
(TeX-deactivate-mark)
;; Query and insert the question text.
(let ((qtext (TeX-read-string (TeX-argument-prompt nil nil "Question Text"))))
(when (and qtext (not (string= qtext "")))
(newline)
(indent-according-to-mode)
(insert qtext)
(LaTeX-fill-paragraph)))
(LaTeX-insert-item)
;; The inserted \item may have outdented the first line to the
;; right. Fill it, if appropriate.
(when (and (not (looking-at "$"))
(not (assoc env LaTeX-indent-environment-list))
(> (- (line-end-position) (line-beginning-position))
(current-fill-column)))
(LaTeX-fill-paragraph nil)))

(defun LaTeX-moodle-item-argument ()
"Insert an \\item with optional argument in environments of moodle package."
(let ((TeX-insert-macro-default-style 'mandatory-args-only))
(TeX-insert-macro "item"))
;; Add * to `LaTeX-moodle-key-val-options' in multi environment:
(let ((opts
(TeX-read-key-val t (if (string= "multi" (LaTeX-current-environment))
(append '(("*")) LaTeX-moodle-key-val-options)
LaTeX-moodle-key-val-options))))
(when (and opts (not (string= opts "")))
(delete-horizontal-space)
(if (string= opts "*")
(insert opts)
(insert LaTeX-optop opts LaTeX-optcl))))
(just-one-space)
(when (string= "matching" (LaTeX-current-environment))
(save-excursion
(insert TeX-esc "answer")
(just-one-space))))

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

(LaTeX-add-environments
;; 3.2 Quiz and Question Environments
'("quiz"
(lambda (environment)
(LaTeX-insert-environment
environment
(let ((opts (TeX-read-key-val t LaTeX-moodle-key-val-options))
(bank (TeX-read-string (TeX-argument-prompt nil nil "Question bank"))))
(concat
(when (and opts (not (string= opts "")))
(format "[%s]" opts))
(format "{%s}" bank)))))))

(TeX-add-symbols
'("moodleset" (TeX-arg-key-val LaTeX-moodle-key-val-options)))

;; Make environments available to AUCTeX:
(dolist (env '("multi" "numerical" "shortanswer" "essay" "matching"))
(LaTeX-add-environments `(,env LaTeX-moodle-question-env-with-args))
(add-to-list 'LaTeX-item-list `(,env . LaTeX-moodle-item-argument) t))

;; Fontification
(when (and (featurep 'font-latex)
(eq TeX-install-font-lock 'font-latex-setup))
(font-latex-add-keywords '(("moodleset" "{"))
'function)
(font-latex-add-keywords '(("answer" "")
;; Cater for a fontified starred \item
("item" "*["))
'textual)))
LaTeX-dialect)

(defvar LaTeX-moodle-package-options
'("draft")
"Package options for the moodle package.")

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

Best, Arash
Thorsten Grothe
2017-06-12 17:25:53 UTC
Permalink
Hi Arash,
Post by Arash Esbati
Thanks, but please test the code below. There was a `concat' missing in
`LaTeX-moodle-question-env-with-args'. I also modified
`LaTeX-moodle-item-argument' in order to offer an asterisk in `multi'
environment.
[...]

where is the best place for moodle.el? I got a personal directory under
.emacs.d created with TeX-auto-generate, named simply auctex and put
moodle.el in this dir, but auctex does not see it, it happens nothing,
what I'm doing wrong?

Regards

Thorsten Grothe
Arash Esbati
2017-06-12 20:17:24 UTC
Permalink
Post by Thorsten Grothe
where is the best place for moodle.el? I got a personal directory under
.emacs.d created with TeX-auto-generate, named simply auctex and put
moodle.el in this dir, but auctex does not see it, it happens nothing,
what I'm doing wrong?
Hi Thorsten,

in your .emacs, set `TeX-style-private' to a directory of your choice,
e.g.

(setq TeX-style-private "~/.emacs.d/auctex")

and put moodle.el in that directory.

Also put this in your .emacs and restart Emacs.

(setq TeX-parse-self t)

Now restart Emacs and open your .tex file which contains
\usepackage{moodle} or do `C-c C-m usepackage RET moodle RET'. AUCTeX
should then load the style.

I've touched the code and it should now cover all macros and
environments provided by the package. Happy testing and thanks in
advance.

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

(defvar LaTeX-moodle-key-val-options
'(("points")
("default grade")
("penalty")
("fraction")
("feedback"))
"Key=value options for moodle macros and environments.")

(defun LaTeX-moodle-question-env-with-args (env)
"Insert ENV provided by moodle.sty incl. arguments and first \\item."
(LaTeX-insert-environment
env
(let ((opts (TeX-read-key-val
t
(cond (;; 3.3.1 Multiple Choice
(string= env "multi")
(append '(("shuffle" ("true" "false"))
("numbering" ("alph" "Alph" "arabic"
"roman" "Roman" "none"))
("single" ("true" "false"))
("multiple" ("true" "false")))
(when (string= "cloze" (LaTeX-current-environment))
'(("vertical" ("true" "false"))
("horizonal" ("true" "false"))))
LaTeX-moodle-key-val-options))
;; 3.3.3 Short Answer
((string= env "shortanswer")
(append '(("case sensitive" ("true" "false"))
("usecase" ("true" "false")))
(when (string= "cloze" (LaTeX-current-environment))
'(("vertical" ("true" "false"))
("horizonal" ("true" "false"))))
LaTeX-moodle-key-val-options))
;; 3.3.4 Essay Questions
((string= env "essay")
(append '(("response required" ("true" "false"))
("response format" ("html" "file"
"html+file"
"text" "monospaced"))
("response field lines")
("attachments allowed" ("0" "1" "2" "3"
"unlimited"))
("attachments required" ("0" "1" "2" "3"))
("response template"))
(when (string= "cloze" (LaTeX-current-environment))
'(("vertical" ("true" "false"))
("horizonal" ("true" "false"))))
LaTeX-moodle-key-val-options))
;; 3.4 Matching Questions
((string= env "matching")
(append '(("shuffle" ("true" "false"))
("drag and drop" ("true" "false"))
("dd" ("true" "false")))
LaTeX-moodle-key-val-options))
(t (append
(when (string= "cloze" (LaTeX-current-environment))
'(("vertical" ("true" "false"))
("horizonal" ("true" "false"))))
LaTeX-moodle-key-val-options)))))
(qname (unless (string= "cloze" (LaTeX-current-environment))
(TeX-read-string (TeX-argument-prompt nil nil "Question name")))))
(concat
(when (and opts (not (string= opts "")))
(format "[%s]" opts))
(when (and qname (not (string= qname "")))
(format "{%s}" qname)))))
(if (TeX-active-mark)
(progn
(LaTeX-find-matching-begin)
(end-of-line 1))
(end-of-line 0))
(delete-char 1)
(when (looking-at (concat "^[ \t]+$\\|"
"^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
(delete-region (point) (line-end-position)))
(delete-horizontal-space)
;; Deactivate the mark here in order to prevent `TeX-parse-macro'
;; from swapping point and mark and the \item ending up right after
;; \begin{...}.
(TeX-deactivate-mark)
;; Query and insert the question text.
(let ((qtext (TeX-read-string (TeX-argument-prompt nil nil "Question Text"))))
(when (and qtext (not (string= qtext "")))
(newline)
(indent-according-to-mode)
(insert qtext)
(LaTeX-fill-paragraph)))
(LaTeX-insert-item)
;; The inserted \item may have outdented the first line to the
;; right. Fill it, if appropriate.
(when (and (not (looking-at "$"))
(not (assoc env LaTeX-indent-environment-list))
(> (- (line-end-position) (line-beginning-position))
(current-fill-column)))
(LaTeX-fill-paragraph nil)))

(defun LaTeX-moodle-item-argument ()
"Insert an \\item with optional argument in environments of moodle package."
(let ((TeX-insert-macro-default-style 'mandatory-args-only))
(TeX-insert-macro "item"))
;; Add * to `LaTeX-moodle-key-val-options' in multi environment:
(let ((opts
(TeX-read-key-val t (if (string= "multi" (LaTeX-current-environment))
(append '(("*")) LaTeX-moodle-key-val-options)
LaTeX-moodle-key-val-options))))
(when (and opts (not (string= opts "")))
(delete-horizontal-space)
(if (string= opts "*")
(insert opts)
(insert LaTeX-optop opts LaTeX-optcl))))
(just-one-space)
(when (string= "matching" (LaTeX-current-environment))
(save-excursion
(insert TeX-esc "answer")
(just-one-space))))

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

(LaTeX-add-environments
;; 3.2 Quiz and Question Environments
'("quiz"
(lambda (environment)
(LaTeX-insert-environment
environment
(let ((opts (TeX-read-key-val t LaTeX-moodle-key-val-options))
(bank (TeX-read-string (TeX-argument-prompt nil nil "Question bank name"))))
(concat
(when (and opts (not (string= opts "")))
(format "[%s]" opts))
(format "{%s}" bank))))))

'("cloze" "Question bank name"))

(TeX-add-symbols
'("moodleset"
(TeX-arg-eval
(lambda ()
(let ((opts (TeX-read-key-val optional
(append '(("ppi")) LaTeX-moodle-key-val-options))))
(format "%s" opts)))))

;; 5 Graphics
'("ghostscriptcommand" "File name")
'("imagemagickcommand" "File name")
'("opensslcommand" "File name"))

;; Make environments available to AUCTeX:
(dolist (env '("multi" "numerical" "shortanswer" "essay" "matching"))
(LaTeX-add-environments `(,env LaTeX-moodle-question-env-with-args))
(add-to-list 'LaTeX-item-list `(,env . LaTeX-moodle-item-argument) t))

;; Fontification
(when (and (featurep 'font-latex)
(eq TeX-install-font-lock 'font-latex-setup))
(font-latex-add-keywords '(("moodleset" "{")
("ghostscriptcommand" "{")
("imagemagickcommand" "{")
("opensslcommand" "{"))
'function)
(font-latex-add-keywords '(("answer" "")
;; Cater for a fontified starred \item
("item" "*["))
'textual)))
LaTeX-dialect)

(defvar LaTeX-moodle-package-options
'("draft")
"Package options for the moodle package.")

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

Best, Arash

Loading...