*! version 1.0.5  17jul2010  by Marc-Andreas Muendler  muendler@ucsd.edu
*! version 1.0.4  15jun2006  by Marc-Andreas Muendler  muendler@ucsd.edu

program define est2extlbl
  version 7
  syntax [, ADDto(string) REPLACE Saving DROPALL Path(string) INtex(string) RETain]

  if "$S_OS" == "MacOS" {
    local dirsep = ":"
    }
  else {
    local dirsep = "/"
    }
  if (substr("`path'",-1,1)~="\" & substr("`path'",-1,1)~="/" & substr("`path'",-1,1)~=":") & "`path'"~="" {
    local path "`path'`dirsep'"
    }
  local outfile "`path'`addto'_rowlbl.dta"
  if "`replace'"~="replace" {
    confirm new file "`outfile'"
    }
  if "`addto'"=="" {
    disp as err "File addto expected"
    error 7
    }
  if "`intex'"=="" & "`retain'"=="" {
    disp as err "Either intex() file or retain option must be specified"
    error
    }
  if substr("`intex'",-4,4)~=".tex" {
    local intex "`intex'.tex"
    }
  local infile "`intex'"
  if substr("`outdta'",-7,7)~="_rowlbl" & substr("`outdta'",-11,11)~="_rowlbl.dta" {
    local outdta "`outdta'_rowlbl.dta"
    }
  if "`retain'"~="" {
    confirm string variable _rowname _rowlbl
    }

  local saved 0
  if "`retain'"=="" {
    local currN = _N
    if `currN'>0 & "`saving'"~="saving" & "`dropall'"~="dropall" {
      error 4
      exit
      }
    if "`saving'"=="saving" & `currN'>0 {
      local dropall = ""
      tempfile tmp
      quietly save `tmp'
      local saved 1
      disp in green "data in memory will be removed temporarily, and restored later"
      disp in green "Press any key to continue, or Break to abort"
      more
      drop _all
      }
    confirm file "`infile'"
    if "`dropall'"=="dropall" & `currN'>0 {
      disp in green "data in memory will be dropped"
      disp in green "Press any key to continue, or Break to abort"
      more
      drop _all
      }
    capture insheet using "`infile'", delimiter("&")
    capture drop if substr(v1,1,1)=="%"
    capture rename v1 _rowname
    capture rename v2 _rowlbl
    capture confirm new variable v3
    if _rc~=0 {
      disp as err "additional column found in &-separated TeX table where two columns expected"
      list if v3~=""
      disp as err "(_rowlbl likely includes ampersand &)"
      disp
      * error 7
      drop v3
      }
    capture {
      capture replace _rowname = trim(_rowname)
      replace _rowlbl = substr(_rowlbl,1,length(_rowlbl)-1) if substr(_rowlbl,-1,1)=="\" /* remove single "\" from end of string */
      replace _rowlbl = substr(_rowlbl,1,length(_rowlbl)-1) if substr(_rowlbl,-1,1)=="\" /* remove line break "\\" from string */
      replace _rowlbl = trim(_rowlbl)
      drop if _rowname=="" & _rowlbl==""
      qui count if index(_rowlbl,"$")>0 & lower(substr(_rowlbl,index(_rowlbl,"$")+1,1))>="a" & lower(substr(_rowlbl,index(_rowlbl,"$")+1,1))<="z"
      sort _rowname
      if `r(N)' > 0 {
        noi disp in blue "Warning. There are TeX math mode $ signs followed by letters."
        noi list if index(_rowlbl,"$")>0 & lower(substr(_rowlbl,index(_rowlbl,"$")+1,1))>="a" & lower(substr(_rowlbl,index(_rowlbl,"$")+1,1))<="z"
        noi disp in blue "Stata will consider these math mode $ signs as global macro calls."
        noi disp in blue "Add blanks after the math mode $ signs if needed."
        noi disp
        }
      qui compress
      }
    }

  qui save "`outfile'", replace
  if _rc==0 {
    disp in green "list of row labels saved to " in yellow "`outfile'"
    }
  if `saved' {
    use `tmp', clear
    disp in green "data in memory restored"
    }

end