[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [patch] ox-latex.el: add `:options' LaTeX attribute to tables
From: |
Juan Manuel Macías |
Subject: |
Re: [patch] ox-latex.el: add `:options' LaTeX attribute to tables |
Date: |
Wed, 27 Oct 2021 15:52:07 +0000 |
Hi Vikas,
Vikas Rawal writes:
> This is excellent. There is only one improvement that I would like you to
> consider. Is it possible to allow multiple ":options " lines that are
> appended when exported to
> +latex? Something like this:
>
> #+ATTR_LATEX: :environment longtblr
> #+ATTR_LATEX: :align colspec = {XXX}, width = 0.85\linewidth
> #+ATTR_LATEX: :options remark{Note} = {Lorem ipsum dolor sit amet}
> #+ATTR_LATEX: :options remark{Source} = {The source of data}
>
> ==> \begin{longtblr}[remark{Note} = {Lorem ipsum dolor sit amet},
> remark{Source} = {The source of data}]{colspec = {XXX},
> width = 0.85\linewidth}
>
> This would be more elegant since table notes can be elaborate and putting
> them all in one line would make them unwieldy.
>
> At the moment, only the last ":options" line is considered.
The normal behavior is that you cannot put more than one property
`:foo', because in that case only the value of the last one is added
when exporting to LaTeX (ther is a property list where each property has
a string value).
I can think of a somewhat tricky solution, in case you want to adapt it
to your use case:
First, you need to convert a plist to an alist (source:
https://caiorss.github.io/Emacs-Elisp-Programming/Elisp_Programming.html#sec-1-8-4):
#+begin_src emacs-lisp
(defun plist->alist (plist)
(if (null plist)
'()
(cons
(list (car plist) (cadr plist))
(plist->alist (cddr plist)))))
#+end_src
And this would be the modified function:
#+begin_src emacs-lisp
(defun org-latex--org-table (table contents info)
"Return appropriate LaTeX code for an Org table.
TABLE is the table type element to transcode. CONTENTS is its
contents, as a string. INFO is a plist used as a communication
channel.
This function assumes TABLE has `org' as its `:type' property and
`table' as its `:mode' attribute."
(let* ((attr (org-export-read-attribute :attr_latex table))
(alignment (org-latex--align-string table info))
;; various `:options' props ;;;; <== modified from here
(attr-alist (plist->alist attr))
(options-list)
(options-str (progn
(mapc (lambda (m)
(when (eq (car m) :options)
(add-to-list 'options-list (cdr m))))
attr-alist)
(mapconcat (lambda (x) (mapconcat 'identity x ""))
options-list " ")))
;;;;;;;; < == to here
(table-env (or (plist-get attr :environment)
(plist-get info :latex-default-table-environment)))
(width
(let ((w (plist-get attr :width)))
(cond ((not w) "")
((member table-env '("tabular" "longtable")) "")
((member table-env '("tabu" "longtabu"))
(format (if (plist-get attr :spread) " spread %s "
" to %s ")
w))
(t (format "{%s}" w)))))
(caption (org-latex--caption/label-string table info))
(above? (org-latex--caption-above-p table info)))
(cond
((member table-env '("longtable" "longtabu"))
(let ((fontsize (let ((font (plist-get attr :font)))
(and font (concat font "\n")))))
(concat (and fontsize (concat "{" fontsize))
(format "\\begin{%s}%s{%s}\n" table-env width alignment)
(and above?
(org-string-nw-p caption)
(concat caption "\\\\\n"))
contents
(and (not above?)
(org-string-nw-p caption)
(concat caption "\\\\\n"))
(format "\\end{%s}" table-env)
(and fontsize "}"))))
(t
(let ((output (format "\\begin{%s}%s%s{%s}\n%s\\end{%s}"
table-env
;; modified here
(if options-list (format "[%s]" options-str) "")
width
alignment
contents
table-env)))
(org-latex--decorate-table output attr caption above? info))))))
#+end_src
Pleas tell me if it works for you.
Best regards,
Juan Manuel