5

Consider we have a long paragraph in a .tex file that includes displayed-math formula. For example

This is a long paragraph that includes many sentences with a few math equations. 
For example, this is the Einstein mass-energy formula
\begin{equation}
E=mC^2,
\end{equation}
while this one is the Newton's second law:
\begin{equation}
F=ma.
\end{equation} 
We can add a few more sentences but I think it is in a good shape by now.

Using vim, I want to wrap the lines of the whole paragraph (not one line at a time) without wrapping the displayed math equations. Is it possible? I tried gqip but it wraps the equations, as well.

P.S.: I know that gqip does not wrap "citations". For example, if there is a

\cite{reference1,reference2,reference3,reference4,reference5,reference6}

in the paragraph then it will not be wrapped by gqip. I am actually looking for something like this.

2

1 Answer 1

2

latexindent.pl does a very good job at this.

You can configure it by setting :set formatprg=latexindent\ -m\ -l\ myWrapOptions.yaml\ - with

# myWrapOptions.yaml
modifyLineBreaks:
    textWrapOptions:
        columns: 40
noIndentBlock:
    mathEnvironment:
      begin: '\\begin\{equation\*?\}'
      end: '\\end\{equation\*?\}'

This is the output using your example with a slightly longer equation:

\documentclass{article}
\begin{document}
This is a long paragraph that includes
many sentences with a few math
equations. For example, this is the
Einstein mass-energy formula
\begin{equation}
E=mC^2,
\end{equation}
while this is the wave equation
\begin{equation}
    \nabla^2 \textbf{E} = \mu \epsilon \frac{\partial^2 \textbf{E}}{\partial t^2}
\end{equation}
We can add a few more sentences but I
think it is in a good shape by now.
\end{document}

For a more ad-hoc solution, latexindent won't touch blocks surrounded by % \begin{noindent} and % \end{noindent}. However, since you probably want to change the default wrapping options, this is probably unnecessary.

1
  • Interesting! Thanks for sharing :)
    – r_31415
    Jul 2, 2022 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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