[TxMt] Re: sudden latex tidy and latex compiling errors

René Schwaiger sanssecours at f-m.fm
Thu Feb 12 15:52:25 UTC 2015


Hi Robert,

> On 12 Feb 2015, at 1:28 , Robert Milton <robert.gomez.milton at gmail.com> wrote:
> 
> I’ve used `LaTeXTidy` regularly for some months now, and for me it is all good…with just one niggle:
> 
> A common idiom is
> \begin{environment} \label{env:whatever}
> 	...
> \end{environment}
> 
> except in captioned environments (table etc) when you usually want
> \begin{environment}
> 	...
> \label{env:whatever} \end{environment}
> 
> This idiom is reflected perfectly in the LaTeX bundle’s ctrl-< functionality, which I love so much I should probably get therapy.
> 
> `LaTeXTidy` has the unfortunate feature of moving the \label to its own line, so the result in any case is
> \begin{environment}
> 	...
> 	\label{env:whatever}
> 	...
> \end{environment}
> 
> My issue is that this cripples code folding. Before I run `LaTeXTidy` I can fold my LaTeX so I only see the environments and their \labels, which is exactly what I want. After `LaTeXTidy`, code folding hides the \labels, so all I see is \begin{environment}\end{environment}, which is probably not what anyone wants. I have Find&Replace workarounds, to put the \labels back on the lines I want them on, but it would be really neat if `LaTeXTidy` effectively left them there.

there is the option that we could change “LaTeX Tidy” so that it ignores lines containing labels. But it would take some time for me to do that. I poked around the script a little bit. Basically the script just uses a lot of regular expression substitutions to change the tex file. After I did not get something working after two hours, I just resorted to the same thing you mentioned. So I just used substitution to “fix” the changes by “LaTeX Tidy” that you do not like. For example:

The second example you mentioned was

    \begin{environment}
		...
    \label{env:whatever} \end{environment}

After we use “LaTeX Tidy” we get

    \begin{environment}
        \label{env:whatever}
    \end{environment}

We can fix that by using the regular expression 

    ^\s*(\\label{[^}]*})\s*(\\end\{[^}]+})

and the substitution `$1$2`. The same works in Perl too of course. So you could edit `latextidy.pl` and insert the following before the statement `print $string;` at the end of the file:

    $string =~ s/^\s*(\\label{[^}]*})\s*(\\end\{[^}]+})/$1$2/mg;

This way you should get the output you want. You can use the same pattern to fix the other things you do not like.

> The LaTeX bundle is a terrific piece of work in general, and although I use it every day this is the only minor grumble I can think of. And there is nothing else I would seriously suggest changing about `LaTeXTidy`.
> 
> Robert

Kind regards,
  René


More information about the textmate mailing list