1

I am trying to write my PhD thesis in vim using LaTeX

So the documents will have two distinct types of content - markups that are relatively short, and paragraph text that is rather long.

\begin{some environment}
 Long content here (Lorem  ipsum dolor ......
.........
\end{end enviroment}

Since the drafts will involve a lot of edits, in order to keep my writing comprehensible for the text, I had set an autocmd to set formatoptions=at and textwidth=110 based on filetype=tex so that vim shall insert linebreaks appropriately and reformat documents on the fly upon delete/edit cycles.

The problem is that these reformats happen for the markup text also. For example, if I edit just the markup, the autoformatting kicks in, realigning linebreaks to satisfy the textwidth setting. Similarly, comment lines are also mangled up by the on-the-fly reformatting.

How do I set up the formatoptions in my vimrc in such a way that these autocommands fire only for long paragraphs and leaves short texts and comments unchanged.

  • You might like to take a look at this answer which solves the same problem but in Markdown files. – Rich Mar 30 '18 at 18:28
  • I think the answer by Hotschke should suffice, but I would also like to add that you should reconsider hardwrapping in general. When working with LaTeX documents, it is much easier to see the difference between different versions of the manuscript if you instead use either a sentence or a paragraph per line and allow long lines. In this case, you should keep set textwidth=0 and set wrap. If you insist on hardwrapping, I would recommend to use textwidth=80 (or smaller) to make sure that the content fits in standard terminals. – Karl Yngve Lervåg Apr 2 '18 at 19:06
  • There is another recommendation for formatting source code: dustycloud.org/blog/…. There has already been a question regarding this style: vi.stackexchange.com/questions/6004/…. I have pointed out a possibility for automation vi.stackexchange.com/a/14227/1292. However, I do it more or less manually. The easier style is using softwrapping. I am not sure whether Karl is necessarily right that diffs are easier to read e.g. git diff in the terminal for a long paragraph. – Hotschke Apr 2 '18 at 19:31
  • @KarlYngveLervåg, In normal mode, I tend to use par to wrap only long lines using gq. But this is a manual process. Can this be triggered in insert mode automatically with vimtex? – Krishna Apr 2 '18 at 19:37
  • 1
    @Hotschke I agree that style is more pleasant, but I've found the "one line per sentence" to work quite well with wrapping in a normal Vim window. And more importantly, it solves the issue of diffing various versions, and it is easy to get coworkers to use the same style. I think the latter point is the most difficult part about the "improved" style. – Karl Yngve Lervåg Apr 6 '18 at 5:56
2

latexindent.pl

I cannot answer your question regarding setting formatoptions and textwidth differently for different parts of the same file.

However, I would recommend to use a dedicated tool to format LaTeX code such as latexindent.pl.

This tool has to be configured via a config.yaml file, e.g.

modifyLineBreaks:
    textWrapOptions:
       columns: 110

Invocation on commandline would be:

$ latexindent.pl -m input.tex -o output.tex -l config.yaml

You can also call this on the current file within vim with

:!latexindent.pl -w -m % -l config.yaml.

latexindent.pl can also read from STDIN and output to STDOUT and therefore can be used as a filter in vim.

You could also assign this command to formatprg.

See the vim plugin https://github.com/sbdchd/neoformat for a setup of latexindent.pl.

vimtex

An alternative to latexindent.pl is the vim plugin vimtex which offers format capabilities by a custom formatexpr. This is not enabled by default:

*g:vimtex_format_enabled*
  If enabled, |vimtex| uses a custom |formatexpr| that should handle inline
  comments. That is, if it is enabled, comments at end of lines will not be
  joined with the |gq| command.

  Default value: 0

If you need to customize how your code is formatted, latexindent.pl offers more possibilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.