Code folding specific environments with TeXstudio?

Using TeXstudio, it is possible to fold sections of code. On the left hand side there appear small arrow markers which if you click will fold sections of code. Here is an example.

and after clicking the arrow next to \begin{figure}

My question is, is there a way to code fold ALL figure environments with one click. Say I have a document with 30 figure environments, I want to be able to code fold all of them with one click, rather than have to manually click this arrow on each line of \begin{figure}.

I have a feeling this may be possible with TeXstudio because it already has some functionality like this where you can collapse things with one click. This is on the 'View' dropdown menu. If I click 'Everything', then everything folds right up to \begin{document}. But I don't know what Level 1, 2, 3, 4 are? What I am looking for is a way to have 'Figure' here and I can click this and all the figures code fold. Is this possible?

You can achieve this with a user macro. Under Macros -> Edit Macros, add this:

%SCRIPT
var tl = editor.document().textLines();
var regEx = /\\begin{figure}/;
var match

for (var i=0;i<tl.length;i++){
match = regEx.exec(tl[i]);
if (match) {
editor.document().foldBlockAt(false,i+1)
}
}


Test document

\documentclass{report}

\begin{document} % Level 1
\chapter{A} % Level 2
Test text.
\begin{figure} % Level 3
\includegraphics{example-image}
\end{figure}

\section{B} % Level 3
Test text.

\subsection{C} % Level 4
Test text.
\begin{figure}
\includegraphics{example-image}
\end{figure}

\section{D}
Test text.
\begin{figure}
\includegraphics{example-image}
\end{figure}

\subsection{E}
Test text.

\chapter{F}
Test text.
\begin{figure}
\includegraphics{example-image}
\end{figure}

\section{G}
Test text.

\section{H}
Test text.

\subsection{I}
Test text.

\paragraph{J}
Test text.
\begin{figure}
\includegraphics{example-image}
\end{figure}

\chapter{K}
Test text.

\end{document}


Note that the \chapter, \section etc. titled A to K have not been collapsed (Test text in each block are still visible after executing the macro.

GIF

** If you want the expand figures macro as well, replace editor.document().foldBlockAt(false,i+1) in the above script with editor.document().expandParents(i+1), and save it as a separate macro.

As for the Level 1 to Level 4 blocks, I tried illustrating that with the test document above (see comments). Basically, it's not a fixed map of Level 1 -> Chapter, Level 2 -> Section etc.

• Level 1 is just the first block that TeXstudio encounters that can be collapsed that is not already a sub-block of another. In most cases, this is the \begin{document} command.
• Level 2 blocks are nested within Level 1 blocks, usually \chapter or \section depending on document class, but not nested within other blocks.

and so on.

But you can see from the first figure environment in my example, which is nested within the \chapter block, is considered to be the same level as the \section block because both of them are nested within \chapter.

• You wanted it to be added under the Collapse submenu, but since you can activate these macros much quicker via keyboard shortcut (Shift+F1, etc. by default), so I didn't see the necessity to do so in my answer. This is possible, in general, and I might come back to do this eventually, just for fun.
• Another possibility is to combine the collapse and expand into a single macro, and have it operate like the toggle comment shortcut (Ctrl+T), which should be fun to do as well.