This query is two questions both related to latex major mode (not Auctex), outline minor mode, code folding. I eventually solved these questions myself. My "answers are given below the ========== line.

Question 1:
I am using the following initialization so that each latex file has its body hidden (i.e. folded) when it is first opened. However, the lower level headings are still showing. How do I prevent the lower level headings from showing?

(add-hook 'latex-mode-hook
    (lambda ()
        (outline-minor-mode 1)
        (setq outline-regexp "%{{{{\\|%{{{\\|%{{\\|%{")

Question 2:
I want the following keybinding to be effective if and only if latex-major-mode is active (assumes that outline minor mode will also be active):

a. C-M-mouse-1 should toggle outline-show-entry and outline-hide-entry, depending on whether entry is currently hidden.

b. C-M-mouse-3 should invoke (outline-hide-sublevels 2).


Answer to Question 1:
The outline-hide-sublevels function was malfunctioning. Documentation indicated that I should code my own function and refer to it with the outline-level variable. I changed the tail end of my lamda clause to :

(setq outline-level 'my-latex-outline-level)
; (outline-hide-body)
(outline-hide-sublevels 2)))

The (default) latex-outline-level function is:

(defun latex-outline-level ()
    (if (looking-at latex-outline-regexp)
        (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1))

I could not get the match-string function to work correctly. So I chose the following kludgy but simple alternative (works for me, but not recommended for everyone):

(defvar my-latex-outline-regexp '"%{{{{\\|%{{{\\|%{{\\|%{")

(defun my-latex-outline-level ()
    "Personalized latex outline level.

Uses regexp that lists longest match strings first.  On match, 
returns length of matched string.  On no match, returns 1000."
    (if (looking-at my-latex-outline-regexp)
        (- (match-end 0) (match-beginning 0))

Answer to Question 2:
Actually, this solution is just routine research, so I should not have included the second question; I was just frustrated because of the first question. Looking at https://www.emacswiki.org/emacs?action=browse;oldid=KeyBindingDiscussion;id=ChoosingKeysToBind I found:

(eval-after-load 'text-mode
        '(define-key text-mode-map (kbd "'") 'maybe-open-apostrophe))

Unfortunately, I wasn't able to alter the above to work okay in latex, so I used the local-set-key command instead. My initialization was changed as follows:

;;; new function added

(defun my-toggle-outline-entry ()
    "Toggle outline folding entry, hidden or shown.

This code folding function uses the outline-invisible-p function to determine if
specific outline entry is hidden or shown.  Based on this determination, the 
visibility of the entry is toggled."

    (if (equal (outline-invisible-p) nil)
    ;; else

;;; local key bindings added

(add-hook 'latex-mode-hook
    (lambda ()
    (local-set-key (kbd "<C-M-mouse-1>") 'my-toggle-outline-entry)
    (local-set-key (kbd "<C-M-mouse-3>") 
            (lambda () (interactive) (outline-hide-sublevels 2)))

I think a warning should be added

After implementing these keyboard bindings, I twice lost all the data in an outline-minor-mode code folding entry. I suspect that the culprit was the mouse-save-then-kill function, which is globally bound to mouse-3. My resolution was to hijack mouse-3 within [major mode = latex, minor mode = outline] by changing the (outline-hide-sublevels 2) key binding away from C-M-mouse-3 to simply mouse-3.

  • 2
    Please do not post a solution in the original question. You can answer your own question with a post as an answer. Please edit you original question and put your answer in an answer post. I will remove my downvote after you make the edits.
    – Dan
    Mar 27, 2018 at 13:47
  • What @Dan said. Please edit the question to form a single, clear question. You can answer that question by posting an answer. You can post additional questions separately. As it stands now, the question is unclear, and risks being closed because of that. Please try to make the question simpler and more helpful. Thx.
    – Drew
    Mar 27, 2018 at 14:52
  • @Dan I wanted to post the answer separately, but could not. For some reason, the interface is not giving me the option to "answer" the questions, and I do not think it is a good idea to provide the info re either problem in a (compressed) comment.
    – zugzwang
    Mar 28, 2018 at 19:34
  • @Drew I agree, I was sloppy. However, see my comment to Dan. I think the 2 questions are pertinent and (arguably) related. I will edit, with "answers" below a ========... line. I request that following my edit, someone lift my solutions into two separate answers.
    – zugzwang
    Mar 28, 2018 at 19:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.