Using data from tables in interactive sessions

Hi, emacs’ org-mode alows me to use the data from tables in text files in code blocks. This way I can type the data into a table and then feed it to the source block to plot it.
Is it possible, to take the data I have typed into a tabular in TeXmacs and feed it to the interactive python session?

While TeXmacs has some spreadsheet functionality, I do not think it will do what you require here. But you can do anything, in particular write a small scheme function which collect data from a table in the document and feed it to python code.

I give here an example. Assume your document looks like:


I,e. there is a table (and only one). Then you can use scheme to extract the table and create a well formatted string to send to python. Let’s see how to do it.

First we get the table as a scheme tree and extract the table.

We can use select to get the value from the second column:


(you need only the last command, I wrote how I obtained it by trying to narrow down the selection)
At this point you need a small function which format for you these values:

And done. :slight_smile:

I attach the texmacs document for reference.

<TeXmacs|2.1.2>

<style|<tuple|generic|british>>

<\body>
  <tabular|<tformat|<cwith|1|-1|1|-1|cell-tborder|1ln>|<cwith|1|-1|1|-1|cell-bborder|1ln>|<cwith|1|-1|1|-1|cell-lborder|1ln>|<cwith|1|-1|1|-1|cell-rborder|1ln>|<table|<row|<cell|<with|font-series|bold|A>>|<cell|<with|font-series|bold|B>>>|<row|<cell|1>|<cell|4>>|<row|<cell|2>|<cell|6>>|<row|<cell|3>|<cell|8>>|<row|<cell|4>|<cell|9>>|<row|<cell|5>|<cell|2>>|<row|<cell|6>|<cell|4>>>>>

  \;

  <\session|scheme|default>
    <\input|Scheme] >
      (define t (tree-\<gtr\>stree (buffer-tree)))
    </input>

    <\unfolded-io|Scheme] >
      (select t `(tabular))
    <|unfolded-io>
      ((tabular (tformat (cwith "1" "-1" "1" "-1" "cell-tborder" "1ln")
      (cwith "1" "-1" "1" "-1" "cell-bborder" "1ln") (cwith "1" "-1" "1" "-1"
      "cell-lborder" "1ln") (cwith "1" "-1" "1" "-1" "cell-rborder" "1ln")
      (table (row (cell (with "font-series" "bold" "A")) (cell (with
      "font-series" "bold" "B"))) (row (cell "1") (cell "4")) (row (cell "2")
      (cell "6")) (row (cell "3") (cell "8")) (row (cell "4") (cell "9"))
      (row (cell "5") (cell "2")) (row (cell "6") (cell "4"))))))
    </unfolded-io>

    <\unfolded-io|Scheme] >
      (select t `(tabular :*))
    <|unfolded-io>
      ((tabular (tformat (cwith "1" "-1" "1" "-1" "cell-tborder" "1ln")
      (cwith "1" "-1" "1" "-1" "cell-bborder" "1ln") (cwith "1" "-1" "1" "-1"
      "cell-lborder" "1ln") (cwith "1" "-1" "1" "-1" "cell-rborder" "1ln")
      (table (row (cell (with "font-series" "bold" "A")) (cell (with
      "font-series" "bold" "B"))) (row (cell "1") (cell "4")) (row (cell "2")
      (cell "6")) (row (cell "3") (cell "8")) (row (cell "4") (cell "9"))
      (row (cell "5") (cell "2")) (row (cell "6") (cell "4"))))) (tformat
      (cwith "1" "-1" "1" "-1" "cell-tborder" "1ln") (cwith "1" "-1" "1" "-1"
      "cell-bborder" "1ln") (cwith "1" "-1" "1" "-1" "cell-lborder" "1ln")
      (cwith "1" "-1" "1" "-1" "cell-rborder" "1ln") (table (row (cell (with
      "font-series" "bold" "A")) (cell (with "font-series" "bold" "B"))) (row
      (cell "1") (cell "4")) (row (cell "2") (cell "6")) (row (cell "3")
      (cell "8")) (row (cell "4") (cell "9")) (row (cell "5") (cell "2"))
      (row (cell "6") (cell "4")))) (cwith "1" "-1" "1" "-1" "cell-tborder"
      "1ln") "1" "-1" "1" "-1" "cell-tborder" "1ln" (cwith "1" "-1" "1" "-1"
      "cell-bborder" "1ln") "1" "-1" "1" "-1" "cell-bborder" "1ln" (cwith "1"
      "-1" "1" "-1" "cell-lborder" "1ln") "1" "-1" "1" "-1" "cell-lborder"
      "1ln" (cwith "1" "-1" "1" "-1" "cell-rborder" "1ln") "1" "-1" "1" "-1"
      "cell-rborder" "1ln" (table (row (cell (with "font-series" "bold" "A"))
      (cell (with "font-series" "bold" "B"))) (row (cell "1") (cell "4"))
      (row (cell "2") (cell "6")) (row (cell "3") (cell "8")) (row (cell "4")
      (cell "9")) (row (cell "5") (cell "2")) (row (cell "6") (cell "4")))
      (row (cell (with "font-series" "bold" "A")) (cell (with "font-series"
      "bold" "B"))) (cell (with "font-series" "bold" "A")) (with
      "font-series" "bold" "A") "font-series" "bold" "A" (cell (with
      "font-series" "bold" "B")) (with "font-series" "bold" "B")
      "font-series" "bold" "B" (row (cell "1") (cell "4")) (cell "1") "1"
      (cell "4") "4" (row (cell "2") (cell "6")) (cell "2") "2" (cell "6")
      "6" (row (cell "3") (cell "8")) (cell "3") "3" (cell "8") "8" (row
      (cell "4") (cell "9")) (cell "4") "4" (cell "9") "9" (row (cell "5")
      (cell "2")) (cell "5") "5" (cell "2") "2" (row (cell "6") (cell "4"))
      (cell "6") "6" (cell "4") "4")
    </unfolded-io>

    <\unfolded-io|Scheme] >
      (select t `(tabular :* row))
    <|unfolded-io>
      ((row (cell "A") (cell "B")) (row (cell "1") (cell "4")) (row (cell
      "2") (cell "6")) (row (cell "3") (cell "8")) (row (cell "4") (cell
      "9")) (row (cell "5") (cell "2")) (row (cell "6") (cell "4")))
    </unfolded-io>

    <\unfolded-io|Scheme] >
      (select t `(tabular :* row 0))
    <|unfolded-io>
      ((cell (with "font-series" "bold" "A")) (cell "1") (cell "2") (cell
      "3") (cell "4") (cell "5") (cell "6"))
    </unfolded-io>

    <\unfolded-io|Scheme] >
      (select t `(tabular :* row 1 0))
    <|unfolded-io>
      ((with "font-series" "bold" "B") "4" "6" "8" "9" "2" "4")
    </unfolded-io>

    <\unfolded-io|Scheme] >
      (cdr (select t `(tabular :* row 1 0)))
    <|unfolded-io>
      ("4" "6" "8" "9" "2" "4")
    </unfolded-io>

    <\input|Scheme] >
      (define (fmt-list l)

      \ \ (define (f a) (if (null? a) "]"\ 

      \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (string-append "," (car a) (f
      (cdr a))) ))

      \ \ (string-append "[" (car l) (f (cdr l))))
    </input>

    <\unfolded-io|Scheme] >
      (fmt-list (cdr (select t `(tabular :* row 1 0))))
    <|unfolded-io>
      "[4,6,8,9,2,4]"
    </unfolded-io>

    <\input|Scheme] >
      \;
    </input>
  </session>
</body>

<initial|<\collection>
</collection>>
3 Likes

If there are several tables I think one can distinguish between them with an additional tag

Yes, that’s a good idea. One could make a new macro with two arguments, a label and the table. Then use select to find the macro with the right label and extract the table from there.

Thanks. That looks nice. Is it possible to plot the tabular in scheme? That way I don’t have to copy it to python.

You can embed Python plots in TeXmacs, pls. see Sketches and plots in Texmacs for examples.

Edit: there is a tool for graphing in TeXmacs (inside a drawing press the ! key), but I know how to use it for graphing functions only. It does not draw axes.
One can program in Scheme plotting of tables, I think it is doable perhaps in one or two hours if you limit the options that you give to the user to “no options” (like Ford was limiting the color of the cars he sold to “black”).

What do you have in mind?

Where do we find a reference for these codes? For example, a reference for select?

Basic things are explained in the online manual Help->Scheme extensions and Full manuals->Developer guide and Full manuals-> Scheme developer guide.

Anyway the following could also be helpful: how to make active cells with python.