3

Can AUCTeX be configured to fold the entire LaTeX preamble (i.e., hide everything before \begin{document})?

For example, it would be nice if the the following document:

\documentclass[letter,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{setspace} \doublespacing
\begin{document}
\section{Introduction}
Test document.
\end{document}

could be folded as:

[preamble]
\begin{document}
Introduction
Test document.
\end{document}

Is there a way to do this?

scaramouche
  • 1,772
  • 10
  • 24
  • I think it does that out of the box. Try hide-all. – Dan Jun 20 '17 at 03:21
  • Where's that function? The ones I have seen are `hs-hide-all` and `TeX-fold-buffer`. Neither of these seem to fold the preamble. – scaramouche Jun 20 '17 at 03:35
  • Sorry! That function does not exist; I wrote one a long time ago that does it, and forgot that it wasn't built in. You need to use `outline-minor-mode` to get the functionality. See my answer below. – Dan Jun 20 '17 at 03:45
  • FWIW, I tend to use narrowing to achieve similar effect. – YoungFrog Jun 23 '17 at 04:49

2 Answers2

1

You can use outline-minor-mode with AUCTeX to get folding along headlines. The following command should fold up all the headlines and do what you want with the preamble:

(defun my-fold-all ()
  "Fold to top level."
  (interactive)
  (while (condition-case nil
             (outline-up-heading 1)
           (error nil)))
  (outline-hide-sublevels 1)
  (unless (= (window-start) (point-min))
    (recenter)))

That function will fold EVERYTHING up. If you ONLY want to hide the preamble, you can use:

(defun hide-latex-preamble ()
  "Hide just the LaTeX preamble."
  (interactive)
  (save-restriction
    (save-excursion
      (save-match-data
        (widen)
        (goto-char (point-min))
        (when (re-search-forward "\\documentclass" nil t)
          (hide-subtree))))))

See also How can I hide/display LaTeX section just like org-mode does with headlines?

Dan
  • 32,584
  • 6
  • 98
  • 168
  • Many thanks, but this still folds the whole buffer on my computer. I recall having had something like this working at some point, so there must be a clash with something else on my init.el. In any case, it would be nicer if the preamble folding functionality was provided by TeX-fold-mode, as TeX-fold-mode has several advantages (e.g., auto expands when going inside the folded area and labels each folded area rather than showing "..."). – scaramouche Jun 20 '17 at 17:24
  • @scaramouche : odd! Even the preamble only function? – Dan Jun 20 '17 at 17:33
  • @scaramouche: I just tested your `hide-latex-preamble` macro. It works pretty well. However how can I unfold the preamble? – student Jul 12 '18 at 16:48
  • @scaramouche: Is it also possible to get a behaviour similar as in AucTeX's environment folding, i.e. displaying a keyword [preamble] as the OP suggested and if you move your cursor from the left or right into that keyword the preamble temporarily unfolds (just like in AucTeX environment fold). It should also highlighted with the same color as an AucTeX environment folded region. – student Jul 12 '18 at 16:51
  • Ok, I just figured out the answer to my first question: `outline-show-subtree` if point is over documentclass... – student Jul 12 '18 at 16:53
0

It is possible to hide the preamble in AUCTeX by using outline-minor-mode without introducing any new function.

Place the cursor on \documentclass{...} and execute M-x outline-hide-subtree (default keybinding: C-c C-@ C-d).

Use M-x outline-show-subtree (keybinding: C-c C-@ C-s) with the cursor on the folded preamble to unfold the preamble again

Note: C-c C-@ is the prefix in outline-minor-mode and is defined in the variable outline-minor-mode-prefix.

fjesser
  • 66
  • 4