emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Export tables as matrices (change tbl-export function on the fly)
@ 2012-11-14 14:49 Rasmus
  2012-11-14 16:21 ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Rasmus @ 2012-11-14 14:49 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I'm doing some stuff where the natural output of my tables are
matrices.  I found a decent translation  function here ¹.  However,
I'm not very successful in making org use it. 

I tried to add
#+begin_src org 
* heading
  :PROPERTIES:
  :TABLE_EXPORT_FORMAT: orgtbl-to-latex-matrix
  :END:
#+end src
to the relevant section of my document, but this does not seem to be
the correct usage. 

I also tried to radio tables.  The problem is that when I do

#+NAME: P
#+ORGTBL: SEND P orgtbl-to-latex-matrix
|1|

I cannot send P to R/Octave.  When I swap the ORGTBL and NAME around,
I can't send it to a radio table.  I also tried with TBLNAME. 

Anyway, the optimal would be to have Org just use a different
translation mechanism on the fly.  Is that possible?

Thanks,
Rasmus

Footnotes: 
 ¹   http://www.patokeefe.com/archives/742

#+begin_src emacs-lisp
(defun orgtbl-to-latex-matrix (table params)
  "Convert the Orgtbl mode TABLE to a LaTeX Matrix."
  (interactive)
  (let* ((params2
          (list
           :tstart (concat "\\[\n\\begin{bmatrix}")
           :tend "\\end{bmatrix}\n\\]"
           :lstart "" :lend " \\\\" :sep " & "
           :efmt "%s\\,(%s)" :hline "\\hline")))
    (orgtbl-to-generic table (org-combine-plists params2 params))))



