# TeX/LaTeX vim fold markers options

In complex files, especially when writing packages with basic TeX-LaTeX code, the standard definition of foldmarker, namely {{{,}}}, is unfortunate — in effect is quite common to have pieces of code like:

\pgfkeys{/tikz/t/.add code={}{\ctikzset{text=#1}}}


which will trigger the fold closing mechanism. What I am doing is adding on the top of my files the following:

% vim: set fdm=marker fmr=%<<<,%>>>:


because I think that this will make the folders independent of the settings of other contributors.

Is this a sensible way of doing folding with TeX? Is there any commonly used setting of foldmarker for TeX/LaTeX I should be aware of, and use it instead of inventing my own?

## 1 Answer

You might want to consider adopting the vimtex plug-in, which improves support for TeX files in Vim.

Among its features, it supports custom folding, which is able to recognize LaTeX blocks delimited with \begin{...} and \end{...} and use them to define folding.

Furthermore, it recognizes {{{ and }}} but only within comments.

See vimtex's documentation under :help vimtex-folding for more details, in particular :help g:vimtex_fold_types which allows you to customize it.

If you decide to use foldmethod=marker with the custom <<< and >>> markers, then instead of fmr=%<<<,%>>>, consider using:

fmr=<<<,>>>


Without the leading % comment character.

You're of course going to use those inside comments, so in practice you'll end up using %<<< often. However, matching only <<< as the marker has advantages, since then you could use additional text at the start of the comment, for example describing the block:

% Introduction <<<


And the other advantage is that you can precede the marker with a level number, in which case you can for the most part skip the closing markers, if you use matching levels on them:

% Introduction 1<<<
...
% Chapter 1: Motivation 1<<<


The choice of <<< and >>> is such that this sequence is very unlikely to happen in the normal body of the text, so forcing them to appear at the start of the comment, with %<<<, isn't totally necessary.

(Though note that using %<<< still allows using them this way, for example you could have %<<< or even 1%<<< inside a comment.)

• Yes, I use vimtex --- the automatic folding is great for LaTeX files, but for low-level TeX files like the ones in the circuitikz core (the package I maintain) automatic folding is at best not useful, and it is quite slow. Jun 28 '20 at 17:16
• Note that vimtex can handle {{{ and }}} within comments, so using %{{{ etc. might be helpful. See also the reference to g:vimtex_fold_types which allow you to customize automatic folding. Having said that, perhaps fdm=marker works best for you, so since you can easily set that up from a modeline, maybe that's the best choice for you! Jun 28 '20 at 17:21
• I've read the vimtex_fold_types and especially the markers option, and hat would be great, matching the pattern only inside comments solves my problem. But problem is that a collaborator that does not use vimtex will have the folding completely wrong. So yes, this is the solution, even if I can't use it on some file... Jun 28 '20 at 17:38
• @Rmano Updated the answer with a further recommendation on using foldmarkers inside comments. Jun 28 '20 at 18:44
• I think that the syntax for the level number is after the marker, like %>>>1... Jun 29 '20 at 8:55