| by Arround The Web | No comments

Guide to Set Up Vim for LaTeX Editing

Vim is an efficient and feature-rich text editor widely used by system administrators and developers. A key advantage of Vim is that it offers many tools to create, edit, and compile the LaTeX documents.

LaTeX is a typesetting method used to create professional documents and is preferred by scientists and researchers for documentation. This guide will explore editing the LaTeX documents in the Vim editor.

Before creating or editing the LaTeX, the Vim editor needs to be prepared for LaTeX editing because Vanilla Vim is not good at it. Though basic LaTeX editing can be done because TeX highlighting comes by default with Vim, it is recommended to have desired plugins installed to edit the LaTeX in Vim properly.

Prerequisites

To prepare Vim for LaTeX editing, we need to install the LaTeX editing plugin. There are two widely used LaTeX plugins for Vim:

Any LaTeX plugin can be used but it entirely depends upon personal preference. For this tutorial, I will be installing and setting up the Vimtex plugin for Vim.

The key feature of Vimtex is that it is stable, supports auto-completion, and provides many quick key mappings to create and edit LaTeX documents.

In Vim, there are multiple methods to install the plugins. However, having a plugin manager solves many complexities, so let’s install a plugin manager first for Vim.

1. Installing Vim Plugin Manager

There are multiple plugin managers for Vim editors; some of the best plugin managers are listed below:

I am going to install the vim-plug plugin manager for Vim as it is quick to set up and lightweight.

To install the vim-plug plugin manager, execute the command mentioned below:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

The plugin manager has been downloaded and set up; it’s time to install the plugin for LaTeX editing.

2. Installing Vim LaTeX Plugin

To enhance the LaTeX editing functionality on Vim, let’s install the Vimtex plugin.

To install the Vimtex plugin on Linux, see the instructions given below:

1. Open the VimAwesome webpage from here.

VimAwesome is an archive of Vim plugins; if you need any Vim plugins search here.

2. Search for Vimtex plugin and click on it to open its page:

3. Here you can see instructions for various plugin managers. As we have installed VimPlug, we will click on it.

Copy the plugin code mentioned as shown in the image below:

This code will be used to paste in the vimrc file. See the next step:

4. Open the vimrc file:

sudo vim /etc/vim/vimrc

It is recommended to create a user-specific vimrc file instead of modifying the system files. To create a user-specific vimrc file, use the following command:

vim ~/.vimrc

5. Insert the below-given lines at the end of the file:

call plug#begin()

<..List of Plugins..>

call plug#end()

The call plug#begin() and call plug#end() are the container tags that contain the list of plugins that you can install.

Paste the plugin code in between the container tags:

call plug#begin()

Plug 'lervag/vimtex'

call plug#end()

After inserting the plugin code, save and quit the file using SHIFT+zz or type :wq in the NORMAL mode. The setup for the plugin is complete and it is now ready for installation.

6. Open the Vim editor and type :PlugInstall command in the NORMAL mode:

:PlugInstall

The above command will install the plugins, as you can see in the following image:

7. Now, quit the Vim editor by typing :quit command and relaunch it.

3. Installing LaTeX Compilation Program

After installing the Vimtex and vim-plug plugin manager you need another key program to compile the LaTeX documents into PDF. There are two main compilation utilities for Linux:

  • latexmk
  • pdflatex

I am installing latexmk utility in the Linux system using the command given below:

sudo apt install latexmk

4. Verifying the Vim for LaTeX Setup

To verify that Vimtex is ready for LaTeX document editing, open any LaTeX file.

vim [filename].tex

Get into the NORMAL mode and type :VimtexInfo command.

:VimtexInfo

The VimtexInfo command gives information about, the system, Vimtex project, compiler, file path, and other related info.

If the command gives an output then the Vimtex is properly installed and functions. However, if you get a Not an editor command error, then you will need to recheck all the prerequisites.

Creating a LaTeX Document in Vim

To create a LaTeX document just use the vim command with the file name and keep the extension .tex.

vim [filename].tex

For example, I am creating a .tex file and naming it main.

vim main.tex

Type the content of the LaTeX file.

\documentclass{article}

\begin{document}

This is my sample LaTeX document.

\end{document}

\documentclass: There are a variety of document classes such as CV/resume, book, and report.

\begin{document} & \end{document}: Tags to include the content of the document.

This LaTeX will only display the text. Let’s add some more environments:

\documentclass{article}

\title{My LaTeX Document}

