*
* @RegToTeX( options )
* takes the most recent regression and produces a TeX equation showing
* the regression information in equation form with coefficients on the
* main line and standard errors in () below them, aligned at the decimal
* point.
*
* Options:
*   PICTURE=picture format for numbers ["*.##"]
*   DVLABEL=TeX code label for dependent variable [series label_t]
*   RHSLABELS=VEC[STRING] of TeX code labels for the right hand side
*       variables [series label_t or series label_{t +/- lag) for lags/leads]
*
* Revision Schedule:
*  10/2011 Written by Tom Doan, Estima
*
* Forward reference for %SplitAtDot
*
function %SplitAtDot value picture
type vect[strings] %SplitAtDot
type real value
type string picture
end
*
* Main procedure
*
procedure RegToTeX
*
option string  picture "*.##"
option vect[strings] rhslabels
option string  dvlabel
*
local integer 	i
local string 	thislabel prefix
local vect[strings] parts
local rect[integer] etable
local string piclocal
*
display "% Regression equation generated by RegToTeX"
display "\begin{equation}"
display "\begin{aligned}"
*
* Dependent variable
*
display "\begin{array}{l}"
if %defined(dvlabel)
   display dvlabel "\\ \\"
else
   display %l(%eqndepvar(0))+"_t" "\\ \\"
display "\end{array}"
*
compute etable=%eqntable(0)
do i=1,%eqnsize(0)
   *
   * Figure out the label for the variable.
   *
   if etable(1,i)==0
      compute thislabel=""
   else
   if %defined(rhslabels).and.%rows(rhslabels)>=i
      compute thislabel=rhslabels(i)
   else
   if etable(2,i)==0
      compute thislabel=%l(etable(1,i))+"_t"
   else
   if etable(2,i)>0
      compute thislabel=%l(etable(1,i))+"_{t-"+etable(2,i)+"}"
   else
      compute thislabel=%l(etable(1,i))+"_{t+"+(-etable(2,i))+"}"
   *
   * Figure out the prefix or operator
   *
   if i==1
      compute prefix="="
   else
   if %beta(i)>0
      compute prefix="+"
   else
      compute prefix="-"
   *
   * Split up the absolute value of the coefficient at the decimal
   * point. If there is no decimal point, right align the number and
   * left align the label. If there is, right align the integer part,
   * and left align to the . the decimal part concatenated with the
   * label.
   *
   compute parts=%SplitAtDot(abs(%beta(i)),picture)
   if %rows(parts)==1 {
      display "\begin{array}{r l}"
      display prefix parts(1) "&" "\," thislabel "\\"
   }
   else {
      display "\begin{array}{r @{.} l}"
      display prefix parts(1) "&" parts(2) "\," thislabel "\\"
   }
   *
   * Do the same thing with the standard error
   *
   compute parts=%SplitAtDot(%stderrs(i),picture)
   if %rows(parts)==1 {
      display "(" parts(1) ")"
      display "\end{array}"
   }
   else {
      display "(" parts(1) "&" parts(2) ")"
      display "\end{array}"
   }
end do i
display "\end{aligned}"
display "\end{equation}"
display "% end of equation"
end
*
* Working code for %SplitAtDot
*
function %SplitAtDot value picture
type vect[strings] %SplitAtDot
type real value
type string picture
*
local string vstring
local integer len i
local string  vstring
*
compute vstring=%strval(value,picture)
*
compute len =%strlen(vstring)
do i=1,len
   if %mid(vstring,i,1)=="." {
      compute %SplitAtDot=||%left(vstring,i-1),%mid(vstring,i+1,-1)||
      return
   }
end do i
compute %SplitAtDot=||vstring||
end

