function tab=textab2b(M,S,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,title)
%TEXTAB2   Conversion of MATLAB tables to TEX tables.
%          tab=textab2(M,S,n,ntype,clabel1,clabel2,clabel3,rlabel1,rlabel2,
%          title) converts numerical (lxm) matrices M and S 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 M,S.)
%          If any element of M or S is `NaN', then no number appears.
%          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,:)|    M(1,1)   |   M(1,2)   |   M(1,3)   |  ...   |
%          |            |  ( S(1,1) ) | ( S(1,2) ) | ( S(1,3) ) |  ...   |
%          ---------------------------------------------------------------
%          |rlabel2(2,:)|    M(2,1)   |   M(2,2)   |   M(2,3)   |  ...   |
%          |            |  ( S(2,1) ) | ( S(2,2) ) | ( S(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.
%
%          Textab2 returns the matrix string tab.
%
%          See also TEXTAB1, TEXMAT, MATTAB1, MATTAB2.
%

%          Ellen R. McGrattan, 10-13-87
%          Revised, 7-29-97,  ERM
%
[l,m]=size(M);
[srow,scol]=size(S);
if srow~=l | scol~=m;
  error('M and S must have the same dimensions')
end;
if max(max(imag(M)~=zeros(l,m))) | max(max(imag(S)~=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;
  tem1=setstr(32*ones(2*l,scol));
  for i=1:l;
    tem1(2*i-1,:)=rlabel2(i,:);
  end;
  rlabel2=tem1;
else;
  if isstr(rlabel1);
    rlabel2=setstr(32*ones(2*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}}                  ';
        '\halign to',int2str(80*(isstr(rlabel1)+m+1)),  ...
                     'pt{\tabskip=1em plus2em              ';
        '#&#\hfil&                                         '];
if isstr(rlabel2);
  header=[header;
        '#&\hfil#&                                         '];
end;
x=length(no2);
for i=1:x;
  for j=1:no2(i)-1;
    header=[header;
           '#&\hfil#&                                         '];
  end;
  if i<x;  
    header=[header;
           '#&\hfil#&                                         '];
  else;
    header=[header;
           '#\tabskip=0pt\cr\tablerule\noalign{\vskip 1pt}    ';
           '\tablerule\noalign{\vskip 3pt}                    '];
  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), ...
       '}\hrulefill\cr\noalign{\vskip 3pt}'];
  [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),'}\hrulefill\cr\noalign{\vskip 3pt}'];
  [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\noalign{\vskip 3pt}\tablerule\noalign{\vskip 3pt}'];
  [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(M);
  aa=abs(S);
  a=(a==0)+(a~=0).*a;
  aa=(aa==0)+(aa~=0).*aa;
  B=floor(log10(a));
  Bb=floor(log10(aa));
  for i=1:l;
    for j=1:m;
      x=10^B(i,j)/10^(n-1);
      y=10^Bb(i,j)/10^(n-1);
      C(i,j)=round(M(i,j)/x)*x;
      Cc(i,j)=round(S(i,j)/y)*y;
    end;
  end;
  c=abs(C);
  cc=abs(Cc);
  c=(c==0)+(c~=0).*c;
  cc=(cc==0)+(cc~=0).*cc;
  B=floor(log10(c));
  Bb=floor(log10(cc));
  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;
      if Bb(i,j)-n+1>=0;
        Dd(i,j)=1;
      elseif Bb(i,j)<0;
        Dd(i,j)=-1;
      else;
        Dd(i,j)=0;
      end;
    end;
  end;
  E=(10).^(-B+n-1).*C;
  Ee=(10).^(-Bb+n-1).*Cc;
  for i=1:l;
    str='';
    strr='';
    for j=1:m;
      if ~isnan(C(i,j));
        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 M(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;
      else
        str=[str,'&& '];
      end;
      if ~isnan(Cc(i,j));
        if Dd(i,j)==1; 
          strr=[strr,'&&$\scriptstyle(',int2str(Cc(i,j)),')$'];
        elseif Dd(i,j)==0;
          x=10^(n-1-Bb(i,j))*(Cc(i,j)-fix(Cc(i,j)));
          count=0;
          for k=1:n-2-Bb(i,j);
            if abs(x)<10^k; count=count+1; end;
          end;
          strr=[strr,'&&$\scriptstyle(',int2str(fix(Cc(i,j))),'.',  ...
                setstr(ones(1,count)*48),int2str(abs(x)),')$'];
        else;
          if S(i,j)>=0;
            strr=[strr,'&&$\scriptstyle(0.',setstr(ones(1,-Bb(i,j)-1)*48), ...
                  int2str(abs(Ee(i,j))),')$'];
          else;
            strr=[strr,'&&$\scriptstyle(-0.',setstr(ones(1,-Bb(i,j)-1)*48), ...
                  int2str(abs(Ee(i,j))),')$'];
          end;
        end;
      else;
        strr=[strr,'&& '];
      end;
    end;
    str=[str,'&\cr \noalign{\vskip -3pt}'];
    if i<l;
      strr=[strr,'&\cr \noalign{\vskip 3pt}'];
    else;
      strr=[strr, ...
       '&\cr\noalign{\vskip 3pt}\tablerule\noalign{\vskip 1pt}\tablerule}}}'];
    end;
    [hrow,hcol]=size(tab);
    [srow,scol]=size(str);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         str,setstr(32*ones(1,hcol-scol))];
    [hrow,hcol]=size(tab);
    [srow,scol]=size(strr);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         strr,setstr(32*ones(1,hcol-scol))];
  end;
else;
  B=fix(10^n*M+.5*sign(M))*10^(-n);
  Bb=fix(10^n*S+.5*sign(S))*10^(-n);
  C=abs(fix(B));
  Cc=abs(fix(Bb));
  D=abs(round(10^n*(B-fix(B))));
  Dd=abs(round(10^n*(Bb-fix(Bb))));
  for i=1:l;
    str='';
    strr='';
    for j=1:m;
      sc=int2str(D(i,j));
      scc=int2str(Dd(i,j));
      x=length(sc);
      y=length(scc);
      if x<n; sc=[setstr(48*ones(1,n-x)),sc]; end;
      if y<n; scc=[setstr(48*ones(1,n-y)),scc]; end;
      if ~isnan(M(i,j))
        if M(i,j)>=0; 
          str=[str,'&& ',int2str(C(i,j)),'.',sc];
        else;
          str=[str,'&&-',int2str(C(i,j)),'.',sc];
        end;
      else
        str=[str,'&& '];
      end;
      if ~isnan(S(i,j));
        if S(i,j)>=0; 
          strr=[strr,'&&$\scriptstyle(',int2str(Cc(i,j)),'.',scc,')$'];
        else;
          strr=[strr,'&&$\scriptstyle(-',int2str(Cc(i,j)),'.',scc,')$'];
        end;
      else
        strr=[strr,'&& '];
      end;
    end;
    str=[str,'&\cr \noalign{\vskip -3pt}'];
    if i<l;
      strr=[strr,'&\cr \noalign{\vskip 3pt}'];
    else;
      strr=[strr, ...
       '&\cr\noalign{\vskip 3pt}\tablerule\noalign{\vskip 1pt}\tablerule}}}'];
    end;
    [hrow,hcol]=size(tab);
    [srow,scol]=size(str);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         str,setstr(32*ones(1,hcol-scol))];
    [hrow,hcol]=size(tab);
    [srow,scol]=size(strr);
    tab=[tab,setstr(32*ones(hrow,scol-hcol));
         strr,setstr(32*ones(1,hcol-scol))];
  end;
end;
tab=tab(2: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))];
