1

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?

  • A simple generalization would be to introduce a regular expression in your search, that is, modify the first line to something like (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 at 15:57
  • Just our of curiosity, why don't you just declare all the operators once and for all in the .tex document? I mean, why do want an emacs script to do it? – StarBug May 29 at 22:54
  • @StarBug I am a professionist typesetter and in my work I often have to correct math operators like classical groups from SL 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 at 9:38
  • I see, interesting. – StarBug May 30 at 12:27
1

Your Test 1 code has no iteration. It does one search and then terminates. Here is a modification with an iteration:

(defun test ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward (rx (group (or "\\GL" "\\SL" "\\SO"))) nil t nil)
      (setq gstring (match-string 1)) ;; this is group with backslash
      (setq gsubstring (substring gstring 1 nil)) ;; this is group without backslash
      (if (not (test-is-operator-declared gsubstring))
      (save-excursion
        (goto-char mypoint)
            (insert "\\DeclareMathOperator{" gstring "}{" gsubstring"}\n"))))))

(defun test-is-operator-declared (arg)
  (save-excursion
    (goto-char (point-min))
    (search-forward (concat "\\DeclareMathOperator{\\" arg "}") nil t nil)))

Note the save-excursion commands, which ensure that point is not modified in the iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.