Automatically insert line breaks at 80 columns in LaTeX source code in VS Code

1st August 2024 by Genevieve Clifford

Quick technical blog post; I have been trying to work out how to automatically format LaTeX source to insert line breaks at 80 columns (I have an example of what I mean on a sourcehut paste, transforming source.tex into formatted.tex). I knew you could do this using Emacs and Vim, but didn’t know how to do this in VS Code (which is the editor I use to author LaTeX documents).

The way you do this is through modifying your latexindent config, and then passing the -m flag to the command to format in LaTeX Workshop1. Modify your VS Code user / project settings to have the following added:

"latex-workshop.latexindent.args": [
  "-c",
  "%DIR%/",
  "%TMPFILE%",
  "-y=defaultIndent: '%INDENT%'",
  "-m"
]

For the actual config, I have the following latexconfig file at ~/latexindent/textwrap.yaml (consult Chapter 6 of the latexindent documentation as there are many options and your use case may not match mine):

modifyLineBreaks:
  textWrapOptions:
    columns: 80
    multipleSpacesToSingle: 0
    comments:
      wrap: 1
      inheritLeadingSpace: 1
  blocksBeginWith:
    other: '\\blockquote'
  blocksFollow:
    headings: 0
    commentOnPreviousLine: 0

Finally, I added the path to this file at ~/.indentconfig.yaml:

paths:
- /home/gcli/latexindent/textwrap.yaml

Et voilà! Automatic formatting (well, on CTRL + A, CTRL + SHIFT + I) of LaTeX source to 80 columns!

Footnotes

  1. A very useful VS Code extension that helps you author LaTeX source