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 02 '18 at 19:06
  • There is another recommendation for formatting source code: http://dustycloud.org/blog/vcs-friendly-patchable-document-line-wrapping/. There has already been a question regarding this style: https://vi.stackexchange.com/questions/6004/text-formatting-cvs-friendly. I have pointed out a possibility for automation https://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 02 '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? – Dr Krishnakumar Gopalakrishnan Apr 02 '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 06 '18 at 05:56
  • @Krishna I strongly recommend that you do not do any autoformatting. If you add let g:vimtex_format_enabled = 1, then you can get an improved formatter with gq for LaTeX and manually format paragraphs with e.g. gqip.

    Note: All of this is clearly very subjective, and even though I propose some personal opinions, you are of course allowed to do whatever you want! But I do think textwidth=80 (or smaller) is good advice, as well as what I've written about diff-friendly styles.

    – Karl Yngve Lervåg Apr 06 '18 at 05:58

1 Answers1

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.

Hotschke
  • 4,740
  • 26
  • 37