Nicolas Goaziou <n.goaz...@gmail.com> writes:

> Hello,
>
> Eric Abrahamsen <e...@ericabrahamsen.net> 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,


Reply via email to