How to use outline-minor-mode to hide some exam class exercises in auctex

I use the exam document class in auctex to write exams for my students. I want to use the features provided by the outline-minor-mode to hide all the exercises I have finished and only show the one I am editing. In the exam class there is \question macro for writing exercises. here is an example :

\documentclass{exam}
\begin{document}
\begin{questions}
\question % exercise N°1
statement of exercise 1
\question % exercise N°2
statement of exercise 2
\question % exercise N° 3
statement of exercise 3
\end{questions}
\end{document}


I want for example to hide the statements of exercise 1 and 3 when I edit the statement of exercise 2! So I have add \question to the heading list of auctex with the variable TeX-outline-extra like this: M-x (setq TeX-outline-extra '(("\question" 1))) But \question is not recognized as a heading as expected!

• Too broad. Please say what you've done. Say what you expected to happen and what happened instead. Provide a step-by-step recipe to show the problem. – Drew Oct 15 '16 at 15:48
• I have use outline-magic-mode with (setq TeX-outline-extra '(("\question" 1))) but when I try outline-cycle nothing happens – Hafid Boukhoulda Oct 15 '16 at 16:18
• Untested: Does it help if use (add-to-list 'TeX-outline-extra '("[ \t]*\\\\question\\b" 1) t)? – Arash Esbati Oct 17 '16 at 7:50

You haven't properly escaped the regexp for "\question". It should be:

"\\\\question\\b"


This is a string, and in order to encode a single backslash in a string, it needs to be escaped with a backslash of its own. And in order to use a literal backslash in a regexp, it must be escaped with another backslash. So to get a regexp to match a single literal \ from a string, you need to use \\\\.

The \\b at the end of the string matches the end of a word, which is the regexp \b.

This will only work if \question appears at the beginning of the line. That's why @arashesbati added the [ \t]*, which makes the regexp match \question with any number of tabs or spaces before it on a line.

Setting the outline level to 1 seems a bit high, I think you might end up folding everything below the question into the question, including subsequent sections. So probably more useful to use something like:

("[ \t]*\\\\question\\b" 4)


Finally, it looks like AucTex only reads this at the beginning of a session, and subsequent changes to take effect until you restart. So the best way to make this addition is probably via M-x customize-variable TeX-outline-extra. That way, you know the variable will be changed before AucTex is loaded the next time you start Emacs. In my case, this variable is defined as:

'(("[   ]*\\\\question\\b" 4) ("[   ]*\\\\\$$bib\$$?item\\b" 7) ("\\\\bibliography\\b" 2))

• This answer helps very much. Thank you for the explanations! – Hafid Boukhoulda Oct 21 '16 at 11:04