I have a LaTeX source, call it main.tex
.
Suppose it contains one or more of the classical groups, like "GL", "SL", "SO", "U", "SU", "Sp", "USp" or the spin group "Spin".
I want to write a script that it inserts all the nedeed \DeclareMathOperator{}{}
macros in a specified point of main.tex
. That's to say, if main.tex
contains, for instance, "GL", "SU" and "Spin", script will print in a specified point of main.tex
\DeclareMathOperator{\GL}{GL}
\DeclareMathOperator{\SU}{SU}
\DeclareMathOperator{\Spin}{Spin}
Here there is my code for one group:
(when (search-forward "\\GL" nil t)
(setq gstring (match-string 0)) ;; this is group with backslash
(setq gsubstring (substring gstring 1 nil)) ;; this is group without backslash
(goto-char mypoint)
(if (search-forward "\\DeclareMathOperator{\\GL}{GL}" nil t)
nil
(progn
(insert "\\DeclareMathOperator{"
gstring
"}{"
gsubstring
"}"))
))
How can I generalize the above-mentioned code?
PS: I am not sure about title of my question. Any suggest?
Test 1
I have tried with StarBug suggestion. So:
(goto-char (point-min))
(setq mypoint (search-forward "\\begin{document}" nil t))
(defun test ()
(interactive)
(goto-char (point-min))
(when (re-search-forward (rx (group (or "\\GL" "\\SL" "\\SO"))))
(setq gstring (match-string 1)) ;; this is group with backslash
(setq gsubstring (substring gstring 1 nil)) ;; this is group without backslash
(goto-char mypoint)
(if (search-forward (concat
"\\DeclareMathOperator{"
gstring
"}{"
gsubstring
"}")
nil t)
nil
(progn
(goto-char mypoint)
(insert "\\DeclareMathOperator{"
gstring
"}{"
gsubstring
"}"))
))
)
I have tested this code on my main.tex
:
%% BOF
\begin{document}
\GL
\SL
\SO
%% EOF
The code works without errors, buy it prints exclusively \DeclareMathOperator{\GL}{GL}
. Do have I misunderstood?
(when (re-search-forward (rx (group (or "\\GL" "\\SL" "\\SO" "\\U")))) ... )
. Then use(match-string 1)
in the next line, and also modify the search for \DeclareMathOperator. Then you have one command to take care of all the groups. – StarBug May 29 '19 at 15:57SL
to\SL
or from{\rm SL}
to\SL
, for instance. In these cases authors do not insert corresponding\DeclareMathOperator{}{}
macros in their tex source, so I have to do it by myself. Hence, I prefer to do this with a script and not by hand :-) – Onner Irotsab May 30 '19 at 9:38