/litex-mode

LiTeX mode for emacs; A minor mode to convert valid lisp expressions to LaTeX

Primary LanguageEmacs LispGNU General Public License v3.0GPL-3.0

https://melpa.org/packages/litex-mode-badge.svg https://stable.melpa.org/packages/litex-mode-badge.svg

LiTeX mode

This is a minor mode to convert valid elisp/lisp expressions to latex.

This is useful for emacs users because emacs allows lisp code to be directly evaluated inside any buffer, which means no need to have any code blocks. But since lisp expressions aren’t popular we can’t expect to put them in reports, so to overcome that I wrote this mode with the help of my friend Bibek Panthi.

The beginning of this mode, and previous codes are here:

For example if you look at this image you can see how just 1 lines of lisp expression can be converted to the final result of several lines of mathematical expression without even having to do any of the calculation.

./images/litex.png

Not only can it convert lisp expressions to latex, it can also, give intermediate solution steps. Perfect for doing homeworks (as that’s what I made it for) :P

Table of contents

How to Use

Here is a demo video describing the features of LiTeX mode.

For Text descriptions refer sections below for details on what each function does, and the meaning of custom variables.

You can also access it all from texinfo file provided with the package.

Installation

You can install it through melpa with M-x package-install litex-mode <ret>.

Or you can clone this github repo and load it using path.

Configuration section shows how you can add the load path using use-package.

Usage

Using use-package you can do:

(use-package litex-mode
  ;; :load-path "/path/to/litex-mode/"
  :commands litex-mode
  :hook text-mode)

Please look through the Keybindings, Functions, Variables, Known problems and the Tips and Tricks to get 100% out of this package.

Keybindings

By default LiTeX-mode doesn’t provide any keybindings, but it does have a variable containing bindings for the interactive functions that you can use.

Setting keybindings for individual functions

You can use local-set-key to bind individual functions to a key binding.

