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?

4
  • I think it does that out of the box. Try hide-all.
    – Dan
    Commented Jun 20, 2017 at 3: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. Commented Jun 20, 2017 at 3: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
    Commented Jun 20, 2017 at 3:45
  • FWIW, I tend to use narrowing to achieve similar effect.
    – YoungFrog
    Commented Jun 23, 2017 at 4:49

2 Answers 2

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?

5
  • 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 "..."). Commented Jun 20, 2017 at 17:24
  • @scaramouche : odd! Even the preamble only function?
    – Dan
    Commented Jun 20, 2017 at 17:33
  • @scaramouche: I just tested your hide-latex-preamble macro. It works pretty well. However how can I unfold the preamble?
    – student
    Commented Jul 12, 2018 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
    Commented Jul 12, 2018 at 16:51
  • Ok, I just figured out the answer to my first question: outline-show-subtree if point is over documentclass...
    – student
    Commented Jul 12, 2018 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.

Your Answer

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

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