emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* using export filters to emulate multi-column table cells?
@ 2013-02-14  5:08 Eric Abrahamsen
  2013-02-14 17:42 ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Abrahamsen @ 2013-02-14  5:08 UTC (permalink / raw)
  To: emacs-orgmode

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.

No clue how feasible that might be...

Eric

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: using export filters to emulate multi-column table cells?
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2013-02-14 17:42 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

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:

#+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,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: using export filters to emulate multi-column table cells?
  2013-02-14 17:42 ` Nicolas Goaziou
@ 2013-02-15  2:53   ` Eric Abrahamsen
  2013-02-15 13:07     ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Abrahamsen @ 2013-02-15  2:53 UTC (permalink / raw)
  To: emacs-orgmode

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,

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: using export filters to emulate multi-column table cells?
  2013-02-15  2:53   ` Eric Abrahamsen
@ 2013-02-15 13:07     ` Nicolas Goaziou
  2013-02-15 13:51       ` Eric Abrahamsen
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2013-02-15 13:07 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

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

> 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...?

Honestly, I would match the entities. You get a uniform syntax and,
since filters are for your personal use, you don't have to tell anyone
about the ugliness of the implementation.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: using export filters to emulate multi-column table cells?
  2013-02-15 13:07     ` Nicolas Goaziou
@ 2013-02-15 13:51       ` Eric Abrahamsen
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Abrahamsen @ 2013-02-15 13:51 UTC (permalink / raw)
  To: emacs-orgmode

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

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Nicolas Goaziou <n.goaziou@gmail.com> writes:
>
>> 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...?
>
> Honestly, I would match the entities. You get a uniform syntax and,
> since filters are for your personal use, you don't have to tell anyone
> about the ugliness of the implementation.

I'll hold my nose, then. Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-02-15 13:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2013-02-15 13:07     ` Nicolas Goaziou
2013-02-15 13:51       ` Eric Abrahamsen

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).