1

I'm having some difficulty with the vim reindent files (with gg=G).

When I have a larger file (not that large, maybe less than 400 lines of code) I think Vim is having trouble to indent some lines correctly since the line on which the indention of the line afterwards depends is lots of lines above (I assume so, because I tried it with smaller blocks and then the indentation is done correctly).

Example:

\begin{itemize}
        \begin{minipage} %indent +2 (after \begin{itemize})
        \item %indent +1 (after \begin{minipage}) but -1 because it's \item
            %some lines %indent +1
        \end{minipage} %indent -1
\end{itemize} %indent -2 <--- here is the Problem, because here has to be -double indent

Now if in this case the lines at %some lines are lots of lines, then the \end{itemize} isn't shifted left by two indents (which would be correct) but by only one indent :/

Problem with this is that this messes the whole indention of all lines below.

The solution I'd like most, is if there would be something like the %stopzone comment for LaTeX to signal the syntax highlighting to stop the current (math)zone.

Maybe something like %indent -1 for move the line by one indent to the left.

Does anyone know how you would implement something like this, or even better, it something like this does already exist?

Or is there some other tool that can do this indentation better than Vim? It would be enough for me to get an approximate indentation from Vim and to use an external terminal utility to make the indentation really correct?

1
  • Or maybe there is a way to increase the buffer of vim for the indention (since with few lines this works) but the requested feature/external program would be much nicer, since this would even work on lines in which vim does the indention always false.
    – atticus
    Apr 30, 2020 at 21:54

1 Answer 1

0

The number of lines to scan to find the indentation of the corresponding \begin{...} is limited, but it can be controlled by the (unfortunately undocumented) global variable g:tex_max_scan_line, which defaults to 60.

See the variable definition in the indent/tex.vim shipped with the Vim runtime.

You can increase it to something more reasonable for your own LaTeX documents. For example, add this to your vimrc file:

let g:tex_max_scan_line = 400

This will increase the limit to 400 lines, which according to your post, should be enough. You will have a small performance hit from this change, but I'd expect it should be pretty acceptable.

3
  • 1
    Oh thanks, didn't know this variable. The performance loose could be minimized, by defining a shortcut which increases that variable, reindents the text and then resets the variable back to its previous value. That way only when reindenting the whole file, it will be a bit slower but while writing the performance should be the same. (In addition I asked the author to provide something like these comments to help the indention engine but I don't know if he'll do it)
    – atticus
    May 1, 2020 at 11:43
  • @atticus Don't worry about performance, really. Vim always stops searching as soon as it hits the matching \begin{...}, so this is really about stop searching when you have an invalid/Incomplete file that doesn't have one... But with even a somewhat weak computer I'm confident you can safely raise this into the thousands and still not notice any difference.
    – filbranden
    May 1, 2020 at 11:48
  • 1
    Oh ok, thanks for the explanation.
    – atticus
    May 1, 2020 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .