Auctex: Prompt for required arguments when Auctex inserts a theorem environment?

So I have defined a theorem environment/style in a separate structure.tex file, and I want to add this theorem environment to the set of Auctex environments when using C-c C-e. However, I also want Auctex to prompt me for two arguments, just as it does when I insert an equation or other such environments.

Here is a copy of my theorem environment.

\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{thmtools}
\usepackage{mathtools}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc}
\usepackage{xcolor}

\newenvironment{theo}[2][]{%
\refstepcounter{theo}
\ifstrempty{#1}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=theorem_color]
{\strut Theorem~\thetheo};}}
}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=theorem_color]
{\strut Theorem~\thetheo:~#1};}}%
}%
\mdfsetup{innertopmargin=10pt,linecolor=black,%
linewidth=0.5pt,topline=true,backgroundcolor=lightgray!25,%
frametitleaboveskip=\dimexpr-\ht\strutbox\relax
}
\begin{mdframed}[]\relax%
\label{#2}}{\end{mdframed}}


This theorem environment takes two arguments. The first is the title of the theorem, and the second is the label reference for the theorem. So the actual theorem should look like this when inserted into the document:

\begin{theo}[Title of my theorem]{thm:mytheorem}
...
\end{theo}


The documentation suggests doing something like the code below. But I don't follow elisp enough to know if I have to reference specific positions in the theorem arguments, or how I set up the brackets, since the arguments need to go into \begin{theo}[#1]{#2} format. And also the #2 arguments should ideally being with "thm:" for the label.

(TeX-add-style-hook
"theo"
(lambda ()
'("theo" "Title" "Label"))))


Unfortunately the document does not go into detail about how to customize a function like this. Does anyone have a sense of how to accomplish this goal?

UPDATE: One thing I noticed is that Auctex will prompt me for the single required argument of the title of the theorem. However, it will not prompt me for the second argument for the label. I am not sure if this is something that Auctex is getting from the definition of the theorem--in particular this line: \newenvironment{theo}[2][] above.

While it is possible to write a lisp function to achieve what you're looking for, I recommend you don't add the label as a mandatory argument to your environment and use \label{foo}. This makes file parsing a lot easier for AUCTeX and RefTeX (which I strongly suggest).

I suggest you put your definitions inside structure.sty (and not structure.tex) and do \usepackage{structure} in your .tex file. In your structure.el file, put this code:

(TeX-add-style-hook
"theo"
(lambda ()
;; Tell AUCTeX about the env and the prefix:

;; Tell RefTeX about the env and the prefix:
(when (boundp 'reftex-label-alist)
'("theo" ?m "thm:" "~\\ref{%s}" nil)))

;; Add the env to AUCTeX:
'("theo"
(lambda (environment)
(LaTeX-insert-environment
environment
(TeX-argument-prompt t nil "Title"))))
(when (and title (not (string= title "")))
(concat LaTeX-optop title LaTeX-optcl))))
(when (LaTeX-label environment 'environment)
(LaTeX-newline)
(indent-according-to-mode)))))))


Now when you hit C-c C-e theo RET, you'll be asked for an optional title and a label; with RefTeX, the label is inserted automatically like this:

\begin{theo}[Optional title]
\label{thm:1}
Cursor here
\end{theo}

• this is so very helpful. Just to clarify your recommendation, you suggested that I not make the label a mandatory argument. Do I need to make a change to the structure of the theorem environment? Like I would have to convert \newenvironment{theo}[2][]{ to \newenvironment{theo}[1][]{% and then remove the references to label in the environment. I have seen a similar structure used in the amsthm environment, or do you have any reference in mind? – krishnab Oct 4 '18 at 20:41
• @krishnab - Yes, you have to change your definition of theo to \newenvironment{theo}[1][]{%. Having the label inside a environment is standard behavior for most LaTeX env's. I don't have any special reference in my mind. – Arash Esbati Oct 4 '18 at 20:51
• @ArashEsbati What is the structure.el file? Do you just put in the same directory as the .tex files? How do you deploy it? – NVaughan Oct 4 '18 at 21:55
• @NVaughan - I suggest you open a new question; this is too much for a comment. I would answer more thoroughly then. – Arash Esbati Oct 11 '18 at 16:58