# How to code-fold in TeX-Studio or LaTeX in general?

I want to fold the whole part before \begin{document} where it is just formatting the LaTeX document. Is there a way to do this in LaTeX, in general, or at least in TeXstudio? Could I use some \begin{} structure to do it?

• Although code folding is possible in TeXStudio tex.stackexchange.com/a/5281/15717, I don't think it's wise to do it. May be add the preamble (packages and custom macros) as package before \begin{document} follow tex.stackexchange.com/questions/40760/… Jan 28 '15 at 3:38
• Cut all your preamble and save as preamble.tex, then type \input{preamble} before of \begin{document}.
– Fran
Jan 28 '15 at 7:06
• In general, you code-fold by editing your LaTeX code with a text editor that supports code folding. My choice of editor is Vim. It also supports syntax highlighting for many languages including LaTeX. Jan 28 '15 at 12:41
• Why not split your code in many files and then include them within a single main file? You can define the main file as Master document and when you compile any open file, the main file will be compiled. Jan 28 '15 at 12:45

Note, code folding is a feature of an editor not a language. So there cannot be in 'code folding in LaTeX in general'.

Coming back to your intention, TeXstudio does not directly support 'folding everything before \begin{document}'. In TXS folding is limited to environments and structure commands (\section, etc.). However, you can define arbitrary folding ranges using the comments

%BEGIN_FOLD

%END_FOLD


That said, you may still consider separating the preamble to another file and \input-ing it as others have proposed in the comments. Personally, I favor this over folding, particularly in large projects that constist of multiple files anyway. While TXS can persistently save foling information in a session, the folding state is gone if you switch to another editor or pass the code to someone else. And then you would have to fold all the stuff again.

• Is it possible to auto-fold certain environments, e.g. everything between \begin{figure} and \end{figure}, meaning they will be collapsed unless manually extended?
– user61658
Oct 24 '17 at 9:58
• No, this is not possible. Oct 24 '17 at 20:19
• It would certainly be a nice feature.
– user61658
Oct 25 '17 at 7:59