help-octave
[Top][All Lists]
Advanced

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

Re: putting a matrix in LaTeX-format [newbie]


From: BVBA NuKey Music
Subject: Re: putting a matrix in LaTeX-format [newbie]
Date: Tue, 20 Dec 2011 11:23:36 +0100

I finally got it working correctly like this:
function printlatexmatrix(data,filename)
%use as follows: printlatexmatrix ([1 0; 0 1], "example.tex")
 f = fopen(filename,"w");
 %%matrix format
 fprintf(f,"\\begin{displaymath} \n");
 fprintf(f,"\\mathbf{X} = \n");
 fprintf(f,["\\left( \\begin{array} {*{ %d }{c}}\n"

            "  \n \\toprule \n"],\
         columns(data));
 %%row data
 for ii = 1:rows(data)
  fprintf(f,"%.3g",data(ii,1));
  fprintf(f,"& %.3g",data(ii,2:end));
  fprintf(f,"\\\\ \n");
end
 fprintf(f,"\\end{array}\\right)\n");
 fprintf(f,"\\end{displaymath}")
fclose(f);
endfunction

best regards,
nukey




2011/12/18 BVBA NuKey Music <address@hidden>


2011/12/17 c. <address@hidden>

Please keep the list in CC as other might also benefit from the thread

On 17 Dec 2011, at 18:51, BVBA NuKey Music wrote:

> Thank you very much Carlo, could you just tell me what I have to fill
> in for the collheaders and rowheaders?
>
> thanks
> nukey


It's the labels you want to give to each row and column, they should be passed as cell-arryas of strings, e.g.:

printlatextable ({"column 1", "column 2"}, {"row 1", "row 2"}, [1 0; 0 1], "example.tex")

c.
I tried to hack your script (obviously I don't understand all the code), unfortunately I now always get one superfluous &

function printlatexmatrix(data,filename)
%use as follows: printlatexmatrix ([1 0; 0 1], "example.tex")

 f = fopen(filename,"w");

 %%matrix format
 fprintf(f,"\\begin{displaymath} \n");
 fprintf(f,"\\mathbf{X} = \n");
 fprintf(f,["\\left( \\begin{array} { c | *{ %d }{ >\\scriptsize c} }\n"

            "  \n \\toprule \n"],\
         columns(data));

 %%row data

 for ii = 1:rows(data)
   fprintf(f,"& %.3g",data(ii,:));
   fprintf(f,"\\\\ \n");
 end
 fprintf(f,"\\end{array}\\right)\n");
 fprintf(f,"\\end{displaymath}")
fclose(f);

endfunction

e.g. entering the following
 a=[1 2 3 4;5 6 7 8;9 10 11 12]
printlatexmatrix(a, "example.tex")

gives the following result:

\begin{displaymath}
\mathbf{X} =
\left( \begin{array} { c | *{ 4 }{ >\scriptsize c} }
& 1& 2& 3& 4\\
& 5& 6& 7& 8\\
& 9& 10& 11& 12\\
\end{array}\right)
\end{displaymath}







reply via email to

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