function tab=textab1(A,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,title)
%TEXTAB1   Conversion of MATLAB tables to TEX tables.
%          tab=textab1(A,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,
%          title) converts a numerical (lxm) matrix A in MATLAB to a table 
%          padded with the appropriate code for TEX.  If ntype=0, n decimal 
%          places will be displayed.  Otherwise, n significant digits
%          will be displayed.  (Note: this holds for all elements of A.)
%          The form that the table can take is:
%
%          ---------------------------------------------------------------
%          |rlabel1(1,:)|                   clabel1                      |
%          |            |-------------------------------------------------
%          |rlabel1(2,:)|       clabel2(1,:)       |clabel2(2,:)|  ...   |
%          |            |-------------------------------------------------
%          |rlabel1(3,:)| clabel3(1,:)|clabel3(2,:)|clabel3(3,:)|  ...   |
%          |--------------------------------------------------------------
%          |rlabel2(1,:)|    A(1,1)   |   A(1,2)   |   A(1,3)   |  ...   |
%          ---------------------------------------------------------------
%          |rlabel2(2,:)|    A(2,1)   |   A(2,2)   |   A(2,3)   |  ...   |
%          ---------------------------------------------------------------
%          |    :              :             :            :         :    |
%          ---------------------------------------------------------------
%                              *** title is put here ***
%
%          where 
%
%            clabel1 = a row string used in the top label which will span 
%                      all columns of A, e.g.  'The Header'
%            clabel2 = a string matrix with m or fewer (since A is l x m) 
%                      rows, the first two columns of each row being in the
%                      set of characters {' ','1','2', ..,'8','9'}, and
%                      giving the number of columns that that label will 
%                      span, e.g.
%                      clabel2=['12 cols 1 to 12 ';  Note: these labels will
%                               '3  cols 13 to 16';  appear without the
%                               ' 1 col  17      ']  first two characters
%            clabel3 = a string matrix with m rows used in the bottom
%                      column labels
%            rlabel1 = a string matrix with 3 rows to be used for 
%                      labeling the rows
%            rlabel2 = a string matrix with l rows corresponding to
%                      rows of A
%            title   = a string with the title for the table
%
%          NOTE: If any of the row or column labels are not desired, set 
%          the option to 0.  For example, if rlabel1=0 and rlabel2=0,
%          there will only be labels for the columns.
%
%          Textab1 returns the matrix string tab.
%
%          See also TEXTAB2, TEXMAT, MATTAB1, MATTAB2.
%

%          Ellen R. McGrattan, 10-13-87
%          Revised, 7-5-89,  ERM
%
[l,m]=size(A);
if max(max(imag(A)~=zeros(l,m)));
  disp('Warning: complex number encountered. Code needs updating for this.');
end;
if isstr(rlabel2);
  [srow,scol]=size(rlabel2);
  if srow<l; error('The second set of row labels have too few rows'); end;
  if srow>l; rlabel2=rlabel2(1:l,:); end;
  if ~isstr(rlabel1); rlabel1=[' ';' ';' ']; end;
else;
  if isstr(rlabel1);
    rlabel2=setstr(32*ones(l,1));
  end;
end;
if isstr(rlabel1);
  [srow,scol]=size(rlabel1);
  x=isstr(clabel1)+isstr(clabel2)+isstr(clabel3);
  if srow< x;
    error('The first set of row labels have too few rows')
  end;
end;
if isstr(clabel2);
  [srow,scol]=size(clabel2);
  no2=[ ];
  for i=1:srow;
    eval(['no2=[no2;',clabel2(i,1:2),'];'])
  end;
  sum2=sum(no2);
  if sum2<m; 
    error('The second set of column labels have too few rows.')
  end;
  if sum2>m;
    disp('WARNING: Not all of your second column labels are being used.')
    x=[ ];
    for i=1:srow
      y=sum(no2(1:i));
      x=[x;clabel2(i,:)];
      if y==m;
        break;
      elseif y>m;
        no2(i)=m-sum(no2(1:i-1));
        break;
      end;
    end;
    clabel2=x;
    [srow,scol]=size(clabel2);
    no2=no2(1:srow);
  end;
  clabel2=clabel2(:,3:scol);
else;
  no2=m;
end;
header=['\centerline{\vbox{\tabskip=0pt\offinterlineskip   ';  
        '\def\tablerule{\noalign{\hrule}}                  ';
        '\def\widetablerule{\noalign{\hrule height1pt}}    ';
        '\def\widehrulefill{\leaders\hrule height1pt\hfill}';
        '\halign to',int2str(50*(isstr(rlabel1)+m+1)),  ...
                     'pt{\tabskip=1em plus2em              ';
        '\vrule width1pt#&\hfil#&                          '];
if isstr(rlabel2);
  header=[header;
        '\vrule width1pt#&\hfil#&                          '];
end;
x=length(no2);
for i=1:x;
  for j=1:no2(i)-1;
    header=[header;
           '\vrule#&\hfil#&                                   '];
  end;
  if i<x;  
    header=[header;
           '\vrule width1pt#&\hfil#&                          '];
  else;
    header=[header;
           '\vrule width1pt#\tabskip=0pt\cr\widetablerule     '];
  end;
