Announcement

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

  • Line breaks while exporting from STATA to latex

    Dear Experts,
    I am using texsave command (from ssc's texsave package) to export tables prepared in data format - like those generated from mrtab or collapse command. However, in the texfile generated, the labels/text do not fit into a page. I tried relabeling variables/text by adding "\n" but it did not work while compiling in Latex. Is there any way to relabel the variables or the text within the variables so that the resultant output in texfile comes with line break?

    Provided below is a sample data and code that did not work:
    Code:
    clear 
    set obs 5
    set seed 123456
    
    gen a = "This is a very long text  that does not fit into my page"
    gen b = runiform(0, 1)
    gen c = runiform(20, 30)
    
    
    
    // I tried to change it to the following but it did not work
    
    replace a = "This is a very long text \nthat does not fit into my page" // I want the tex output such that  the text line "that does not fit into my page" starts in a new line 
    
    //ssc install texsave * Use it once 
    
    texsave a b c  using texfile.tex, replace frag varlabels

  • #2
    In LaTeX, you can introduce line breaks using either two backslashes (\\) or \newline command. So you want:

    Code:
    clear
    set obs 5
    set seed 123456
    
    gen a = "This is a very long text  that does not fit into my page"
    gen b = runiform(0, 1)
    gen c = runiform(20, 30)
    
    replace a = "This is a very long text \newline that does not fit into my page"
    texsave a b c  using texfile.tex, replace frag varlabels

    Comment


    • #3
      Hi Andrew,
      Thank you for this. I tried but it does not work with texsave. Could you suggest alternative ways of exporting data tables (generated from collapse/ mrtab, etc.) to latex?

      Comment


      • #4
        I'm not sure what you mean. The only thing I notice is that the command uses the package "tabularx", which you need to include in your preamble. The following works for me:

        Code:
        clear
        set obs 5
        set seed 123456
        
        gen a = "This is a very long text  that does not fit into my page"
        gen b = runiform(0, 1)
        gen c = runiform(20, 30)
        
        replace a = "This is a very long text \\ that does not fit into my page" 
        texsave a b c  using texfile.tex, replace frag varlabels
        
        
        // backup table and open new file
        copy texfile.tex tmp.tex
        file open fh using texfile.tex, write replace
        // write top lines
        file write fh "\documentclass{article}" _n
        file write fh "\usepackage{multirow}" _n
        file write fh "\usepackage{tabularx}" _n
        file write fh "\usepackage{amsmath}" _n
        file write fh "\usepackage{booktabs}" _n
        file write fh "\usepackage{ulem}" _n
        file write fh "\usepackage[table]{xcolor}" _n
        file write fh "\begin{document}" _n
        // now insert the document
        file open fh2 using tmp.tex, read
        file read fh2 line
        while r(eof)==0 {
            file write fh `"`line'"' _n
            file read fh2 line
        }
        file close fh2
        // write bottom lines
        file write fh _n "\end{document}" _n
        // clean up
        file close fh
        erase tmp.tex

        Res.:

        Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	48.9 KB
ID:	1749285

        Comment


        • #5
          I am using "Overleaf" for online collaboration but not calling LaTeX from within the do file. While I export my tables from STATA, texsave automatically fixes my "\" to "" in .tex output files. I also tried adding "nofix" option to the lines of code. I also have defined "tabularx" package in my preamble. Are there any additional options for the texsave command that I am not aware of ?

          Comment


          • #6
            By the way I had included "double backslashes" and "single backslash" symbols in #5 which however resulted to "single backslash" and "missing" after I posted. Is it that I have to wrap these with some tags?

            Comment


            • #7
              My LaTeX editor is WinEdt, and as I demonstrated in #4, it produces the desired line breaks. I have no experience with Overleaf. To find out if the issue is with the editor, run the code in #4 and do not edit the output. Then compile it in your editor. If you do not get a table similar to the uploaded screenshot, then you have found the issue. You will need to consult with people familiar with Overleaf to understand why its output differs from that of WinEdt.

              Comment

              Working...
              X