emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: Re: using export filters to emulate multi-column table cells?
Date: Fri, 15 Feb 2013 10:53:48 +0800	[thread overview]
Message-ID: <871uci9tj7.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 874nhe94gz.fsf@gmail.com

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

> Hello,
>
> Eric Abrahamsen <eric@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	other threads:[~2013-02-15  2:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-14  5:08 using export filters to emulate multi-column table cells? Eric Abrahamsen
2013-02-14 17:42 ` Nicolas Goaziou
2013-02-15  2:53   ` Eric Abrahamsen [this message]
2013-02-15 13:07     ` Nicolas Goaziou
2013-02-15 13:51       ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871uci9tj7.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).