Skip to content

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

License

Notifications You must be signed in to change notification settings

Bu156/litex-mode

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

NOTE: 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, and I’m working on optional integration with slime which doesn’t have this problem.

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)

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))

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-maybe-enclose?nilEnclose latex converted to paran if needed.
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-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.

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Emacs Lisp 100.0%