Folding braces in Emacs/AucTeX

This should work (requires AUCTeX and you first need to enable TeX-fold-mode with C-c C-o C-f or M-x TeX-fold-mode)

(defun mg-TeX-fold-brace ()
  "Hide the group in which point currently is located with \"{...}\"."
  (interactive)
  (let ((opening-brace (TeX-find-opening-brace))
    (closing-brace (TeX-find-closing-brace))
    priority ov)
    (if (and opening-brace closing-brace)
    (progn
      (setq priority (TeX-overlay-prioritize opening-brace closing-brace))
      (setq ov (make-overlay opening-brace closing-brace
                 (current-buffer) t nil))
      (overlay-put ov 'category 'TeX-fold)
      (overlay-put ov 'priority priority)
      (overlay-put ov 'evaporate t)
      (overlay-put ov 'TeX-fold-display-string-spec "{...}")
      (TeX-fold-hide-item ov))
      (message "No group found"))))

;; Bind the function to C-c C-o p
(eval-after-load "tex-fold"
  '(define-key TeX-fold-keymap "p" 'mg-TeX-fold-brace))

enter image description here

Point must be placed between braces, braces excluded. I've almost copy-pasted TeX-fold-make-overlay defined in tex-fold.el. You can call this function with M-x mg-TeX-fold-brace or bind it to your favorite key binding. I used C-c C-o p in the example, TeX-fold-mode automatically prefixes the defined key with C-c C-o.

To automatically unfold braces move point between them, instead to permanently show the braces use C-c C-o i or M-x TeX-fold-clearout-item.

Starting from this function it's possible to write a function for folding whatever you want. What you need is to find a way for searching the points where folding starts and ends. In this case I used the AUCTeX functions TeX-find-{opening,closing}-brace for finding the two braces. In the line

(overlay-put ov 'TeX-fold-display-string-spec "{...}")

you can set the string with which the folded region will be replaced.


Giordano solution works perfectly but the fold will be gone after the buffer killed. Each time you want to fold, you need to run the function again.

I have another solution to automatically fold everything you want. I use this function with comments. Example:

% Why I write this paragraph in latex \begin{fold}
% comment
% another comment
% \end{fold}

or You want to fold paragraph or other

% Paragraph that explains about A \begin{fold}
A is a letter. Another sentence. End of paragraph.
% \end{fold}

When nyou run this function. M-x latex-fold-foo It will fold everythig that \begin{fold} \end{fold}

The result like this

% Why I write this paragraph in latex [fold]
% Paragraph that explains about A [fold]

Each time you move your cursor to the folded paragraph, It will open it temporarily. If you want to remove the fold, use C-c C-o i

(defun latex-fold-foo ()
  (interactive)
    (save-excursion
      (goto-char (point-min))
      (while (search-forward (format "begin{fold}") nil t)
        (TeX-fold-env))))

My workflow is

  1. Open the buffer
  2. Run the function to automatically fold everything
  3. C-c C-o i to delete the fold if u want to edit it and see it permanently