Announcement

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

  • use putpdf to combine a graph and a table (made with tabdisp)

    Dear Stata Experts,

    I would like to use the putpdf command to combine a graph and a table into a single pdf file.
    The trouble is that I made the table with tabdisp. I suspect there are at least two possible solutions:

    Solution 1: save/export the tabdisp output in a format that putpdf can handle
    Solution 2: replicate the tabdisp output with the putpdf table or putpdf collect command

    Unfortunately, I am not sure how to do either of these things. . .

    I have put some example code below.

    Thanks,

    Jeremy

    Code:
    sysuse auto, clear
    
    *Make graph
    scatter weight mpg
    graph export "Figure 1.png"
    
    *Make table
    tabdisp rep78 foreign if mi(rep78)==0, cellvar(make) 
    *Solution 1 here?
    
    *combine graph and table into a pdf
    *open pdf
    putpdf clear
    putpdf begin, landscape halign(center)
    
    *Add the graph
    putpdf paragraph
    putpdf image "Figure 1.png"
    
    *Add the table 
    *Solution 2 here?
    
    *save pdf
    putpdf save "car info.pdf", replace

  • #2
    Normally I would recommand using the new table command, but the cells
    of your table are populated by strings, which table's first
    statistic is not able to handle --

    So I used collapse to trim the dataset (copy) down to the
    reported values, then loop over the observations to get those values
    into the default collection.
    Code:
    sysuse auto
    
    * use -collect- to build a table similar to:
    tabdisp rep78 foreign if missing(rep78)==0, cellvar(make)
    
    collect clear
    frame copy default play
    frame play {
        * collapse the dataset to the values reported by -tabdisp-
        collapse (first) make if missing(rep78) == 0, by(foreign rep78)
        * verify
        list
        * loop over observations to populate the collection
        forval i = 1/`=_N' {
            local r = rep78[`i']
            local f = foreign[`i']
            collect get make=(make[`i']), ///
                tags(rowname[`r'.rep78] colname[`f'.foreign])
        }
        * some style choices to produce a similarly looking table
        collect style header result, title(hide) level(hide)
        collect style row stack, nobinder
        collect style column, dups(center)
        * arrange the collected items into our table
        collect layout (rowname) (colname#result)
    }
    Here is the resulting table.
    Code:
    --------------------------------------------
                       |        Car origin
                       |      Domestic   Foreign
    -------------------+------------------------
    Repair record 1978 |
      1                | Olds Starfire
      2                | Cad. Eldorado
      3                |   AMC Concord  Audi Fox
      4                | Buick Electra  BMW 320i
      5                |    Dodge Colt Audi 5000
    --------------------------------------------
    Now you can use putdocx collect to get this table into your document.

    Comment


    • #3
      Thank you Jeff! That worked beautifully.

      I also discovered that I could use the log command to convert the output from tabdisp into txt files, assemble them with the graphs using the putdocx command, and then convert it all to a pdf file using the docx2pdf command.
      This second option does not require as much fancy code, but the tables are not as pretty as with Jeff's solution. I have put the code for this second solution below in case someone else might find it useful.

      Jeremy

      Code:
      cd "C:\Users\reyno113\OneDrive - purdue.edu\misc\Grace\dance"
      sysuse auto, clear
      
      *Make graph
      scatter weight mpg
      graph export "Figure 1.png", replace
      
      *Make table 
      *Prevent commands from showing in log: https://www.stata.com/statalist/archive/2009-11/msg01129.html
      quietly {
           log using table1, replace text 
           noisily: tabdisp rep78 foreign if mi(rep78)==0, cellvar(make) 
           log close
      }
      
      *combine graph and table into a docx command
      putdocx clear
      putdocx begin, landscape // halign(center)
      
      *Add item 1
      putdocx paragraph
      putdocx image "Figure 1.png"
      putdocx pagebreak
      
      *Add item 2
      putdocx paragraph
      putdocx textfile table1.log
      
      *save pdf
      putdocx save "car info.docx", replace
      
      *convert the docx file to a pd file
      docx2pdf "car info.docx", replace
      Last edited by Jeremy Reynolds; 28 Feb 2024, 18:32.

      Comment


      • #4
        In #2, I should have mentioned putpdf collect instead of putdocx collect.

        Comment

        Working...
        X