ox-latex: Extend LaTeX attributes and captions to "table.el" tables

* lisp/ox-latex.el (org-latex--decorate-table): New function.
(org-latex--org-table): Use new function.
(org-latex--table.el-table): Use new function.
This commit is contained in:
Nicolas Goaziou 2018-11-30 18:43:30 +01:00
parent 366d38633a
commit b3c40c32b6
2 changed files with 103 additions and 114 deletions

View File

@ -42,6 +42,9 @@ See [[git:3367ac9457]] for details.
** New features ** New features
*** Babel *** Babel
**** Add LaTeX output support in PlantUML **** Add LaTeX output support in PlantUML
*** Allow LaTeX attributes and captions for "table.el" tables
Supported LaTeX attributes are ~:float~, ~:center~, ~:font~ and
~:caption~.
*** Attach buffer contents to headline *** Attach buffer contents to headline
With =<b>= key from attachment dispatcher (=<C-c C-a>=), it is now With =<b>= key from attachment dispatcher (=<C-c C-a>=), it is now
possible to write the contents of a buffer to a file in the headline possible to write the contents of a buffer to a file in the headline

View File

@ -3184,6 +3184,58 @@ centered."
info) info)
(apply 'concat (nreverse align))))) (apply 'concat (nreverse align)))))
(defun org-latex--decorate-table (table attributes caption above? info)
"Decorate TABLE string with caption and float environment.
ATTRIBUTES is the plist containing is LaTeX attributes. CAPTION
is its caption. It is located above the table if ABOVE? is
non-nil. INFO is the plist containing current export parameters.
Return new environment, as a string."
(let* ((float-environment
(let ((float (plist-get attributes :float)))
(cond ((and (not float) (plist-member attributes :float)) nil)
((member float '("sidewaystable" "sideways")) "sidewaystable")
((equal float "multicolumn") "table*")
((or float
(org-element-property :caption table)
(org-string-nw-p (plist-get attributes :caption)))
"table")
(t nil))))
(placement
(or (plist-get attributes :placement)
(format "[%s]" (plist-get info :latex-default-figure-position))))
(center? (if (plist-member attributes :center)
(plist-get attributes :center)
(plist-get info :latex-tables-centered)))
(fontsize (let ((font (plist-get attributes :font)))
(and font (concat font "\n")))))
(concat (cond
(float-environment
(concat (format "\\begin{%s}%s\n" float-environment placement)
(if above? caption "")
(when center? "\\centering\n")
fontsize))
(caption
(concat (and center? "\\begin{center}\n" )
(if above? caption "")
(cond ((and fontsize center?) fontsize)
(fontsize (concat "{" fontsize))
(t nil))))
(center? (concat "\\begin{center}\n" fontsize))
(fontsize (concat "{" fontsize)))
table
(cond
(float-environment
(concat (if above? "" (concat "\n" caption))
(format "\n\\end{%s}" float-environment)))
(caption
(concat (if above? "" (concat "\n" caption))
(and center? "\n\\end{center}")
(and fontsize (not center?) "}")))
(center? "\n\\end{center}")
(fontsize "}")))))
(defun org-latex--org-table (table contents info) (defun org-latex--org-table (table contents info)
"Return appropriate LaTeX code for an Org table. "Return appropriate LaTeX code for an Org table.
@ -3193,109 +3245,44 @@ channel.
This function assumes TABLE has `org' as its `:type' property and This function assumes TABLE has `org' as its `:type' property and
`table' as its `:mode' attribute." `table' as its `:mode' attribute."
(let* ((caption (org-latex--caption/label-string table info)) (let* ((attr (org-export-read-attribute :attr_latex table))
(attr (org-export-read-attribute :attr_latex table))
;; Determine alignment string.
(alignment (org-latex--align-string table info)) (alignment (org-latex--align-string table info))
;; Determine environment for the table: longtable, tabular...
(table-env (or (plist-get attr :environment) (table-env (or (plist-get attr :environment)
(plist-get info :latex-default-table-environment))) (plist-get info :latex-default-table-environment)))
;; If table is a float, determine environment: table, table* (width
;; or sidewaystable. (let ((w (plist-get attr :width)))
(float-env (unless (member table-env '("longtable" "longtabu")) (cond ((not w) "")
(let ((float (plist-get attr :float))) ((member table-env '("tabular" "longtable")) "")
(cond ((member table-env '("tabu" "longtabu"))
((and (not float) (plist-member attr :float)) nil) (format (if (plist-get attr :spread) " spread %s "
((or (string= float "sidewaystable") " to %s ")
(string= float "sideways")) "sidewaystable") w))
((string= float "multicolumn") "table*") (t (format "{%s}" w)))))
((or float (caption (org-latex--caption/label-string table info))
(org-element-property :caption table) (above? (org-latex--caption-above-p table info)))
(org-string-nw-p (plist-get attr :caption)))
"table")))))
;; Extract others display options.
(fontsize (let ((font (plist-get attr :font)))
(and font (concat font "\n"))))
;; "tabular" environment doesn't allow to define a width.
(width (and (not (equal table-env "tabular")) (plist-get attr :width)))
(spreadp (plist-get attr :spread))
(placement
(or (plist-get attr :placement)
(format "[%s]" (plist-get info :latex-default-figure-position))))
(centerp (if (plist-member attr :center) (plist-get attr :center)
(plist-get info :latex-tables-centered)))
(caption-above-p (org-latex--caption-above-p table info)))
;; Prepare the final format string for the table.
(cond (cond
;; Longtable. ((member table-env '("longtable" "longtabu"))
((equal "longtable" table-env) (let ((fontsize (let ((font (plist-get attr :font)))
(concat (and fontsize (concat "{" fontsize)) (and font (concat font "\n")))))
(format "\\begin{longtable}{%s}\n" alignment) (concat (and fontsize (concat "{" fontsize))
(and caption-above-p (format "\\begin{%s}%s{%s}\n" table-env width alignment)
(org-string-nw-p caption) (and above?
(concat caption "\\\\\n")) (org-string-nw-p caption)
contents (concat caption "\\\\\n"))
(and (not caption-above-p) contents
(org-string-nw-p caption) (and (not above?)
(concat caption "\\\\\n")) (org-string-nw-p caption)
"\\end{longtable}\n" (concat caption "\\\\\n"))
(and fontsize "}"))) (format "\\end{%s}" table-env)
;; Longtabu (and fontsize "}"))))
((equal "longtabu" table-env) (t
(concat (and fontsize (concat "{" fontsize)) (let ((output (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
(format "\\begin{longtabu}%s{%s}\n" table-env
(if width width
(format " %s %s " alignment
(if spreadp "spread" "to") width) "") contents
alignment) table-env)))
(and caption-above-p (org-latex--decorate-table output attr caption above? info))))))
(org-string-nw-p caption)
(concat caption "\\\\\n"))
contents
(and (not caption-above-p)
(org-string-nw-p caption)
(concat caption "\\\\\n"))
"\\end{longtabu}\n"
(and fontsize "}")))
;; Others.
(t (concat (cond
(float-env
(concat (format "\\begin{%s}%s\n" float-env placement)
(if caption-above-p caption "")
(when centerp "\\centering\n")
fontsize))
((and (not float-env) caption)
(concat
(and centerp "\\begin{center}\n" )
(if caption-above-p caption "")
(cond ((and fontsize centerp) fontsize)
(fontsize (concat "{" fontsize)))))
(centerp (concat "\\begin{center}\n" fontsize))
(fontsize (concat "{" fontsize)))
(cond ((equal "tabu" table-env)
(format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
(if width (format
(if spreadp " spread %s " " to %s ")
width) "")
alignment
contents))
(t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
table-env
(if width (format "{%s}" width) "")
alignment
contents
table-env)))
(cond
(float-env
(concat (if caption-above-p "" (concat "\n" caption))
(format "\n\\end{%s}" float-env)))
((and (not float-env) caption)
(concat
(if caption-above-p "" (concat "\n" caption))
(and centerp "\n\\end{center}")
(and fontsize (not centerp) "}")))
(centerp "\n\\end{center}")
(fontsize "}")))))))
(defun org-latex--table.el-table (table info) (defun org-latex--table.el-table (table info)
"Return appropriate LaTeX code for a table.el table. "Return appropriate LaTeX code for a table.el table.
@ -3309,18 +3296,20 @@ property."
;; Ensure "*org-export-table*" buffer is empty. ;; Ensure "*org-export-table*" buffer is empty.
(with-current-buffer (get-buffer-create "*org-export-table*") (with-current-buffer (get-buffer-create "*org-export-table*")
(erase-buffer)) (erase-buffer))
(let ((output (with-temp-buffer (let ((output
(insert (org-element-property :value table)) (replace-regexp-in-string
(goto-char 1) "^%.*\n" "" ;remove comments
(re-search-forward "^[ \t]*|[^|]" nil t) (with-temp-buffer
(table-generate-source 'latex "*org-export-table*") (save-excursion (insert (org-element-property :value table)))
(with-current-buffer "*org-export-table*" (re-search-forward "^[ \t]*|[^|]" nil t)
(org-trim (buffer-string)))))) (table-generate-source 'latex "*org-export-table*")
(with-current-buffer "*org-export-table*"
(org-trim (buffer-string))))
t t)))
(kill-buffer (get-buffer "*org-export-table*")) (kill-buffer (get-buffer "*org-export-table*"))
;; Remove left out comments. (let ((attr (org-export-read-attribute :attr_latex table))
(while (string-match "^%.*\n" output) (caption (org-latex--caption/label-string table info))
(setq output (replace-match "" t t output))) (above? (org-latex--caption-above-p table info)))
(let ((attr (org-export-read-attribute :attr_latex table)))
(when (plist-get attr :rmlines) (when (plist-get attr :rmlines)
;; When the "rmlines" attribute is provided, remove all hlines ;; When the "rmlines" attribute is provided, remove all hlines
;; but the the one separating heading from the table body. ;; but the the one separating heading from the table body.
@ -3329,10 +3318,7 @@ property."
(setq pos (string-match "^\\\\hline\n?" output pos))) (setq pos (string-match "^\\\\hline\n?" output pos)))
(cl-incf n) (cl-incf n)
(unless (= n 2) (setq output (replace-match "" nil nil output)))))) (unless (= n 2) (setq output (replace-match "" nil nil output))))))
(let ((centerp (if (plist-member attr :center) (plist-get attr :center) (org-latex--decorate-table output attr caption above? info))))
(plist-get info :latex-tables-centered))))
(if (not centerp) output
(format "\\begin{center}\n%s\n\\end{center}" output))))))
(defun org-latex--math-table (table info) (defun org-latex--math-table (table info)
"Return appropriate LaTeX code for a matrix. "Return appropriate LaTeX code for a matrix.