How to import a table from a csv file to TeXmacs?

It’s a large table so I don’t want to type it word by word.

The only solution that I see is writing a program in Scheme that does that; one could keep it simple and refine the appearance of the table in the TeXmacs editor if that is possible - and I do not know if it is, i.e. if one can edit elements that are generated via Scheme inside the editor; otherwise one would need to completely program the table in Scheme. Perhaps someone has a better answer.

As far as I know, currently, TeXmacs does not support CSV file import.

Well, it is a good feature request.

I wrote a small package that reads the contents of a csv file and formats them as a TeXmacs table; I placed it in https://github.com/pireddag/csvTable

It works (I have seen TeXmacs crash once while using it, but I do not know if the reason was the package), but it is too simple to be useful for a practical document, as the table is not formatted.

It could be extended quickly to a usable package if there were a way to insert the TeXmacs code generated by the Scheme code inside the TeXmacs document “as code” instead than “as typeset output” as it is now: in that way, one could import the csv file into a table, then format the table manually. Scheme typed into a Scheme session does that, I do not know how to do it with a package (I do not know if it is possible either).

1 Like

thank you, I’ll try it

I have updated the program so that the table is also formatted. Please let me know if it works for you.

I think it will be possible to get a table formatted with cell backgrounds, spacings and lines according to your wishes (I am not able to change the color of the lines around the table cells). What I still do not know how to do is how to format the text inside the cells: for example make the text in the first column bold. But maybe I will be able to do that too.

To use the program it is necessary to set the security to Accept all scripts. As far as I understand it is a security risk (one could write a script to erase your hard drive), but it is a limited risk as the only damage can come from TeXmacs scripts that you install, so you are safe if you make sure that you install only safe scripts (@sadhen: am I understanding well?).

The one I am giving you seems completely safe to me.

If you can convert the table in XML or HTML, then maybe you can import in TeXmacs. Or also in LaTeX. There could be tools out there for all these instances. For example Google Docs could be able to read CVS and export HTML. TeXmacs then can read it.

The Scheme code that I wrote has the limitation that one cannot edit the typeset output within TeXmacs. On the mailing list JvdH said (if I understood him well and also remember well: I did not find the message anymore) that it is possible to “throw” programmatically TeXmacs “source” into a TeXmacs document. If that were possible, one could apply it to importing a table from a csv file: first read the file into a simple table using a macro based on a Scheme program, then customize it with the TeXmacs editor.
By the way “throwing in the code” would be applicable in general: generate a complex structure using a macro, edit it with the editor.
I have got a hint that one can do this using the “routines for editing documents” in Chapter 3 of the TeXmacs Scheme developer guide but I did not read yet enough to know if the hint is right. Does it make sense to you?

1 Like

This would be nice quality-of-life feature. Retyping table contents is tedious.

plan to work on it later.
See https://github.com/XmacsLabs/mogan/issues/86

1 Like

you can write a simple scheme program which converts csv to texmacs format, should not be complicate if you do not want to handle corner-cases. @pireddag, what is the status of your scheme script? why don’t you put it in tm-forge? (just prepare a PR and I will integrate it).

Please let me take a look again.
If I recall correctly it needs to be rewritten to avoid being able to execute arbitrary Scheme code, but I think I know how to do it, now that I have a bit of experience more.

1 Like

Here’s a solution with an octave bash.
The output file is … “output_file.tm”.
It can be included in your TeXmacs document.
Unix philosophy: Do One Thing and Do It Well
Nicolas

#!/usr/bin/octave -f
% ./csv2tm.m data.csv
%==============================================================================
%
% MODULE : csv2tm.m
% DESCRIPTION : Convert csv file into a TeXmacs file containing the data table
% COPYRIGHT : © 2022 Nicolas Ratier
%
% This software falls under the GNU general public license version 3 or later.
% It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
% in the root directory or http://www.gnu.org/licenses/gpl-3.0.html.
%
%==============================================================================
% data.csv
%-------------------------------------------------
% aaa,bbb
% ccc,ddd
% eee,fff
%-------------------------------------------------
pkg load io
A = csv2cell(argv(){1});

fid = fopen('output_file.tm','w');
fprintf(fid,'%s\n\n','<TeXmacs|2.1.1>');
fprintf(fid,'%s\n\n','<style|<tuple|generic|british>>');
fprintf(fid,'%s\n','<\body>');
fprintf(fid,'%s','<block|<tformat|');
tmtable(A, fid);
fprintf(fid,'%s\n','>>');
fprintf(fid,'%s\n','</body>');
fclose(fid);

function tmtable(A,fid)
  [M,N] = size(A);
  fprintf(fid,'%s','<table|');
  for m=1:M-1,
    fprintf(fid,'%s','<row|');
    for n=1:N-1,
      fprintf(fid,'<cell|%s>|',A{m,n});
    end
    fprintf(fid,'<cell|%s>>|',A{m,N});
  end
  fprintf(fid,'%s','<row|');
  for n=1:N-1,
    fprintf(fid,'<cell|%s>|',A{M,n});
  end
  fprintf(fid,'<cell|%s>>>',A{M,N});
endfunction
1 Like

I have written the program in a different way, so that it inserts a table at the cursor using the contents of a file chosen by the user. I will upload it to GitHub and then I will need to see how to make it safe—that it does not crash documents: maybe it is now, but I do not know, perhaps I have to limit the type of trees where one can insert a table.
The advantage of inserting using a keyboard shortcut, rather than typesetting using a macro, is that by inserting then one can apply to the inserted table the formatting they like using interactive commands, rather than through a file written using the TeXmacs Scheme syntax

The files are at https://github.com/pireddag/csvTable/tree/simple
As soon as I am convinced that it works reasonably I will open a pull request.

2 Likes

I would like to limit the trees in which one can insert a table to the trees with label document; I think that it is an ok way of doing for a first implementation of the code for inserting tables, so that I do not have to analyze which kind of trees are ok for inserting a table.
If the user invokes the table insertion command in a tree different from that, I would like to issue an error message.
Would anyone know how to display an error message in one of the consoles? I read in the Scheme developer manual that I can issue a message in the footer with set-message, but I need a promiment display. I tried texmacs-error but I could not make it work.

Pull request sent, let us see if the little program helps.