*! qqtable 1.0.4 29may2026
*! Pretty console table + LaTeX export for QQR results
*! Author: Merwan Roudane

program qqtable
    version 14
    syntax [using/] [, VALUE(name) STARS VARIABLE(name) BAND(name) ///
        DIGITS(integer 3) TITLE(string) LATEX(string) REPLACE ]

    if "`value'"==""  local value "coef"
    if `"`title'"' == ""  local title "Quantile-on-Quantile results"

    preserve
    if `"`using'"' != "" qui use `"`using'"', clear

    cap confirm variable `value'
    if _rc {
        di as err "value variable not found: `value'"
        exit 111
    }
    cap confirm variable tau
    if _rc {
        di as err "expected variable tau"
        exit 111
    }
    cap confirm variable theta
    if _rc {
        di as err "expected variable theta"
        exit 111
    }

    if "`variable'" != "" {
        cap confirm variable variable
        if !_rc qui keep if variable == "`variable'"
    }
    if "`band'" != "" {
        cap confirm variable band
        if !_rc qui keep if band == "`band'"
    }

    local has_p = 0
    cap confirm variable p
    if !_rc local has_p = 1
    local want_stars = cond("`stars'"!="" & `has_p', 1, 0)

    mata: lqqr_qqtable_print("`value'", `digits', `want_stars')

    * ---- Optional LaTeX export (pure ado, no Mata) ----------------------
    if `"`latex'"' != "" {
        qui levelsof tau,   local(taus)
        qui levelsof theta, local(thetas)
        local L : word count `thetas'
        local fmt %12.`digits'f

        * Build LaTeX special chars safely: Stata treats $ and \\ specially.
        local bs = char(92)     // backslash  \
        local dl = char(36)     // dollar     $
        local eol "`bs'`bs'"    // LaTeX row terminator  \\

        tempname fh
        cap file close `fh'
        if "`replace'"=="replace" file open `fh' using `"`latex'"', write replace text
        else                      file open `fh' using `"`latex'"', write text

        file write `fh' "% generated by qqtable (qqr package)" _n
        file write `fh' "`bs'begin{table}[htbp]`bs'centering" _n
        file write `fh' "`bs'caption{`title'}" _n
        file write `fh' "`bs'begin{tabular}{l*{`L'}{r}}" _n
        file write `fh' "`bs'hline`bs'hline" _n

        * Header row: tau \ theta values
        file write `fh' "`dl'`bs'tau`bs'backslash`bs'theta`dl'"
        foreach th of local thetas {
            local thd : di %5.2f `th'
            file write `fh' " & `thd'"
        }
        file write `fh' " `eol'" _n "`bs'hline" _n

        * Body rows
        foreach t of local taus {
            local td : di %5.2f `t'
            file write `fh' "`td'"
            foreach th of local thetas {
                qui su `value' if tau==`t' & theta==`th', meanonly
                if r(N)==0 | r(mean)==. {
                    file write `fh' " & "
                }
                else {
                    local cell : di `fmt' r(mean)
                    local cell = trim("`cell'")
                    local star ""
                    if `want_stars' {
                        qui su p if tau==`t' & theta==`th', meanonly
                        local pv = r(mean)
                        if      `pv' < 0.01 local star "`dl'^{***}`dl'"
                        else if `pv' < 0.05 local star "`dl'^{**}`dl'"
                        else if `pv' < 0.10 local star "`dl'^{*}`dl'"
                    }
                    file write `fh' " & `cell'`star'"
                }
            }
            file write `fh' " `eol'" _n
        }

        file write `fh' "`bs'hline`bs'hline" _n
        file write `fh' "`bs'end{tabular}" _n
        if `want_stars' ///
            file write `fh' "`eol'[2pt]{`bs'footnotesize `dl'^{*}`dl' p`dl'<`dl'0.10, `dl'^{**}`dl' p`dl'<`dl'0.05, `dl'^{***}`dl' p`dl'<`dl'0.01}" _n
        file write `fh' "`bs'end{table}" _n
        file close `fh'

        di as txt "  LaTeX table written to " as res `"`latex'"'
    }

    restore
end