X9hRRDys / latex-labeler

Simplify equation labeling in LaTeX documents using Emacs.
GNU General Public License v3.0
2 stars 1 forks source link
emacs emacs-lisp latex

LaTeX Labeler - Simplify equation labeling in LaTeX

LaTeX Labeler is an Emacs Lisp package designed to streamline the process of labeling equations in LaTeX documents. This package enables numbering equations sequentially in a LaTeX file from top to bottom. The main purpose of LaTeX Labeler is to synchronize equation labels in a LaTeX file and a compiled document. You can avoid the hassle of manually managing label naming. This is a simple yet powerful solution that saves you time and eliminates labeling confusion.

Note: We would like to emphasize that this package is inspired by texlabel.el, created by tama.sh. While LaTeX Labeler offers similar functionality to texlabel.el, it includes several enhanced and improved features.

Features

Important note

While LaTeX Labeler works seamlessly with AUCTeX and YaTeX, it may encounter unexpected errors when used with the built-in latex-mode, especially when labeling equations within subequations environment. We recommend using LaTeX Labeler with AUCTeX or YaTeX.

Getting started

  1. LaTeX Labeler is available on MELPA:

    M-x package-install RET latex-labeler RET.

  2. If you use AUCTeX, add the following code to your Emacs configuration file (e.g. ~/.emacs.d/init.el):

    (add-hook 'LaTeX-mode-hook #'latex-labeler-mode)

    Additionaly, it is recommended to add the following code. This code suppresses the prompt for inserting an equation label when executing the LaTeX-environment command (C-c C-e):

    (setq LaTeX-equation-label nil)

    If you use YaTeX instead of AUCTeX, add the following code:

    (add-hook 'yatex-mode-hook #'latex-labeler-mode)

Usage

LaTeX Labeler provides three commands for labeling equations and updating references.

Example

  1. Assume you have a LaTeX file shown as follows. The default label prefix is set to eq.

  2. Use M-x latex-labeler-update command to update labels that match the \"eq: + number\" format. In the place where there are no labels, new labels are inserted in the \"eq: + number\" format. This will make the labels in your LaTeX file match the equation numbers in the PDF. Additionally, a reference using eqref is updated.

  3. Use M-x latex-labeler-update-force command to update all labels and references, regardless of the format.

  4. If you want to change the label prefix, use M-x latex-labeler-change-prefix-and-update command. You are prompted to enter a new prefix in the minibuffer. The labels are updated with the new prefix, and a local variable setting is added to the file.

Label format with section numbers

When working with a LaTeX file that includes sections, you can include the section number in equation numbering in the compiled document. This can be achieved by adding the following lines to the LaTeX preamble:

\numberwithin{equation}{section}

For LaTeX files edited in Emacs, you can also append section numbers to equation labels. To enable this feature, set the custom variable latex-labeler-with-section-counter to t. If you want to set it for a particular file, add the following lines at the end of the LaTeX file:

% local variables:
% latex-labeler-with-section-counter: t
% end:

If you use AUCTeX and want to apply this configuration to all LaTeX files within a particular directory, create a file named .dir-locals.el in that directory and add the following lines in .dir-locals.el (alternatively, you can achieve this by running the command M-x add-dir-local-variables):

;;; Directory Local Variables            -*- no-byte-compile: t -*-
;;; For more information see (info "(emacs) Directory Variables")

((latex-mode . ((latex-labeler-with-section-counter . t))))

If you prefer to set this configuration globally, add the following line to your Emacs configuration file:

(setq latex-labeler-with-section-counter t)

After the \appendix command, appropriate letters are added as section counters instead of numbers.

Note for multi-file documents

Please note that LaTeX Labeler updates labels and references only within the currently edited file. It does not affect labels and references in other files included using \input{...} or \include{...}. If you work with a large document spanning multiple files, we recommend the following workflow:

  1. Divide your LaTeX document into files at units where equation labels are reset, such as chapters or sections.

  2. Use latex-labeler-change-prefix-and-update to set a label prefix for each individual file.

  3. For equations that are referenced in other files, assign unique labels that do not follow the prefix + number format. Use latex-labeler-update to update labels within the file.

  4. If you need to modify label names that do not follow the prefix + number format across multiple files, you can use built-in commands in Emacs. For instance, when managing LaTeX files as a project, you can use project-query-replace-regexp, which is bound to C-x p r.

Examples of customization

Custom variables