(local-set-key (kbd "×") 'litex-insert-or-replace-x)

Setting the whole litex provided keybindings to a prefix key

You can set a prefix key (C-e for me here) like in this example, which makes it so you can for example use litex-format-region-last using C-e f using the following in your config. In some cases you might have to unset key C-e because it’s used to goto end of line, I’m replacing that because I don’t use it (as End key does the same).

(local-set-key (kbd "C-e") litex-key-map)

Contents of litex-key-map are below.

(define-key litex-key-map (kbd "F") 'litex-format-region)
(define-key litex-key-map (kbd "f") 'litex-format-region-last)
(define-key litex-key-map (kbd "E") 'litex-eval-and-replace)
(define-key litex-key-map (kbd "e") 'litex-eval-and-insert)
(define-key litex-key-map (kbd "s") 'litex-sexp-to-latex-exp)
(define-key litex-key-map (kbd "S") 'litex-sexp-solve-all-steps)
(define-key litex-key-map (kbd "+") 'litex-increment-number)
(define-key litex-key-map (kbd "l") 'litex-exp-to-latex)
(define-key litex-key-map (kbd "m") 'litex-exp-in-latex-math)
(define-key litex-key-map (kbd "A") 'litex-sexp-solve-all-steps-equation)
(define-key litex-key-map (kbd "a") 'litex-sexp-solve-all-steps-eqnarray)

Complete setup using use-package

This is the complete setup using use-package, if you installed from melpa. If you installed by cloning the repo, uncomment and provide the load path.

(use-package litex-mode
  ;; :load-path "/path/to/litex-mode/"
  :commands litex-mode
  :hook text-mode
  :config
  (local-set-key (kbd "C-e") litex-key-map)
  (local-set-key (kbd "×") 'litex-insert-or-replace-x))

Known Problems:

elisp uses integer calculations so (/ 1 2) is evaluated to 0, be careful of such pitfalls. For now (/ 1.0 2) is evaluated as 0.5, so I’d recommend using floats when you need floats.

This problem doesn’t exist if you use slime integration. But slime will evaluate (/ 1 2) as 1/2 so expect that to be the final answer.

Tips and Tricks

Including Units

LiTeX mode now supports unit conversion and formatting using the [[https://github.com/Atreyagaurav/units-mode][units-mode]] which in turn uses gnu units.

The formatting is like below, the functions are from units-mode package.

  • (units-convert-simple (/ 1 2) "m" "ft") ⇒ \unit[\frac{1}{2}]{m}
  • (units-ignore 5 "ft") ⇒ \unit[5]{ft}

It also formats correctly on step by step solve, for example:

  • (setq L (units-convert-simple (+ 1 2) "m" "ft"))
    \begin{align*}
    L& = \unit[1 + 2]{m}\\
    & = \unit[3]{m}\\
    & = \unit[9.843]{ft}
    \end{align*}
        
  • (setq l (units-ignore 24 "in"))
\begin{align*}
l& = \unit[24]{in}
\end{align*}

Note that the =\unit= latex command is from =units= package, so you have to include it in your preamble.

If you’re using slime for evaluation, you need to load the units functions in slime too. The functions are available in units-mode’s github named clisp/units.lisp.

For sbcl you can put the contents in clisp/units.lisp to ~/.sbclrc so it’s evaluated in sbcl/slime startup.

Using case sensitive symbols

Inside emacs the symbols are read without case sensitivity, so if you define and variable names ABCD, it’ll replace the variable named abcd. To avoid that, specially if you have formula with both lowercase and uppercase symbols you can use this customization.

(setq readtable-case :preserve)

NOTE: Currently it only works for elisp, and not for slime integration, I’m searching for a solution with slime.

Slime integration

If you want to do the calculations in your favorite lisp dilect instead of doing it in elisp, or polluting the emacs environment with your variables, or mistakenly messing something up. You can start a slime process with slime and use that process to evaluate everything.

You Only need to set this configuration variable true:

(setq litex-use-slime-for-eval t)

Using LiTeX variables in your normal calculations

You can use the function litex-eval to evaluate expressions for you. If you don’t have slime integration then normal evaluation will work, but this will handle both cases for you.

For example:

(defun calc-Q (a b c)
  (litex-eval `(+ (* C-1 ,b) (* C-2 ,a) (* C-3 ,c))))

Here, the function calc-Q can be called from elisp, so you can put it in the org table formula, while internally it uses the variables C-1 to C-3 from your slime instance.

Using Greek letters

Someone who writes in LaTeX will definitely want to include greek letters, so you can use greek letters multiple ways in LiTeX.

By double escaping the backslash

You can use double escape to escape the backslash so you’ll get the variable correct. For example: (setq \\alpha 2)\alpha = 2

Using Unicode:

You can input unicode greek letters like α,β,γ…,Σ…,Ω, and they’ll be rendered fine by LaTeX. For example: (setq α 2)α = 2. Which is the default behavior.

If you want to use them to input, but still want to use LaTeX equivalent command then you can set litex-make-unicode-to-latex to true, that’ll convert the unicode to LaTeX command. For example: (setq α 2){\alpha} = 2.

(setq litex-make-unicode-to-latex t)

As for how to type unicode directly, you can use Compose key in Linux machines, and there is also TeX input method in emacs that lets you do that. If you type C-u C-\ TeX <RET> for TeX input method then when you type \alpha emacs will convert it into unicode α.

Using conversion from their names

By default you can use variables names like alpha without having it any effect, for example: (setq alpha 2)alpha = 2 but if you set the variable litex-make-name-to-latex-glyph true then you can just convert normal greek character’s names to LaTeX symbols. Like: (setq alpha 2){\alpha} = 2

(setq litex-make-name-to-latex-glyph t)

When you have more than one letters, in this use case you have to separate them with ., for example: (setq Delta.alpha 2){\Delta}{\alpha} = 2

This is so that the letters inside other words won’t be converted automatically. As you can see . is only used for initial separation of words so you can use it to make the bounds where you want greek letters: (setq Delta./.alpha 2){\Delta}/{\alpha} = 2

Explanation for functions

litex-format-region-last

Formats the selection based on variable litex-format-string.

For example: 2.34343432.34 (when litex-format-string is .2f)

litex-format-region

Same as litex-format-region-last but asks for the format, it also sets the litex-format-string variable.

NOTE: Doesn’t work well with multiple-cursors, so first use this once, then use the litex-format-region-last on the multiple cursors.

litex-eval-and-replace

Evals the last sexp and replaces it with the evaluation value.

litex-eval-and-insert

Evals the last sexp and inserts the evaluation value after that.

The value and sexp are separated by litex-steps-join-string which is “= ” by default.

litex-sexp-to-latex-exp

Converts valid lisp sexp to latex Expression:

For example: (+ 2 3 (* 6 x))2 + 3 + 6 x

litex-sexp-solve-all-steps

Solves lisp sexp steps by steps:

For example: (setq x 5)x = 5 then (setq y (+ 2 3 (* 6 x)))y = (+ 2 3 (* 6 x)) = (+ 2 3 (* 6 5)) = (+ 2 3 30) = 35

litex-increment-number

Increments the number.

some/url/to/chapter-2some/url/to/chapter-3

litex-exp-to-latex

Converts exponential term to latex format.

1.23e-341.23 \times 10^{-34}

litex-exp-in-latex-math

Encloses the selection in latex inline math.

1.23e-34\(1.23e-34\)

litex-sexp-solve-all-steps-equation

Same as litex-sexp-solve-all-steps but puts them in equation environment.

For example: (setq y (+ 2 3 (* 6 x)))

\begin{equation}
y= 2 + 3 + 6 x  = 2 + 3 + 6 \times 5  = 2 + 3 + 30 = 35
\end{equation}

litex-sexp-solve-all-steps-eqnarray

Same as litex-sexp-solve-all-steps but puts them in eqnarray* environment.

For example: (setq y (+ 2 3 (* 6 x)))

\begin{eqnarray*}
y &=& 2 + 3 + 6 x \\
 &=& 2 + 3 + 6 \times 5 \\
 &=& 2 + 3 + 30\\
 &=& 35
\end{eqnarray*}

Customization

There are lots of variables that define how each of these functions behave.

Variable NameDefault ValueWhat it does
litex-latex-functions‘(sin cos tan)Lisp functions that have their own latex commands.
litex-make-hyphenated-to-subscripttWhether to make the hyphenated variables subscript or not.
litex-latex-always-enclose?nilEnclose latex converted to paran all the time.
litex-format-float-string”%.3f”Format string to be used by floats.
litex-format-float-upper-limit1e4Upper limit of what number is formatted as float.
litex-format-float-lower-limit1e-2Lower limit of what number is formatted as float.
litex-format-float-trim-decimalnilTrim zeros after decimal if all decimals are zeros.
litex-steps-join-string”= ”String used for joining strings in steps of a solution.
litex-steps-end-string” ”String used at the end of each strings in steps of a solution.
litex-math-inline-start“\(”Opening syntax for math inline environment.
litex-math-inline-end“\)”Closing syntax for math inline environment.
litex-math-equation-start“\begin{equation}\n”Opening syntax for math equation environment.
litex-math-equation-end“\n\end{equation}\n”Closing syntax for math equation environment.
litex-math-steps-equation-join-string”= ”Value of `litex-steps-join-string’ to be used in equation environment.
litex-math-steps-equation-end-string” ”Value of `litex-steps-end-string’ to be used in equation environment.
litex-math-eqnarray-start“\begin{eqnarray*}\n”Opening syntax for math eqnarray environment.
litex-math-eqnarray-end“\n\end{eqnarray*}\n”Closing syntax for math eqnarray environment.
litex-math-steps-eqnarray-join-string” &=& ”Value of `litex-steps-join-string’ to be used in eqnarray environment.
litex-math-steps-eqnarray-end-string“\\\n”Value of `litex-steps-end-string’ to be used in eqnarray environment.
litex-math-align-start“\begin{align*}\n”Opening syntax for math align environment.
litex-math-align-end“\n\end{align*}\n”Closing syntax for math align environment.
litex-math-steps-align-join-string“& = ”Value of `litex-steps-join-string’ to be used in align environment.
litex-math-steps-align-end-string“\\\n”Value of `litex-steps-end-string’ to be used in align environment.
litex-make-unicode-to-latexnilConvert unicode to LaTeX equivalent (eg. α -> α)
litex-make-name-to-latex-glyphnilConvert variables with the same name as a glyph to a LaTeX glyph (eg. alpha -> α).
litex-use-slime-for-evalnilWhether to use slime process for evalulation or not. You need to start slime yourself.
litex-greek-unicode-latex-alistAlist of greek unicode symbols and their LaTeX counterparts.

Contributing

Since this package is new, I’d appreciate contributions on few things:

  • Finding bugs and reporting them in github issues.
  • There are many tests to be written for the functions.
  • Many functions that might have special syntax in LaTeX yet to be written. For example 1+, defun were added later (it only started with 4 operators), similar could be done for many more.
  • Fixing some glitches with the current functions.
  • Maybe some symbolic calculations using calc-eval if it has variables that are not yet defined.
  • I’m thinking of making an org babel implementation where it’ll generate the equations that we can include in latex export.