2

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?

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

  • 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

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.