(defcustom orgtbl-latex-matrix-string  "% BEGIN RECEIVE ORGTBL %n
% END RECEIVE ORGTBL %n
\\begin{comment}
#+ORGTBL: SEND %n orgtbl-to-latex-matrix :splice nil :skip 0

\\end{comment}\n"
  "Template for the latex matrix orgtbl translator
All occurrences of %n in a template will be replaced with the name of the
table, obtained by prompting the user."
  :type 'string
  :group 'org-table)
#+end_src

-- 
C is for Cookie

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-14 14:49 Export tables as matrices (change tbl-export function on the fly) Rasmus
@ 2012-11-14 16:21 ` Nicolas Goaziou
  2012-11-14 18:05   ` Rasmus
  2012-11-17 13:26   ` Rasmus Pank Roulund
  0 siblings, 2 replies; 11+ messages in thread
From: Nicolas Goaziou @ 2012-11-14 16:21 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> I'm doing some stuff where the natural output of my tables are
> matrices.  I found a decent translation  function here ¹.  However,
> I'm not very successful in making org use it. 

Using the new exporter, something like should replace any table using
default environment (i.e. no special attribute) and without horizontal
rules with bmatrix environment. It should also insert it in math mode
automatically.

Untested.

#+begin_src emacs-lisp
(defun my-latex-table-to-matrix (table backend info)
  (when (and (memq backend '(e-latex e-beamer))
             (not (string-match "^\\\\[A-Za-z]+$" table))
             (not (string-match "\\begin{\\(table*\\|sidewaystable\\)}" table)))
    (let ((default-env (format "\\\\\\(begin\\|end\\){\\(%s\\)}.*"
                               org-e-latex-default-table-environment)))
      (when (string-match default-env table)
        (concat "\\[\n"
                (org-trim
                 (replace-regexp-in-string
                  "\\\\\\(begin\\|end\\){\\(center\\)}" ""
                  (replace-regexp-in-string
                   default-env "\\\\\\1{bmatrix}" table)))
                "\n\\]")))))

(add-to-list 'org-export-filter-table-functions 'my-latex-table-to-matrix)
#+end_src


Regards,

-- 
Nicolas Goaziou

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-14 16:21 ` Nicolas Goaziou
@ 2012-11-14 18:05   ` Rasmus
  2012-11-17 13:26   ` Rasmus Pank Roulund
  1 sibling, 0 replies; 11+ messages in thread
From: Rasmus @ 2012-11-14 18:05 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode

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

> Hello,
>
> Rasmus <rasmus@gmx.us> writes:
>
>> I'm doing some stuff where the natural output of my tables are
>> matrices.  I found a decent translation function here ¹.  However,
>> I'm not very successful in making org use it. 
>
> Using the new exporter, something like should replace any table using
> default environment (i.e. no special attribute) and without horizontal
> rules with bmatrix environment. It should also insert it in math mode
> automatically.

this one seems to work better for me, but might be more ugly . . .

#+begin_src emacs-lisp

(defun my-latex-table-to-matrix (table backend info)
  (when (and (memq backend '(e-latex e-beamer))
             (not (string-match "\\\\^[A-Za-z]+$"  table))
             (not (string-match "\\begin{\\(table*\\|sidewaystable\\)}" table)))
    (let ((default-env (format "\\\\\\(begin\\|end\\){\\(%s\\)}.*"
                               org-e-latex-default-table-environment)))
      (when (string-match default-env table)
        (concat "\\[\n"
                (org-trim
		 (replace-regexp-in-string "\n\n" "\n"
                 (replace-regexp-in-string
                  "\\\\\\(begin\\|end\\){\\(center\\|table\\)}\\|\\\\toprule\\|\\\\bottomrule\\|\[[htbH]+?\]" ""
                  (replace-regexp-in-string
                   default-env "\\\\\\1{bmatrix}" table))))
                "\n\\]\n")))))
(add-to-list 'org-export-filter-table-functions 'my-latex-table-to-matrix)

#+end_src


-- 
Dung makes an excellent fertilizer

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-14 16:21 ` Nicolas Goaziou
  2012-11-14 18:05   ` Rasmus
@ 2012-11-17 13:26   ` Rasmus Pank Roulund
  2012-11-17 15:32     ` Nicolas Goaziou
  1 sibling, 1 reply; 11+ messages in thread
From: Rasmus Pank Roulund @ 2012-11-17 13:26 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode

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

>> I'm doing some stuff where the natural output of my tables are
>> matrices.  I found a decent translation function here ¹.  However,
>> I'm not very successful in making org use it. 
>
> Using the new exporter, something like should replace any table using
> default environment (i.e. no special attribute) and without horizontal
> rules with bmatrix environment. It should also insert it in math mode
> automatically.

I didn't manage to get your (Nicolas') or my own attempt working
correctly for exporting matrices.  I still think it would be nice.

I tried to use the regexp 
   (not (string-match "|[\\+-]+|"  table))
to identify tables without heading separators, but it didn't work
properly. 

Thinking about it, it might be nice to be able to specify table export
function more generally.  For instance, I might have a matrix with
labels (in LaTeX a bordermatrix or kbordermatrix).  Likewise, it might
also be nice to specify a header argument to tables s.t. I can specify
a name, e.g.

#+NAME: P 
#+TBLOPTIONS: :prefix "P=" :type matrix
| a| b|
| c| d|

would export to 
\[P=\begin{bmatrix}a&b\\c&d\end{bmatrix}\]

–Rasmus

-- 
May the Force be with you

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-17 13:26   ` Rasmus Pank Roulund
@ 2012-11-17 15:32     ` Nicolas Goaziou
  2012-11-17 18:29       ` Rasmus
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2012-11-17 15:32 UTC (permalink / raw)
  To: Rasmus Pank Roulund; +Cc: emacs-orgmode

Rasmus Pank Roulund <rasmus@gmx.us> writes:

> I didn't manage to get your (Nicolas') or my own attempt working
> correctly for exporting matrices.  I still think it would be nice.

It's hard to fix this since you're not very explicit here.

> I tried to use the regexp 
>    (not (string-match "|[\\+-]+|"  table))
> to identify tables without heading separators, but it didn't work
> properly.

It cannot work in filters. These are applied on back-end ouput, in this
case LaTeX code, not on Org code.

> Thinking about it, it might be nice to be able to specify table export
> function more generally.  For instance, I might have a matrix with
> labels (in LaTeX a bordermatrix or kbordermatrix).  Likewise, it might
> also be nice to specify a header argument to tables s.t. I can specify
> a name, e.g.
>
> #+NAME: P 
> #+TBLOPTIONS: :prefix "P=" :type matrix
> | a| b|
> | c| d|
>
> would export to 
> \[P=\begin{bmatrix}a&b\\c&d\end{bmatrix}\]

There is no TBLOPTIONS affiliated keyword, but it could go in ATTR_LATEX
since this probably only makes sense in a LaTeX-type output. If you
provide a full description of options and their effect, I might try to
implement it.


Regards,

-- 
Nicolas Goaziou

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-17 15:32     ` Nicolas Goaziou
@ 2012-11-17 18:29       ` Rasmus
  2012-11-18  8:53         ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Rasmus @ 2012-11-17 18:29 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode

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

>> I didn't manage to get your (Nicolas') or my own attempt working
>> correctly for exporting matrices.  I still think it would be nice.
> It's hard to fix this since you're not very explicit here.

Sorry; you posted a code first which didn't work at all for me; I
tried to fix it, and I posted a code which I though worked but in the
end it didn't.  Thus, I though it would be fair to post another
message stating this, if nothing else than for future viewers.

>> I tried to use the regexp 
>>    (not (string-match "|[\\+-]+|"  table))
>> to identify tables without heading separators, but it didn't work
>> properly.
>
> It cannot work in filters. These are applied on back-end ouput, in
> this case LaTeX code, not on Org code.

Aha, that explains a lot.  At some point when I don't have so many
assignments I really need to get to know the details of your work.
It's has so many cool features. 

> There is no TBLOPTIONS affiliated keyword, but it could go in
> ATTR_LATEX

That a lot more clever.  I just made up TBLOPTIONS since I couldn't
think of anywhere better.  But you are of right.  Such an option would
fit perfectly into ATTR_LATEX.


> If you provide a full description of options and their effect, I
> might try to implement it.

Okay I'll try.  I don't know whether something like the below is what
you are thinking of.  I use matrices all of the time so it would be
nice for me.

 - PROPOSAL: New option(s)for ATTR_LATEX
   - :type :: options a lisp translation function or key words
              associated with a lisp list translation function.
     - Default keyword: table; other known keywords: matrix
       - table: current exporter
       - matrix: exports to LaTeX matrix determined by the variable
         org-export-latex-tables-matrix-default-type or :matrix-type.
         Default is: bmatrix or pmatrix (probably bmatrix).
         - in general array requires more configuration, but for me
           array need not be supported.

   - Matrix relevant keywords :: are the following
     - If the additional variable ALIGN is set to k ∈ {l,r,c} use
       the starred version of
       org-export-latex-tables-matrix-default-type or :matrix-type
     - If the additional keyword :bordered is t use the typeset
       the matrix as \borderedmatrix{&col1&
       ... &colN\\row1&...\\...\\rowN}.  A better example is
       here ¹.  Also, the default bordermatrix macro is determined
       via org-export-latex-tables-matrix-bordered-type s.t. one
       can specify kbordermatrix ² or qbordermatrix ³.  Perhaps
       Org automatically add the respective usepackage if this
       option is set to something different from bordermatrix
       (i.e. org-export-latex-tables-matrix-bordered-type is a
       list of lists where the first element of a list is the
       macro name and the second is the needed package).
     - If :matrix-pre "string" is set "string" is typeset before
       the matrix
     - If :matrix-post "string" is set then "string" is typeset
       after the matrix.
       - Alternatively, CAPTION could be used, but it seems weird.
         Are they written before or after the matrix?  I'd prefer
         CAPTIONs to be ignored typeset when matrices are typeset.
     - If the table has a name the matrix is typeset using
       equation and given an label.  If not it may be typeset
       using equation* or \[·\].
     - Potentially: an :inline exists s.t. if inline is t the
       matrix is typeset inline [i.e. with \(\)].  Perhaps, it
       should be smart and use the small verison of
       org-export-latex-tables-matrix-default-type.  I.e. if
       bmatrix use bsmallmatrix.  This could be set via
       org-export-latex-tables-matrix-inline-small.

Thanks,
Rasmus


Footnotes: 
 ¹   http://www.math.harvard.edu/texman/node25.html
 ²   https://www.hss.caltech.edu/~kcb/LaTeX.shtml
 ³   https://code.google.com/p/qbordermatrix/

-- 
When the facts change, I change my mind. What do you do, sir?

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-17 18:29       ` Rasmus
@ 2012-11-18  8:53         ` Nicolas Goaziou
  2012-11-18 11:50           ` Rasmus
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2012-11-18  8:53 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Rasmus <rasmus@gmx.us> writes:

> Sorry; you posted a code first which didn't work at all for me; I
> tried to fix it, and I posted a code which I though worked but in the
> end it didn't.  Thus, I though it would be fair to post another
> message stating this, if nothing else than for future viewers.

I meant: what is the exact problem with my code (with examples and
expected output if possible)? It shouldn't be hard to fix.

>  - PROPOSAL: New option(s)for ATTR_LATEX
>    - :type :: options a lisp translation function or key words
>               associated with a lisp list translation function.

Forget the lisp translation function. At this level, the function
operates on parsed data. It doesn't use any orgtbl-to-* function. Also,
even if the ATTR_LATEX keyword applies on the table, it also affects how
`table-row' elements and `table-cell' objects are transcoded.

The usual way to define a new transcoder in the new export API is to
create a derived backed using an alternative translation table. It is
easier than it sounds.

>      - Default keyword: table; other known keywords: matrix
>        - table: current exporter
>        - matrix: exports to LaTeX matrix determined by the variable
>          org-export-latex-tables-matrix-default-type or :matrix-type.
>          Default is: bmatrix or pmatrix (probably bmatrix).
>          - in general array requires more configuration, but for me
>            array need not be supported.

I suggest to use a :math-mode t. With this mode, all cells are treated
as raw math code (no transformation is made on any cell). A global
variable can also set it `org-export-latex-table-math-mode' (default to
nil).

Then, the :environment keyword can be set to "tabular", "longtable",
"tabularx", "tabulary", "bmatrix", "pmatrix", "vmatrix", "kbordermatrix"
or "qbordermatrix". "bordermatrix", "array"... It would default to
`org-e-latex-default-table-environment' (default to "tabular").

>    - Matrix relevant keywords :: are the following
>      - If the additional variable ALIGN is set to k ∈ {l,r,c} use
>        the starred version of
>        org-export-latex-tables-matrix-default-type or :matrix-type

What is the variable ALIGN? You mean :align keyword? Do matrix
environments accept alignment? Also I haven't heard about starred
version of these. What do they do?

>      - If the additional keyword :bordered is t use the typeset
>        the matrix as \borderedmatrix{&col1&
>        ... &colN\\row1&...\\...\\rowN}.  A better example is
>        here ¹.  Also, the default bordermatrix macro is determined
>        via org-export-latex-tables-matrix-bordered-type s.t. one
>        can specify kbordermatrix ² or qbordermatrix ³.  

From the syntax perspective, I see no difference between bordered
matrices and the others: they just use a different environment, which
can be provided through :environment keyword. Am I missing something?

>        Perhaps Org automatically add the respective usepackage if this
>        option is set to something different from bordermatrix (i.e.
>        org-export-latex-tables-matrix-bordered-type is a list of lists
>        where the first element of a list is the macro name and the
>        second is the needed package).

The backend never adds any usepackage automatically. It is a can of
worms (incompatibilities, order of packages...). It's the responsibility
of the user to do so.

>      - If :matrix-pre "string" is set "string" is typeset before
>        the matrix
>      - If :matrix-post "string" is set then "string" is typeset
>        after the matrix.
>        - Alternatively, CAPTION could be used, but it seems weird.
>          Are they written before or after the matrix?  I'd prefer
>          CAPTIONs to be ignored typeset when matrices are typeset.
>      - If the table has a name the matrix is typeset using
>        equation and given an label.  If not it may be typeset
>        using equation* or \[·\].

Good idea.

>      - Potentially: an :inline exists s.t. if inline is t the
>        matrix is typeset inline [i.e. with \(\)].  Perhaps, it
>        should be smart and use the small verison of
>        org-export-latex-tables-matrix-default-type.  I.e. if
>        bmatrix use bsmallmatrix.  This could be set via
>        org-export-latex-tables-matrix-inline-small.

Do all matrix environments accept a small counterpart?


Regards,

-- 
Nicolas Goaziou

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-18  8:53         ` Nicolas Goaziou
@ 2012-11-18 11:50           ` Rasmus
  2012-11-18 13:20             ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Rasmus @ 2012-11-18 11:50 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode


Nicolas,

Thanks for your reply.


> I meant: what is the exact problem with my code (with examples and
> expected output if possible)? It shouldn't be hard to fix.

They are not caught by the regexp.  And I wasn't able to find a regexp
which could correctly identify all cases.  I have a sample in the footnote ¹ 

> Forget the lisp translation function. At this level, the function
> operates on parsed data. It doesn't use any orgtbl-to-* function. Also,
> even if the ATTR_LATEX keyword applies on the table, it also affects how
> `table-row' elements and `table-cell' objects are transcoded.

OK, thanks for pointing out.

> I suggest to use a :math-mode t. With this mode, all cells are
> treated as raw math code (no transformation is made on any cell).

Sounds good to me.  However, should $'s be stripped?  Say I have an
org-table which I'm indecisive about.  In one sell I might have a
\frac{·}{·} in a cell; so in a normal table export I'd have to insert
$'s, but in :math-mode I wouldn't.  It could thus be expensive to go
from :math mode to nil math mode.  Hence, stripping $ might be
appropriate

> A global variable can also set it `org-export-latex-table-math-mode'
> (default to nil).

Nil by default for sure.


> Then, the :environment keyword can be set to "tabular", "longtable",
> "tabularx", "tabulary", "bmatrix", "pmatrix", "vmatrix", "kbordermatrix"
> or "qbordermatrix". "bordermatrix", "array"... It would default to
> `org-e-latex-default-table-environment' (default to "tabular").

and Bmatrix ({matrix}) and Vmatrix ( ||matrix||) and matrix (no
decoration).

Array needs per column configuration. 

> What is the variable ALIGN? You mean :align keyword? Do matrix
> environments accept alignment? Also I haven't heard about starred
> version of these. What do they do?

They allow you to align a matrix to [rlc] (and maybe others?)  They
are defined in mathtools.

Here's an example (unrelated to tables):

#+BEGIN_SRC latex
\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
$\max_{x}f(x)\quad\text{s.t. }\left\{\begin{matrix*}[r]g(x)\geq0\\h(x)+h(y)\geq
    k\end{matrix*}\right.$
\end{document}
#+END_SRC


> From the syntax perspective, I see no difference between bordered
> matrices and the others: they just use a different environment, which
> can be provided through :environment keyword. Am I missing something?

No.  But they are typically macros.  Note the use of cr which
sometimes needs to be used in the bordermatrix (from TeX).  It's not
the case in kbordermatrix (which I use) and presumably qbordermatrix.

#+BEGIN_SRC latex
\documentclass{article}
\begin{document}
$\bordermatrix{&a&b&c\cr a&1&0&0\cr b&0&1&0\cr c&0&0&1}$
\end{document}
#+END_SRC


> The backend never adds any usepackage automatically. It is a can of
> worms (incompatibilities, order of packages...). It's the responsibility
> of the user to do so.

I agree.  

>>      - Potentially: an :inline exists s.t. if inline is t the
>>        matrix is typeset inline [i.e. with \(\)].  Perhaps, it
>>        should be smart and use the small verison of
>>        org-export-latex-tables-matrix-default-type.  I.e. if
>>        bmatrix use bsmallmatrix.  This could be set via
>>        org-export-latex-tables-matrix-inline-small.
>
> Do all matrix environments accept a small counterpart?

 amsmath provides: 
    smallmatrix from which all variants be generated using
    appropriated \left "bracket" \right "bracket"
 mathtools provides:
     smallmatrix*
     psmallmatrix 
     psmallmatrix*
     bsmallmatrix 
     bsmallmatrix*
     Bsmallmatrix 
     Bsmallmatrix*
     vsmallmatrix 
     vsmallmatrix*
     Vsmallmatrix 
     Vsmallmatrix*
     smallmatrix*

so I think it covers all variants except "small bordermatrix" and
"small arrary".

Thanks,
Rasmus

Footnotes: 
 ¹   Adding the function function in the second post, the following
examples all translates to tables

#+BEGIN_SRC org
#+NAME: t1
| my table | with rows |
|----------+-----------|
| a        | 2         |
| 5        | 3         |


matrix

| 1 | 2 |
| 3 | 4 |

#+NAME:t2
| 1 | 2 |
| 3 | 4 |
#+END_SRC

and output (note in dirty Emacs [not -q])

#+BEGIN_SRC LaTeX
\begin{table}[htb]
\label{t1}
\begin{center}
\begin{tabular}{lr}
\toprule
my table & with rows\\
\midrule
a & 2\\
5 & 3\\
\bottomrule
\end{tabular}
\end{center}
\end{table}


matrix

\begin{center}
\begin{tabular}{rr}
\toprule
1 & 2\\
3 & 4\\
\bottomrule
\end{tabular}
\end{center}

\begin{table}[htb]
\label{t2}
\begin{center}
\begin{tabular}{rr}
\toprule
1 & 2\\
3 & 4\\
\bottomrule
\end{tabular}
\end{center}
\end{table}
#+END_SRC

-- 
A page of history is worth a volume of logic

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-18 11:50           ` Rasmus
@ 2012-11-18 13:20             ` Nicolas Goaziou
  2012-11-18 14:05               ` Rasmus
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2012-11-18 13:20 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Rasmus <rasmus@gmx.us> writes:

>> I meant: what is the exact problem with my code (with examples and
>> expected output if possible)? It shouldn't be hard to fix.
>
> They are not caught by the regexp.  And I wasn't able to find a regexp
> which could correctly identify all cases.  I have a sample in the
> footnote ¹ 

That's because you use "booktabs" by default. It creates horizontal
rules, and the filter hence doesn't apply.

> Sounds good to me.  However, should $'s be stripped?  Say I have an
> org-table which I'm indecisive about.  In one sell I might have a
> \frac{·}{·} in a cell; so in a normal table export I'd have to insert
> $'s, but in :math-mode I wouldn't.  It could thus be expensive to go
> from :math mode to nil math mode.  Hence, stripping $ might be
> appropriate

Since e-latex back-end doesn't strip anything in regular LaTeX snippets
or environments, it should be the same here. Switching to math mode
won't happen automatically, so the user can act appropriately.

>> Then, the :environment keyword can be set to "tabular", "longtable",
>> "tabularx", "tabulary", "bmatrix", "pmatrix", "vmatrix", "kbordermatrix"
>> or "qbordermatrix". "bordermatrix", "array"... It would default to
>> `org-e-latex-default-table-environment' (default to "tabular").
>
> and Bmatrix ({matrix}) and Vmatrix ( ||matrix||) and matrix (no
> decoration).

:environment value is a string. It can be set to anything.

> Array needs per column configuration. 

That's not a problem, Org handles alignment per column already:

    | <c> | <c> |
    | a   | b   |
    | c   | d   |

Also, every math environment but "array" ignores column separators.

>> What is the variable ALIGN? You mean :align keyword? Do matrix
>> environments accept alignment? Also I haven't heard about starred
>> version of these. What do they do?
>
> They allow you to align a matrix to [rlc] (and maybe others?)  They
> are defined in mathtools.
>
> Here's an example (unrelated to tables):
>
> #+BEGIN_SRC latex
> \documentclass{article}
> \usepackage{amsmath,mathtools}
> \begin{document}
> $\max_{x}f(x)\quad\text{s.t. }\left\{\begin{matrix*}[r]g(x)\geq0\\h(x)+h(y)\geq
>     k\end{matrix*}\right.$
> \end{document}
> #+END_SRC

Then the backend can add the alignment as an optional argument in math
mode only when the name of the environment ends with a star and the
alignment string is one character long.

>> From the syntax perspective, I see no difference between bordered
>> matrices and the others: they just use a different environment, which
>> can be provided through :environment keyword. Am I missing something?
>
> No.  But they are typically macros.  Note the use of cr which
> sometimes needs to be used in the bordermatrix (from TeX).  It's not
> the case in kbordermatrix (which I use) and presumably qbordermatrix.
>
> #+BEGIN_SRC latex
> \documentclass{article}
> \begin{document}
> $\bordermatrix{&a&b&c\cr a&1&0&0\cr b&0&1&0\cr c&0&0&1}$
> \end{document}
> #+END_SRC

To circumvent the problem, org-e-latex.el can use a defconst to store
table/matrix environments requiring "\cr" instead of "\\" at the end of
each row. It will, as a starter, only contain "bordermatrix".

Any name to suggest for it?

>>>      - Potentially: an :inline exists s.t. if inline is t the
>>>        matrix is typeset inline [i.e. with \(\)].  Perhaps, it
>>>        should be smart and use the small verison of
>>>        org-export-latex-tables-matrix-default-type.  I.e. if
>>>        bmatrix use bsmallmatrix.  This could be set via
>>>        org-export-latex-tables-matrix-inline-small.
>>
>> Do all matrix environments accept a small counterpart?
>
>  amsmath provides: 
>     smallmatrix from which all variants be generated using
>     appropriated \left "bracket" \right "bracket"
>  mathtools provides:
>      smallmatrix*
>      psmallmatrix 
>      psmallmatrix*
>      bsmallmatrix 
>      bsmallmatrix*
>      Bsmallmatrix 
>      Bsmallmatrix*
>      vsmallmatrix 
>      vsmallmatrix*
>      Vsmallmatrix 
>      Vsmallmatrix*
>      smallmatrix*
>
> so I think it covers all variants except "small bordermatrix" and
> "small arrary".

Ok. Then the sanest approach is to allow :math-mode to be set to nil,
t or `inline'. When `inline', as you suggest, it replaces \[...\] with
\(...\) and ignores any label. But it still uses whatever environment is
provided in :environment, that is, it won't make any guess about which
one to use.

Also, instead of using a single `org-e-latex-default-table-environment',
its value can be a plist like the following:

   (:table-mode "tabular" :math-mode "bmatrix" :inline-math-mode "smallmatrix")

What do you think about it?


Regards,

-- 
Nicolas Goaziou

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-18 13:20             ` Nicolas Goaziou
@ 2012-11-18 14:05               ` Rasmus
  2012-11-18 19:12                 ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Rasmus @ 2012-11-18 14:05 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode


Nicolas,

> That's because you use "booktabs" by default. It creates horizontal
> rules, and the filter hence doesn't apply.

Aha.  Is this (again) a misunderstanding because I though of org
source?  I tried to identify org tables with rules.

> Since e-latex back-end doesn't strip anything in regular LaTeX
> snippets or environments, it should be the same here. Switching to
> math mode won't happen automatically, so the user can act
> appropriately.

I guess it's fine for most applications.


> :environment value is a string. It can be set to anything.

Probably the best approach. 

> That's not a problem, Org handles alignment per column already:
>
>     | <c> | <c> |
>     | a   | b   |
>     | c   | d   |

I had no idea.  That's very cool.


> Then the backend can add the alignment as an optional argument in math
> mode only when the name of the environment ends with a star and the
> alignment string is one character long.

In the LATEX_ATTR or as in the example above?  The former I assume.


> To circumvent the problem, org-e-latex.el can use a defconst to store
> table/matrix environments requiring "\cr" instead of "\\" at the end of
> each row. It will, as a starter, only contain "bordermatrix".

OK.  How about just making a list of strings (with typical member
STRING) which are known to have the format
\STRING{matrix-like-output}?  Then one can add whichever crazy
environment that might be needed to this list.  And users can easily
add their favorite flavor of e.g. bordermatrix.  

A short experiment seems to suggest that \cr works in place of \\.


> Any name to suggest for it?

I prefer the approach where the exists a list of macro-matrices, maybe
with delimiter and/or line separator. Then the the value of
:environment can be checked against this list.
E.g. org-e-latex-matrix-macro-environments.  By default:

     '(("bordermatrix" "\cr"))

And I'd personally extend it to 

    '(("bordermatrix" "\cr") ("kbordermatrix" "\\"))

NOTE: probably it's not nice to store line seperator and such here,
and indeed

      '("bordermatrix" "kbordermatix")

seems like a nicer (as in simpler) list.


> Ok. Then the sanest approach is to allow :math-mode to be set to nil,
> t or `inline'. When `inline', as you suggest, it replaces \[...\] with
> \(...\) and ignores any label. But it still uses whatever environment is
> provided in :environment, that is, it won't make any guess about which
> one to use.

Yeah, you're right.  Less guessing is better guessing.  'Display'
might be same t.


> Also, instead of using a single `org-e-latex-default-table-environment',
> its value can be a plist like the following:
>
>    (:table-mode "tabular" :math-mode "bmatrix" :inline-math-mode "smallmatrix")

Yeah, that would be nice.  Would it extend to the options in
ATTR_LATEX?  I.e. would it be nice (?) to be able to specify
   - environment
   - math-environment
   - inline-math-environment
in one option line s.t. I could easily switch from table to matrix to
inline matrix?  E.g.

#+ATTR_LATEX: :math-environment "Bmatrix" :math-mode t
prints a display matrix 
but 
#+ATTR_LATEX: :math-environment "Bmatrix" :math-mode nil
prints a table.

I don't know whether this is just making things more confusing or
whether it would be useful. . . 

Also, at least a prefix string would be very useful as that's how
matrices are usually named.  E.g. 

#+ATTR_LATEX: :math-mode t :prefix "P="
|1|2|
|3|4|

is translated to 
P=\begin{bmatrix}1&2\\3&4\end{bmatrix}


Thanks,
Rasmus

-- 
Dung makes an excellent fertilizer

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

* Re: Export tables as matrices (change tbl-export function on the fly)
  2012-11-18 14:05               ` Rasmus
@ 2012-11-18 19:12                 ` Nicolas Goaziou
  0 siblings, 0 replies; 11+ messages in thread
From: Nicolas Goaziou @ 2012-11-18 19:12 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Rasmus <rasmus@gmx.us> writes:

>> Then the backend can add the alignment as an optional argument in math
>> mode only when the name of the environment ends with a star and the
>> alignment string is one character long.
>
> In the LATEX_ATTR or as in the example above?  The former I assume.

Correct.

>> To circumvent the problem, org-e-latex.el can use a defconst to store
>> table/matrix environments requiring "\cr" instead of "\\" at the end of
>> each row. It will, as a starter, only contain "bordermatrix".
>
> OK.  How about just making a list of strings (with typical member
> STRING) which are known to have the format
> \STRING{matrix-like-output}?  Then one can add whichever crazy
> environment that might be needed to this list.  And users can easily
> add their favorite flavor of e.g. bordermatrix.

That's the idea: a list of strings which are used as commands and not as
environments, since environments use "\\".

> A short experiment seems to suggest that \cr works in place of \\.

"\\" is high-level, "\cr" is not. There are certainly drawbacks to use
replace \\ with \cr.

>> Also, instead of using a single `org-e-latex-default-table-environment',
>> its value can be a plist like the following:
>>
>>    (:table-mode "tabular" :math-mode "bmatrix" :inline-math-mode "smallmatrix")
>
> Yeah, that would be nice.  Would it extend to the options in
> ATTR_LATEX?  I.e. would it be nice (?) to be able to specify
>    - environment
>    - math-environment
>    - inline-math-environment
> in one option line s.t. I could easily switch from table to matrix to
> inline matrix?  E.g.
>
> #+ATTR_LATEX: :math-environment "Bmatrix" :math-mode t
> prints a display matrix 
> but 
> #+ATTR_LATEX: :math-environment "Bmatrix" :math-mode nil
> prints a table.
>
> I don't know whether this is just making things more confusing or
> whether it would be useful. . . 

That would be too much properties to my liking. There are already many
of them in tables.

> Also, at least a prefix string would be very useful as that's how
> matrices are usually named.  E.g. 
>
> #+ATTR_LATEX: :math-mode t :prefix "P="
> |1|2|
> |3|4|
>
> is translated to 
> P=\begin{bmatrix}1&2\\3&4\end{bmatrix}

Sure :math-prefix and :math-suffix seem like a good idea.


Regards,

-- 
Nicolas Goaziou

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

end of thread, other threads:[~2012-11-18 19:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-14 14:49 Export tables as matrices (change tbl-export function on the fly) Rasmus
2012-11-14 16:21 ` Nicolas Goaziou
2012-11-14 18:05   ` Rasmus
2012-11-17 13:26   ` Rasmus Pank Roulund
2012-11-17 15:32     ` Nicolas Goaziou
2012-11-17 18:29       ` Rasmus
2012-11-18  8:53         ` Nicolas Goaziou
2012-11-18 11:50           ` Rasmus
2012-11-18 13:20             ` Nicolas Goaziou
2012-11-18 14:05               ` Rasmus
2012-11-18 19:12                 ` Nicolas Goaziou

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