[TxMt] LaTeX Tidy and code folding with \labels

Robert Milton robert.gomez.milton at gmail.com
Tue Nov 29 11:05:26 UTC 2016


Hi guys,
I guess this is primarily of interest to Rene Schwaiger. A long time ago we discussed LaTeX Tidy, \labels and code folding. I love everything about LaTex Tidy except for the fact that it puts \labels on a separate line to \begin or \end, so when I use code folding all I see is a list of \begins, \ends, \sections and the like, without a \label in sight. Rene pointed me to latextidy.pl, which was the help I needed, and I hacked the file so the code now ends

	$string =~ s/({[^}]*}[ \t]*)(\n[ \t]*)((\[[^\]]*\][ \t]*)*(\\label[ \t]*(\[[^\]]*\][ \t]*)*{[^}]*}[ \t]*)*)\n?[ \t]*/$1$3$2/mg;
	$string =~ s/((\\label[ \t]*(\[[^\]]*\][ \t]*)*{[^}]*}[ \t]*)+)(\n[ \t]*)(\\end{[^}]*})/$4$1$5/mg;
	$string =~ s/(\\section.*(\\label[ \t]*(\[[^\]]*\][ \t]*)*{[^}]*}[ \t]*))(\S)/$1\n$4/mg;
	$string =~ s/(?<=\S)[ \t]*(?:\n[ \t]*){0,2}(\\section)/\n\n\n$1/mg;
	$string =~ s/(?<=\S)[ \t]*(?:\n[ \t]*){0,1}(\\subsection)/\n\n$1/mg;

	print $string;

I’ve been using this for a while now and it has proved fairly reliable. I’ve allowed for optional arguments on \labels, labels pinned to \end (for captioned environments), and insert blank lines before \sections and \subsections. Now when I code fold I see not just the environments but their \labels, which mean something to me.

This message is intended in the spirit of giving back. Please feel free to ignore this hack, or try it out, change it, whatever.

Regards
Robert Milton


More information about the textmate mailing list