end;
if isstr(clabel1);
  header=[header;'\strut&                                           '];
  if isstr(rlabel1);
    str=['      ',rlabel1(1,:),'&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \multispan{',int2str(2*m-1),'}\hfil ',clabel1,'\hfil&\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
  if isstr(rlabel1);
    [hrow,hcol]=size(header);
    header=[header;'      &&',setstr(32*ones(1,hcol-8))];
  end;
  str=['      \multispan{',int2str(2*m+1),'}\widehrulefill\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;
if isstr(clabel2);
  [hrow,hcol]=size(header);
  header=[header;'\strut&',setstr(32*ones(1,hcol-7))];
  if isstr(rlabel1);
    if isstr(clabel1);
      str=['      ',rlabel1(2,:),'&&'];
    else;
      str=['      ',rlabel1(1,:),'&&'];
    end;
    [srow,scol]=size(str);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;

  x=length(no2);
  for i=1:x-1;
    str=['      \multispan{',int2str(2*no2(i)-1),'}\hidewidth ', ...
         clabel2(i,:),'\hidewidth&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \multispan{',int2str(2*no2(x)-1),'}\hidewidth ', ...
       clabel2(x,:),'\hidewidth&\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
  if isstr(rlabel1);
    [hrow,hcol]=size(header);
    header=[header;'      &&',setstr(32*ones(1,hcol-8))];
  end;
  str=['      \multispan{',int2str(2*m+1),'}\widehrulefill\cr'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;

if isstr(clabel3);
  [hrow,hcol]=size(header);
  header=[header;'\strut&',setstr(32*ones(1,hcol-7))];
  if isstr(rlabel1);
    if (~isstr(clabel1) & ~isstr(clabel2));
      str=['      ',rlabel1(1,:),'&&'];
    elseif ((isstr(clabel1) & ~isstr(clabel2))  ...
          | (isstr(clabel2) & ~isstr(clabel1)));
      str=['      ',rlabel1(2,:),'&&'];
    else;
      str=['      ',rlabel1(3,:),'&&'];
    end;
    [srow,scol]=size(str);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  for i=1:m-1;
    str=['      \omit\hidewidth ',clabel3(i,:),'\hidewidth&&'];
    [srow,scol]=size(str);
    [hrow,hcol]=size(header);
    header=[header,setstr(32*ones(hrow,scol-hcol));
            str,setstr(32*ones(1,hcol-scol))];
  end;
  str=['      \omit\hidewidth ',clabel3(m,:),'\hidewidth&\cr\widetablerule'];
  [srow,scol]=size(str);
  [hrow,hcol]=size(header);
  header=[header,setstr(32*ones(hrow,scol-hcol));
          str,setstr(32*ones(1,hcol-scol))];
end;
tab=' ';
if ntype;
  a=abs(A);
  a=(a==0)+(a~=0).*a;
  B=floor(log10(a));
  for i=1:l;
    for j=1:m;
      x=10^B(i,j)/10^(n-1);
      C(i,j)=round(A(i,j)/x)*x;
    end;
  end;
  c=abs(C);
  c=(c==0)+(c~=0).*c;
  B=floor(log10(c));
  for i=1:l;
    for j=1:m;
      if B(i,j)-n+1>=0;
        D(i,j)=1;
      elseif B(i,j)<0;
        D(i,j)=-1;
      else;
        D(i,j)=0;
      end;
    end;
  end;
  E=(10).^(-B+n-1).*C;
  for i=1:l;
    str='';
    for j=1:m;
      if D(i,j)==1; 
        str=[str,'&& ',int2str(C(i,j))];
      elseif D(i,j)==0;
        x=10^(n-1-B(i,j))*(C(i,j)-fix(C(i,j)));
        count=0;
        for k=1:n-2-B(i,j);
          if abs(x)<10^k; count=count+1; end;
        end;
        str=[str,'&& ',int2str(fix(C(i,j))),'.',setstr(ones(1,count)*48), ...
             int2str(abs(x))];
      else;
        if A(i,j)>=0;
          str=[str,'&& 0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j)))];
        else;
          str=[str,'&& -0.',setstr(ones(1,-B(i,j)-1)*48),int2str(abs(E(i,j)))];
        end;
      end;
    end;
    if i<l;
      str=[str,'&\cr\tablerule'];
    else;
      str=[str,'&\cr\widetablerule}}}'];
    end;
    [hrow,hcol]=size(tab);
    [srow,scol]=size(str);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         str,setstr(32*ones(1,hcol-scol))];
  end;
else;
  B=fix(10^n*A+.5*sign(A))*10^(-n);
  C=abs(fix(B));
  D=abs(round(10^n*(B-fix(B))));
  for i=1:l;
    str='';
    for j=1:m;
      sc=int2str(D(i,j));
      x=length(sc);
      if x<n; sc=[setstr(48*ones(1,n-x)),sc]; end;
      if A(i,j)>=0; 
        str=[str,'&& ',int2str(C(i,j)),'.',sc];
      else;
        str=[str,'&&-',int2str(C(i,j)),'.',sc];
      end;
    end;
    if i<l;
      str=[str,'&\cr\tablerule'];
    else;
      str=[str,'&\cr\widetablerule}}}'];
    end;
    [hrow,hcol]=size(tab);
    [srow,scol]=size(str);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         str,setstr(32*ones(1,hcol-scol))];
  end;
end;
tab=tab(2:l+1,:);
[srow,scol]=size(tab);
if isstr(rlabel2);
  x=abs('\strut&');
  tab=[setstr(ones(srow,1)*x),rlabel2,tab];
else;
  x=abs('\strut');
  tab=[setstr(ones(srow,1)*x),tab(:,2:scol)];
end;
[srow,scol]=size(tab);
[hrow,hcol]=size(header);
tab=[header,setstr(32*ones(hrow,scol-hcol));
     tab,setstr(32*ones(srow,hcol-scol))];
if ~isstr(title); title=' '; end;
str=['\centerline{',title,' }'];
[hrow,hcol]=size(tab);
[srow,scol]=size(str);
x=max(hcol,scol);
tab=[tab,setstr(32*ones(hrow,scol-hcol));
     '\medskip',setstr(32*ones(1,x-8));
     str,setstr(32*ones(1,hcol-scol));
     '\vfill\eject',setstr(32*ones(1,x-12))];
