help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: table latex output


From: Bill Denney
Subject: Re: table latex output
Date: Sun, 27 Nov 2005 09:17:54 -0500 (EST)

I personally think that the function is more along the lines of something like dlmwrite (it could probably be written as a special case of dlmwrite) and should be kept separate from save. The reason is that there can be rather significant variation in the way that the table is presented in latex and if something is part of the save command it needs to also be able to be loaded.

Bill

On Sun, 27 Nov 2005, Gorazd Brumen wrote:

Thanks a lot, Michael.

It works great. I think you should include it in octave-forge.
I dont think name is so important (latex should be in the name though)
- I would suggest that this is
included in the save command in octave itself - with switch -tex, -latex or -textabular
or something. In my oppinion, this makes more sense.

Maybe somebody else has an oppinion.

Ciao,
Gorazd


Michael Creel wrote:
Gorazd Brumen wrote:

hi,

does there exist a matrix output into latex format?
example: x = [1 2; 3 4] and when you save one gets

1 & 2
\\
3 & 4

this would be a great feature, since these things go
directly into .latex documents? i had to save as text,
import into gnumeric, export as text with another separator
and manually correct the file a bit.

regards,
gorazd


This question came up not long ago. The attached file will do this. I can add this to o-f, and in fact Paul K. and I were discussing names for the function. I forget what we agreed upon. Maybe matrix2latex.m? If anyone has an opinion, let me know.


------------------------------------------------------------------------

## Copyright (C) 1996, 1997 Andreas Weingessel 2005 Michael Creel <address@hidden>
## ## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
## ## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details. ## ## You should have received a copy of the GNU General Public License
## along with this file.  If not, write to the Free Software Foundation,
## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

## usage:  save2tex (X, file, cname, rname, prec, align, lines)
##
## Saves the data of the matrix X in a latex tabular environment to
## file. cname and rname are matrices whose rows are the column
## respectively row headings, if c/rname == "" (default), no headings
## are printed. prec gives the number of digits printed after the
## comma. If prec<0 (default) the number is printed unformatted. align
## gives the alignment of the data, default is "r" (flush right). If
## lines == 0 or == "none", no lines are printed in the
## tabular; if lines == 2 or == "full" there are lines around each cell,
## otherwise (default) there are lines at the border and between the headings and
## the data.
##
## Example:
## a = rand(3,2);
## cnames = str2mat("c1, "c2");
## rnames = str2mat("r1, "r2", "r3");
## save2tex(a, "test.tex", cnames, rnames); ## Author: AW <address@hidden>
## Author:  MC <address@hidden> (minor format modifications)

## Description:  Save to a file in a LaTeX tabular environment

function save2tex(tabledata, file, cname, rname, prec, align, lines)

        if ((nargin == 1) || (nargin > 7))
printf("usage: save2tex (X, file, cname, rname, prec, align, lines)\n");
        endif

        if (nargin < 7) lines = 1; endif
        if (nargin < 6) align = "r"; endif
        if (nargin < 5) prec = -1; endif
        if (nargin < 4) rname = ""; endif
        if (nargin < 3) cname = ""; endif

        if (isstr (lines))
                if (strcmp (lines, "full"))
                        lines = 2;
                elseif (strcmp (lines, "none"))
                        lines = 0;
                else
                        lines = 1;
                endif
        endif

        nr = rows(tabledata);
        nc = columns(tabledata);

        is_rn = columns(rname);
        is_cn = columns(cname);

        if ((is_rn) && (rows(rname) != nr))
error ("save2tex: Numbers of rows and row names do not match.");
        endif

        if ((is_cn) && (rows(cname) != nc))
error ("save2tex: Numbers of columns and column names do not match.");
        endif


        ### open output file
        FN = fopen (file, "w");
        if (FN < 0)
                error ("save2tex: Can not open File %s", file);
        endif

        ### create format line
        LSTR = "";
        LFSTR = "";
#       if (lines)
#               LSTR = "|";
#               if (lines == 2)
#                       LFSTR = "|";
#               endif
#       endif
        STR = ["\\begin{tabular}{", LSTR];
        if (is_rn)
                STR = [STR, "l", LSTR];
        endif

        for i=1:nc
                STR = [STR, align, LFSTR];
        endfor
                if (lines == 2)
                STR = [STR, "}\n"];
        else
                STR = [STR, LSTR, "}\n"];
        endif
        fprintf (FN, STR);
                if (lines)
                fprintf (FN, "\\hline\n");
        endif

                ### print column headers
        if (is_cn)
                if (is_rn)
                        STR = "  & ";
                else
                        STR = "  ";
                endif
                                for i = 1:nc
                        STR = [STR, cname(i,:), " & "];
                endfor
                                les=length(STR);
                fprintf(FN, [STR(1:les-2), "\\\\\n"]);
                                if (lines)
                        fprintf (FN, "\\hline\\hline\n");
                endif
        endif

        ### print data
        for i = 1:nr
                if (is_rn)
                        STR = [rname(i,:), "  & "];
                else
                        STR = "  ";
                endif
                                if (prec >= 0)
                        form = sprintf("%%.%df & ", prec);
                else
                        form = "%f & ";
                endif
                                for j = 1:nc
                        STR = [STR, sprintf(form,tabledata(i,j))];
                endfor
                                les=length(STR);
                                if (i < nr)
                        if (lines == 2)
fprintf(FN, [STR(1:les-2), "\\\\\n\\hline\n"]);
                        else
                                fprintf(FN, [STR(1:les-2), "\\\\\n"]);
                        endif
                else
                        if (lines)
fprintf(FN, [STR(1:les-2), "\\\\\n\\hline\n"]);
                        else
                                fprintf(FN, [STR(1:les-2), "\n"]);
                        endif
                endif
        endfor
                ### finish output
        fprintf(FN, "\\end{tabular}\n");
        fclose(FN);

endfunction

--
Gorazd Brumen
Mail: address@hidden
WWW: http://valjhun.fmf.uni-lj.si/~brumen
PGP: Key at http://pgp.mit.edu, ID BCC93240



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


--
"That's right. Being a kernel maintainer suddenly turns your sex appeal
up by about 1000% for some magic reason."
  -- Linus Torvalds



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]