1

This is an orgtbl table

#+begin_src emacs-lisp :results none
(setq calc-language 'latex)
(calc-radians-mode)
#+end_src

#+attr_latex: :mode math 
|----------+-----+----------|
| Fonction | var | Dérivée  |
|----------+-----+----------|
| cos(x)   | x   | -\sin(x) |
| \cos(x)  | x   | #ERROR   |
|----------+-----+----------|
#+TBLFM: $3=deriv($1,$2);RL

I would like orgmode to display \cos(x) on the @2 line when I Input cos(x). If I input \cos(x) on the @3 line, the formula deriv($1,$2) gives an error. How can I obtain the correct LaTeX syntax for the input and the output ?

3

You can add the setting latex for calc-language to org-calc-default-modes.

The following example Org file shows how you can do that with file-local variables:


|----------+-----+----------+----------|
| Fonction | var | Dérivée  |          |
|----------+-----+----------+----------|
| cos(x)   | x   | -\sin{x} | -\cos{x} |
| \cos(x)  | x   | -\sin{x} | -\cos{x} |
|----------+-----+----------+----------|
#+TBLFM: $3=deriv($1,$2);RL


* Local Variables :noexport:

Local Variables:
eval: (plist-put org-calc-default-modes 'calc-language 'latex)
End:

You must reopen the file for the setting to take effect.


Alternatively, you can add the following code to your init file.

With that code you can add comma-separated Calc settings to the modes of a table formula. The first setting must be a mode as described in the Org manual. The subsequent settings have the form VARIABLE:VALUE where VARIABLE is the Calc variable such as calc-language and VALUE is the value for this variable such as latex. Below the code there is an example Org table with such setting.

(defun org+-top-level-split-string (equation &optional separators table)
  "Split string EQUATION at top-level SEPARTORS.
SEPARATORS is a regexp.
EQUATION can be an org table equation as used for `org-table-eval-formula'.
In that the default value [;,] for SEPARATORS is appropriate."
  (unless separators
    (setq separators "[;,]"))
  (unless table
    (setq table org-mode-syntax-table))
  (let (ret
    (pos 1))
    (with-temp-buffer
      (with-syntax-table table
    (insert equation)
    (goto-char (point-min))
    (while (null (eobp))
      (when (looking-at separators)
        (setq ret (cons (buffer-substring-no-properties pos (point))
                ret)
          pos (goto-char (match-end 0))))
      (forward-sexp))
    (when (> (point) pos)
      (setq ret (cons (buffer-substring-no-properties pos (point)) ret)))))
    (nreverse ret)))
;; Test: (org+-table-formula-mode-split "deriv($1,$2);RL,calc-language:latex")

(defvar org-calc-default-modes) ;; defined in org-table.el
(declare-function org-table-eval-formula "org-table")
(eval-when-compile
  (require 'subr-x))

(defun org+-table-eval-formula-mode (fun arg equation &rest rest)
  "Allow direct settings for `org-calc-default-modes'.
These settings are given in the formula as comma separated list.
Each setting has the form VARIABLE:VALUE where VARIABLE is one of
Calc's variable names and VALUE is the value in `read' syntax."
  (let* ((org-calc-default-modes (seq-copy org-calc-default-modes))
     (eq-flags (org+-top-level-split-string equation))
     (eq (car eq-flags))
     (modes (cadr eq-flags))
     (settings (cddr eq-flags)))
    (when (stringp modes)
      (setq eq (concat eq ";" modes)))
    (dolist (setting settings)
      (when-let ((pos (cl-position ?: setting))
         (var (intern-soft (substring setting 0 pos)))
         (val (read (substring setting (1+ pos)))))
    (setq org-calc-default-modes
          (nconc (list var val)
             org-calc-default-modes))))
    (apply fun arg eq rest)))

(advice-add 'org-table-eval-formula :around #'org+-table-eval-formula-mode)

The example org table:

|----------+-----+----------+----------|
| Fonction | var | Dérivée  |          |
|----------+-----+----------+----------|
| cos(x)   | x   | -\sin{x} | -\cos{x} |
| \cos(x)  | x   | -\sin{x} | -\cos{x} |
|----------+-----+----------+----------|
#+TBLFM: $3=deriv($1,$2);RL,calc-language:latex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.