From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Abrahamsen Subject: Re: using export filters to emulate multi-column table cells? Date: Fri, 15 Feb 2013 10:53:48 +0800 Message-ID: <871uci9tj7.fsf@ericabrahamsen.net> References: <87sj4zbhy5.fsf@ericabrahamsen.net> <874nhe94gz.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([208.118.235.92]:54713) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U6BM3-0001co-Ti for emacs-orgmode@gnu.org; Thu, 14 Feb 2013 21:48:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U6BM0-0006ir-1q for emacs-orgmode@gnu.org; Thu, 14 Feb 2013 21:48:51 -0500 Received: from plane.gmane.org ([80.91.229.3]:57240) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U6BLz-0006ho-Rz for emacs-orgmode@gnu.org; Thu, 14 Feb 2013 21:48:47 -0500 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U6BMG-00065r-RD for emacs-orgmode@gnu.org; Fri, 15 Feb 2013 03:49:04 +0100 Received: from 114.250.119.40 ([114.250.119.40]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 15 Feb 2013 03:49:04 +0100 Received: from eric by 114.250.119.40 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 15 Feb 2013 03:49:04 +0100 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Nicolas Goaziou writes: > Hello, > > Eric Abrahamsen writes: > >> Is there any way to use the new exporter mechanisms to emulate table >> cells that span rows or columns on export? I'm envisioning something >> like this: >> >> | | <2col>Grains | | >> | Year | Oats | Wheat | >> | 2007 | 10lbs | 40lbs | >> >> Where an export filter would pick up on the <2col> cookie, replace it >> with something backend-specific like \multicolumn{2}{Grains}, and then >> somehow remove the following table field from export. > > You can add a function to `org-export-filter-table-row-functions'. It > will be called with three arguments. The first one is the table row, as > a string in back-end syntax. If you recognize the pattern "<2col>", you > modify the string accordingly and return it as a replacement. E.g: Brilliant! This is a great solution. I've pasted a version below which expands the cookie slightly to allow for per-field alignment, with a syntax that looks like <2colc> for two-column, center alignment. I'd like to do the same for HTML, but of course the brackets in the cookie are escaped by the time this filter kicks in. Would you recommend using a different character to delineate the cookie, or match the entities (yuck), or...? Very pleased, Eric #+begin_src emacs-lisp (defun my-latex-multicolumn-filter (row backend info) (when (org-export-derived-backend-p backend 'latex) (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 "[[: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)) (add-to-list 'org-export-filter-table-row-functions 'my-latex-multicolumn-filter) #+end_src > #+begin_src emacs-lisp > (defun my-multicolumn-filter (row backend info) > (when (and (org-export-derived-backend-p backend 'latex) > (string-match "<\\([0-9]+\\)col>" row)) > (let ((columns (string-to-number (match-string 1 row))) > (start (match-end 0))) > (setq row (replace-match "" 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 'my-multicolumn-filter) > #+end_src > > > Regards,