User11111
User11111

Reputation: 23

Latex precompiling with Vim

I am looking for a way to pre-compile a text, i.e. reformat it following some custom rules, and then compile it with Tex in Vim.

For example I would like to reformat a text like this

THM   The sum $1+2+3  is equal to $$[six] 6.
% a comment line
PROOF   $$ 1+2 &= 3 \\ 1+2+3 &= 6

into this

\begin{theorem}
The sum $ 1+2+3 $ is equal to
\begin{align}
\label{six}
6.
\end{align}
\end{theorem}
\begin{proof}
\begin{align*}
1+2 &= 3\\
1+2+3 &= 6
\end{align*}
\end{proof}

and then run MikTex on the latter.

Is this obtainable all inside Vim?

Upvotes: 2

Views: 90

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

preprocessor

If you want to keep your custom syntax in the source file, and just generate the Latex syntax as an intermediate step to compilation with the Latex compiler, you have to insert a preprocessing step, e.g. using the C preprocessor, cpp. Sketch:

:setlocal makeprg=cpp\ -D\ THM='\\begin{theorem}'\ -\ \|\ miktex\ ...

(In reality, you probably would externalize the (many!) macro definitions in an include file instead of passing them on the command-line.)

snippets

If, on the other hand you just want to avoid typing all those long Latex definitions, but keep the Latex document as the source, you can use snippets to speed up the document creation and editing. snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

Upvotes: 1

Related Questions