\author{Sam}

% Inserting today’s date

\date{\today}

% Starting document

\begin{document}

This is my sample LaTeX document.

% Creating a list

\begin{itemize}

\item Sample Item 1

\item Sample Item 2

\item Sample Item 3

\end{itemize}

$ E=mc^2 $

% Creating a mathematical equation

\begin{math}

F =\frac {mv^2}{r}

\end{math}

\end{document}

In the above LaTeX document, I have added, the title, author name, and date. In the main document, I have created a list of three items and an equation. In LaTeX, comments can be added using the percentage sign % followed by a comment.

Editing the LaTeX Document in Vim

This is where Vimtex is going to help; Vimtex provides various commands to quickly edit the LaTeX file. Let’s explore Vimtex commands to edit the LaTeX document in the Vim editor:

Deleting Surrounding Environments

Use the dse command in the NORMAL mode:

dse

Bring the cursor over the surrounding environments that you want to delete and type the command:

Changing Surrounding Environments

Use the cse command in the NORMAL mode:

cse

Bring the cursor over the surrounding environments and type the command, you will be prompted to type the new surrounding:

Deleting Surrounding Command

Use dsc command in NORMAL mode:

dsc

Put the cursor over the surrounding environments and type the command, the LaTeX command will be deleted and only arguments will be left.

Deleting Surrounding Delimiter

Use dsd command in NORMAL mode:

dsd

Put the cursor over delimiter such as round brackets (), square brackets [], \left \right or \big \big and type the command:

Changing the Surrounding Delimiter

Use csd command in NORMAL mode:

csd

Bring the cursor over the delimiter and type the csd command, then write the new delimiter and hit return.

Deleting Math Surrounding

Use ds$ command in NORMAL mode:

ds$

Put the cursor over math’s surroundings, type the command; surroundings will be deleted:

And:

Changing Math Surrounding

Use cs$ command in NORMAL mode:

cs$

Put the cursor over the surroundings and execute the command, write the new surroundings, and hit Enter.

Toggling Starred Command

Use the tse and tsc command to toggle the starred commands:

tse

This command will insert the asterisk to the LaTeX command.

Next, for tsc:

tsc

This command will insert an asterisk to the arguments of the surroundings.

Toggling Math Between Inline and Display

Use ts$ in the NORMAL mode:

ts$

Toggling Delimiter

Use the tsd command in the NORMAL mode for toggling the delimiter:

tsd

Toggling Fraction

Use the tsf command to toggle the fraction:

tsf

Navigational Commands

Vimtex offers various navigation keymaps as well:

% To navigate between delimiters.
]] and [[ To navigate between sections and subsections.
]m and [m To navigated between environments.
]n and [n To navigate between math zones.

To learn about all the Vimtex key-mapping execute the :h vimtex-default-keymaps command in the Vim editor.

:h vimtex-default-keymappings

Compiling and Exporting the LaTeX Document File

The last step is compiling the LaTeX document in PDF format. In Linux LaTeX documents can be compiled using several methods. In the prerequisites section, we have installed the latexmk utility which is used by Vimtex to compile the document.

To compile and live view the document, use the VimtexCompile command in the NORMAL mode:

VimtexCompile

A window will open showing the live compilation of the document. To stop the live preview of the document, execute the same command.

There is a key-binding as well to quickly compile and live-preview the LaTeX document that is \ll: it will start and stop the compilation.

To view the PDF file use:

VimtexView

The LaTeX file can also be compiled separately using the latexmk utility.

latexmk -pvc -pdf [filename].tex

The -pvc flag is for live view the -pdf indicates the export file format.

Other Helpful Plugins to Edit the LaTeX in Vim

Various Vim plugins can be incorporated with the Vimtex to enhance productivity. A list of helpful plugins is:

These plugins are handy for autocompletion in the Vim editor and can be installed by following the method in the above sections of the guide.

Conclusion

LaTex is a typesetting system widely used by scientists and researchers for documentation making. It is a bit complex but it is better for large projects such as books. Moreover, it is free to use, you don’t need to pay for a word processor like MS Word.

LaTeX and Vim combination is a handy way to create, edit, and compile LaTeX documents quickly. Although Vim provides basic functionality to edit the LaTeX document; it is not as good as other GUI-based editors. LaTeX documents can be edited using the Vimtex plugin which provides a wide range of tools that can be used to quickly create, edit, and compile the LaTeX documents.

Share Button

Source: linuxhint.com

Leave a Reply