NB: much of this text came from an answer to a question that turned out to be a duplicate of this question. Some of the material is sufficiently different that it seemed worthwhile to add it to this question.
Outline mode and the AUCTeX folding facilities are two different beasts that are meant to do broadly the same thing: hide and show selected parts of the buffer.
A key element to recognize when using outline-minor-mode
is that it's got a different prefix key than outline-mode
(ie, the major mode) -- precisely to avoid key conflicts with motion keys, etc. That prefix defaults to the awkward-to-type C-c @
(although you can change that by customizing outline-minor-mode-prefix
). All the keybindings listed on this node of the manual should still work, but with the twist that you replace the major mode prefix of C-c
with the minor mode prefix of C-c @
. Hence, C-c @ C-c
runs hide-entry
, C-c @ C-e
runs show-entry
, and so on.
To use these facilities, you need to enable outline-minor-mode
. The simplest way is with a mode hook:
(add-hook 'LaTeX-mode-hook #'outline-minor-mode)
changing outline-minor-mode-prefix
It should be a simple thing to change outline-minor-mode-prefix
(it used to be back in the day). But it's not. What fun!
The docstring now gives a cryptic note that:
Prefix key to use for Outline commands in Outline minor mode.
The value of this variable is checked as part of loading Outline mode.
After that, changing the prefix key requires manipulating keymaps.
Yes, very helpful. Oddly enough, even customize
doesn't set this variable in time to avoid "manipulating keymaps" (despite it being defined with defcustom
in the source code). The practical work-around: set this variable in your init file before you load outline
or anything else built off of it (eg,org-mode
).
You might, for example, want the prefix to be C-o
(which means you're losing open-line
, but you can rebind that if you really want it). Putting the following early in your init file will do the trick:
(global-unset-key "\C-o")
(setq outline-minor-mode-prefix "\C-o")
Now, you should be able to access all of the outline mode functionality using the C-o
prefix -- C-o C-c
to call hide-entry
, C-o C-e
to call show-entry
, and so on.
adding new outline headings
As an added bonus, you can also customize which outline headers can get folded, and to what levels. I, for example, like to be able to make "fake" section headers to hide away text that doesn't have a true LaTeX header, but logically is kept at the same level of the document. With the following code, you can use:
%section{a fake section}
Blah blah blah blah blah, LaTeX doesn't recognize me as a section...
\section{a real section}
Blah blah blah blah blah, LaTeX treats me as a real section
And you will be able to manipulate the fake section just like any other one, but LaTeX will ignore it when you compile the document:
;; extra outline headers
(setq TeX-outline-extra
'(("%chapter" 1)
("%section" 2)
("%subsection" 3)
("%subsubsection" 4)
("%paragraph" 5)))
;; add font locking to the headers
(font-lock-add-keywords
'latex-mode
'(("^%\\(chapter\\|\\(sub\\|subsub\\)?section\\|paragraph\\)"
0 'font-lock-keyword-face t)
("^%chapter{\\(.*\\)}" 1 'font-latex-sectioning-1-face t)
("^%section{\\(.*\\)}" 1 'font-latex-sectioning-2-face t)
("^%subsection{\\(.*\\)}" 1 'font-latex-sectioning-3-face t)
("^%subsubsection{\\(.*\\)}" 1 'font-latex-sectioning-4-face t)
("^%paragraph{\\(.*\\)}" 1 'font-latex-sectioning-5-face t)))
outline-magic
extends the outline modes to use org-mode
-like hiding/showing with the magic tab key. In other words, you can probably add this in to your workflow and make your LaTeX documents behave more like org
documents. (I don't use it myself, so can't comment on functionality.)
AUCTeX's folding facilities work on environments and what they (confusingly) call macros, rather than sections. To use this functionality, you'll need to enable it with TeX-fold-mode
.
For example, the itemize
environment, unfolded, might look like:
\begin{itemize}
\item An item
\item Another item
\end{itemize}
Calling TeX-fold-env
(default: C-c C-o C-e
) inside the environment folds it up to look like:
[itemize]
Placing the cursor somewhere inside [itemize]
will temporarily expand it again while you edit it, and then will fold back down to [itemize]
when you move the cursor out of the environment.
By a "macro," AUCTeX means things like \footnote{...}
. Hence, calling TeX-fold-macro
(default: C-c C-o C-m
) when inside \footnote{This is a footnote}
will fold it up to [f]
; it will expand and contract in analogous fashion to what happens with environments.
You can use both outline-minor-mode
and AUCTeX's folding facilities together in the same document: they do different things on different parts of it.