Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export entire data into tex file with formatting

    I am trying to export the entire dataset into tex file. Specifically, I am looking for something equivalent to below Python pandas syntax. So it formats all the floats into percentage and print out until second decimal. NaN values are printed out with "-" sign and all the symbols including percentage sign are printed with backslash.


    Code:
    data.to_latex(escape=True, float_format = '{:.2%}'.format, column_format = 'lccc', na_rep= '-')
    Is it possible to do this using `esttab`? Or requires some other packages?

    Thanks in advance.
    Last edited by Younghun Lee; 24 Jan 2025, 14:40.

  • #2
    See

    Code:
    ssc describe texsave
    It is unclear what you mean by exporting percentages, as percentages are not a type of format. If your variables are expressed as proportions, you will need to convert them to percentages yourself by multiplying by 100. Once converted, you can specify the desired format for all or a subset of variables before exporting using texsave.

    Code:
    sysuse auto, clear
    keep in 1/10
    keep price weight length disp
    *ASSUME SOME TRANSFORMATION SUCH AS EXPRESSING AMOUNTS IN THOUSANDS IS NEEDED
    *FURTHER, ASSUME WE NEED NUMBERS CORRECT TO 2 DECIMAL PLACES
    foreach var of varlist *{
        replace `var'= `var'/1000
        format `var' %4.2f
    }
    texsave * using myfile.tex, replace
    Res.:

    Code:
    \documentclass{article}
    
    \usepackage{booktabs}
    
    \usepackage{tabularx}
    
    \usepackage[margin=1in]{geometry}
    
    \begin{document}
    
    
    
    
    \begin{table}[tbp] \centering
    
    \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
    
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    
    \newcolumntype{C}{>{\centering\arraybackslash}X}
    
    
    
    
    \begin{tabularx}{\linewidth}{@{}lCCC@{}}
    
    
    
    
    \toprule
    
    {price}&{weight}&{length}&{displacement} \tabularnewline
    
    \midrule \addlinespace[\belowrulesep]
    
    4.10&2.93&0.19&0.12 \tabularnewline
    
    4.75&3.35&0.17&0.26 \tabularnewline
    
    3.80&2.64&0.17&0.12 \tabularnewline
    
    4.82&3.25&0.20&0.20 \tabularnewline
    
    7.83&4.08&0.22&0.35 \tabularnewline
    
    5.79&3.67&0.22&0.23 \tabularnewline
    
    4.45&2.23&0.17&0.30 \tabularnewline
    
    5.19&3.28&0.20&0.20 \tabularnewline
    
    10.37&3.88&0.21&0.23 \tabularnewline
    
    4.08&3.40&0.20&0.23 \tabularnewline
    
    \bottomrule
    
    
    
    
    \end{tabularx}
    
    \end{table}
    
    \end{document}

    Comment


    • #3
      Got it. I was totally lost around formatting. It seems the best practice is to manually format each variable before exporting. Thank you.

      Comment

      Working...
      X