The question title says it, pretty much.

How can I link together RefTeX's TOC functionality with outline-mode such that I can fold sections/subsections/etc.?

  • Does this help? – giordano Feb 18 '14 at 23:56
  • @giordano Not quite, it would seem. I tested out the baked-in functionality you linked to, but it would not fold \subsections under \sections, as I would have expected. (Am I confusing outline-mode with org-mode?) – Sean Allred Feb 19 '14 at 0:51
  • org-mode is something like outline-mode on steroids :-) How do you expect \subsections to be folded? – giordano Feb 19 '14 at 1:05
  • @giordano has hit the nail on the head -- i.e., we need the beginning and ending regexp of the region you want to fold. – lawlist Feb 19 '14 at 3:23
  • @giordano Consider \sections S and \subsections o (continuing the pattern down the hierarchy): SooSooooSoSooo could be folded into S...SooooSoSooo or S...S...SoS..., etc. To establish the pattern: CSSooSooSSoo -> CSS...S...SS... -> C.... (where each ... is an org-like continuation line) – Sean Allred Feb 21 '14 at 2:37

INITIAL DRAFT (February 19, 2014):   First rough draft.

EDIT (February 20, 2014):  Consolidated both functions into just one. Added a variable beg-flag-regexp and revised the logic of the function in relation thereto. The function now examines the beginning regexp to determine the matching ending regexp; and, it will work with the format of \begin{anything} and \end{anything} -- provided (of course) that anything is the same for both begin and end. The function now takes into consideration the possibility that there may be several occurrences on the same line matching the beg-flag-regexp -- e.g., \begin{minipage}\begin{singlespace*} -- i.e., selection is based upon proximity to the cursor position.

The following is a demonstration of how code folding works in Emacs. Code folding requires that there be a defined regexp for the beginning and ending of the folded region.

(defvar beg-flag-regexp (concat
  "\\)\\(\}\\)" )
"Regexp matching the beginning of the folded region.")

(defun toggle-block ()
"When FLAG is non-nil, hide the region.  Otherwise make it visible.  For this
function to work, the cursor must be on the same line as the beginning regexp."
  (require 'outline)
        ;; sweet-spot
        (looking-at beg-flag-regexp)
        ;; point could be between backslash and before the letter n
        (let ((line-begin (save-excursion (beginning-of-line 1) (point))))
            (re-search-backward "\\\\" line-begin t)
            (looking-at beg-flag-regexp)))
        ;; point could be to the right of \begin
        (let ((line-begin (save-excursion (beginning-of-line 1) (point))))
            (re-search-backward "\\\\begin" line-begin t)
            (looking-at beg-flag-regexp)))
        ;; point could be to the left of \begin
        (let ((line-end (save-excursion (end-of-line 1) (point))))
            (re-search-forward "\\\\begin" line-end t)
            (backward-char 6)
            (looking-at beg-flag-regexp))))
      (let* (
          (flag (not (get-char-property (match-end 0) 'invisible)))
          (beg (match-end 0))
          (base-flag-match (regexp-quote
            (buffer-substring-no-properties (match-beginning 2) (match-end 2))))
          (end-flag-match (concat "\\\\end\{" base-flag-match "\}"))
          (go-fish (concat "\\begin\{" base-flag-match "\}"))  )
          (if (re-search-forward end-flag-match nil t)
              (setq end (point))
              (outline-flag-region beg end flag)
                  (overlay-put (make-overlay beg end) 'display "\u25be"))
                  (mapc 'delete-overlay (overlays-in beg end)))))
            (user-error "Error locating an ending match for:  %s." go-fish)))
        (if (> (point) beg)
          (goto-char beg)) ))
      (message "Sorry, you are not on a line containing the beginning regexp."))))
  • This is certainly useful to get the job done, but how might I define Emacs-flavored regular expressions to match what needs to be matched? – Sean Allred Feb 21 '14 at 2:33
  • My understanding is that AUCTeX has a bunch of gizmos built in to do some folding; however, I have finally liberated myself completely from that package and never really learned it well. For my example above, I used the most common situation I could think of (e.g., begin/end anything). I essentially only do two types of documents -- letters and legal pleadings, so my LaTeX knowledge is somewhat limited. If you have an example of something that has a specific beginning and a specific ending, then I'd be happy to take a stab at it. I would need to see it laid out so that I can visualize it. – lawlist Feb 21 '14 at 3:11

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.