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:
- Stackexchange Question
- Bibek’s gist
- My previous calculation template mode and first few versions of LiTeX
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.
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.
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.
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.
Using use-package you can do:
(use-package litex-mode
;; :load-path "/path/to/litex-mode/"
:commands litex-mode
:hook text-mode)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.
You can use local-set-key to bind individual functions to a key binding.
(local-set-key (kbd "×") 'litex-insert-or-replace-x)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)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))Formats the selection based on variable litex-format-string.
For example: 2.3434343 ⇒ 2.34 (when litex-format-string is .2f)
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.
Evals the last sexp and replaces it with the evaluation value.
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.
Converts valid lisp sexp to latex Expression:
For example: (+ 2 3 (* 6 x)) ⇒ 2 + 3 + 6 x
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
Increments the number.
some/url/to/chapter-2 ⇒ some/url/to/chapter-3
Converts exponential term to latex format.
1.23e-34 ⇒ 1.23 \times 10^{-34}
Encloses the selection in latex inline math.
1.23e-34 ⇒ \(1.23e-34\)
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}
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*}
There are lots of variables that define how each of these functions behave.
| Variable Name | Default Value | What it does |
|---|---|---|
| litex-latex-functions | ‘(sin cos tan) | Lisp functions that have their own latex commands. |
| litex-make-hyphenated-to-subscript | t | Whether to make the hyphenated variables subscript or not. |
| litex-latex-maybe-enclose? | nil | Enclose latex converted to paran if needed. |
| litex-format-float-string | ”%.3f” | Format string to be used by floats. |
| litex-format-float-upper-limit | 1e4 | Upper limit of what number is formatted as float. |
| litex-format-float-lower-limit | 1e-2 | Lower 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. |
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+,defunwere 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-evalif it has variables that are not yet defined.
