From mboxrd@z Thu Jan 1 00:00:00 1970 From: Vikas Rawal Subject: multicolumn cells in latex export Date: Sat, 5 Nov 2016 13:27:21 +0530 Message-ID: <1F648E1B-FFC1-4F1A-A29C-69030E4A5084@agrarianresearch.org> Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_D10FD911-C5FC-4681-BC56-686C78561649" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:50254) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c2vr3-0004ih-PJ for emacs-orgmode@gnu.org; Sat, 05 Nov 2016 03:57:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c2vr0-00072d-Kf for emacs-orgmode@gnu.org; Sat, 05 Nov 2016 03:57:33 -0400 Received: from mail-pf0-x22c.google.com ([2607:f8b0:400e:c00::22c]:34750) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c2vr0-00071T-9L for emacs-orgmode@gnu.org; Sat, 05 Nov 2016 03:57:30 -0400 Received: by mail-pf0-x22c.google.com with SMTP id n85so64419207pfi.1 for ; Sat, 05 Nov 2016 00:57:29 -0700 (PDT) Received: from [192.168.1.11] ([182.64.172.128]) by smtp.gmail.com with ESMTPSA id w15sm25262174pfi.55.2016.11.05.00.57.26 for (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 05 Nov 2016 00:57:27 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: org-mode mailing list --Apple-Mail=_D10FD911-C5FC-4681-BC56-686C78561649 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Two years ago, Eric Schulte had shared some most useful code snippets = (https://lists.gnu.org/archive/html/emacs-orgmode/2014-08/msg00113.html = ).= This included an export filter (code pasted below) that provided a very = useful way of introducing multicolumn cells in latex export. I have been using this since then without any trouble. But today I tried = using it to centre-align column headings in the top row by using = <1colc>. While it works for other columns, it does not work on the last = column.=20 The last column gets exported as=20 \multicolumn{1}{c}{Australia (2015)\\ }\hline rather than as \multicolumn{1}{c}{Australia (2015)}\\ \hline Can anyone help debug what is wrong with this code? Vikas --------------- ;;; Multi-column Table Cells ;; ;; Export table cells with multiple columns using Latex-like syntax. ;; For example in the following the "<3colc>Backends" cell spans 3 ;; columns with it's text centered. ;; ;; | | <3colc>Backends | | | ;; | | LaTeX | HTML | Text | ;; |-----------+-----------------+-------+-------| ;; | extension | .tex | .html | .txt | ;; ;; The `org-export-multicolumn-filter-latex' function is taken from ;; the following. ;; = http://thread.gmane.org/gmane.emacs.orgmode/66332/match=3Dlatex+table+mult= icolumn+cell (defun org-export-multicolumn-filter (row backend info) (cond ((org-export-derived-backend-p backend 'latex) (org-export-multicolumn-filter-latex row backend info)) ((org-export-derived-backend-p backend 'html) (org-export-multicolumn-filter-html row backend info)))) (defun org-export-multicolumn-filter-latex (row backend info) (while (string-match "\\(<\\([0-9]+\\)col\\([lrc]\\)?>[[:blank:]]*\\([^&]+\\)\\)" = row) (let ((columns (string-to-number (match-string 2 row))) (start (match-end 0)) (contents (replace-regexp-in-string "\\\\" "\\\\\\\\" (replace-regexp-in-string "[[:blank:]]*$" "" (match-string 4 row)))) (algn (or (match-string 3 row) "l"))) (setq row (replace-match (format "\\\\multicolumn{%d}{%s}{%s}" columns algn = contents) nil nil row 1)) (while (and (> columns 1) (string-match "&" row start)) (setq row (replace-match "" nil nil row)) (decf columns)))) row) (defun org-export-multicolumn-filter-html (row backend info) (while (string-match "class=3D\".*\" = *><\\([0-9]+\\)col\\([lrc]\\)?>" row) (let ((columns (string-to-number (match-string 1 row))) (start (match-end 0)) (algn (case (intern (or (match-string 2 row) "l")) (c "center") (r "right") (l "left")))) (setq row (replace-match (format " class=3D\"%s\" colspan=3D\"%s\">" algn = columns) nil nil row)) (while (and (> columns 1) (string-match " " row start)) (setq row (replace-match "" nil nil row)) (decf columns)))) row) (add-to-list 'org-export-filter-table-row-functions 'org-export-multicolumn-filter) --Apple-Mail=_D10FD911-C5FC-4681-BC56-686C78561649 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Two years ago, Eric Schulte had shared some most useful code = snippets (https://lists.gnu.org/archive/html/emacs-orgmode/2014-08/msg001= 13.html). This included an export filter (code pasted below) that = provided a very useful way of introducing multicolumn cells in latex = export.

I have been = using this since then without any trouble. But today I tried using it to = centre-align column headings in the top row by using <1colc>. = While it works for other columns, it does not work on the last = column. 

The last column gets exported as 
\multicolumn{1}{c}{Australia = (2015)\\
}\hline

rather than as

\multicolumn{1}{c}{Australia (2015)}\\
\hline

Can anyone help debug what is wrong with this code?

Vikas

---------------

;;; = Multi-column Table Cells
;;
;; = Export table cells with multiple columns using Latex-like = syntax.
;; For example in the following the = "<3colc>Backends" cell spans 3
;; columns = with it's text centered.
;;
;; = |           | <3colc>Backends |   =     |       |
;; |   =         |           LaTeX | =  HTML |  Text |
;; = |-----------+-----------------+-------+-------|
;; = | extension |            .tex | .html | = .txt  |
;;
;; The = `org-export-multicolumn-filter-latex' function is taken from
;; the following.
(defun = org-export-multicolumn-filter (row backend info)
  (cond
  =  ((org-export-derived-backend-p backend 'latex)
    (org-export-multicolumn-filter-latex row = backend info))
  =  ((org-export-derived-backend-p backend 'html)
    (org-export-multicolumn-filter-html row backend = info))))

(defun = org-export-multicolumn-filter-latex (row backend info)
  (while (string-match
  =         = "\\(<\\([0-9]+\\)col\\([lrc]\\)?>[[:blank:]]*\\([^&]+\\)\\)" = row)
    (let ((columns (string-to-number = (match-string 2 row)))
        =   (start (match-end 0))
      =     (contents (replace-regexp-in-string
                =      "\\\\" "\\\\\\\\"
    =                 =  (replace-regexp-in-string "[[:blank:]]*$" ""
                =                     =            (match-string 4 = row))))
          (algn = (or (match-string 3 row) "l")))
    =   (setq row (replace-match
    =              (format = "\\\\multicolumn{%d}{%s}{%s}" columns algn contents)
                =  nil nil row 1))
      (while = (and (> columns 1) (string-match "&" row start))
        (setq row (replace-match "" nil = nil row))
        (decf = columns))))
  row)

(defun = org-export-multicolumn-filter-html (row backend info)
  (while (string-match "class=3D\".*\" = *>&lt;\\([0-9]+\\)col\\([lrc]\\)?&gt;" row)
    (let ((columns (string-to-number (match-string = 1 row)))
          (start = (match-end 0))
          = (algn (case (intern (or (match-string 2 row) "l"))
                =   (c "center")
        =           (r "right")
 =                 (l = "left"))))
      (setq row = (replace-match
          =        (format " class=3D\"%s\" colspan=3D\"%s\">" = algn columns)
          =        nil nil row))
  =     (while (and (> columns 1)
  =                 (string-match = "<th .*>&#xa0;</th>" row start))
        (setq row (replace-match "" nil = nil row))
        (decf = columns))))
  row)

(add-to-list = 'org-export-filter-table-row-functions
  =           =  'org-export-multicolumn-filter)

= --Apple-Mail=_D10FD911-C5FC-4681-BC56-686C78561649--