Announcement

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

  • Formatting Superheaders and Selective Borders using Collect

    Hello,

    I am having trouble formatting a table that I will need to repeat 50+ times and would like Stata to take care of the formatting as much as possible.

    I have been able to get most of what I need, however I am having trouble with two aspects:

    - Formatting the Super-headers: I would like the rows "Version1" and "Version2" to have a gray background color. In the example below, when I try to format the collection dimension, it formats the entire table. I cannot find a way to format just the header row for the collection dimension (Version1 and Version2)

    - Selective borders: I would like to have borders around each combination of coefficient and confidence interval. I can only figure out how to format it so no borders appear among the item cell type, or all of them. I can get most of the way there using the below code, but it 1) includes borders for non-results like the spacer and the collection super-header, and 2) doesn't extend to the row-headers to make boxes of the same size. Is there a way to do this?

    Thank you for your help:

    Code:
    collect clear
    sysuse auto, clear
    
    collect create Version1, replace
    
        collect _r_b _r_ci : reg price headroom trunk
        collect _r_b _r_ci : reg price headroom trunk weight length
    
    collect create Version2, replace
    
        collect _r_b _r_ci : reg price headroom trunk if foreign == 0
        collect _r_b _r_ci : reg price headroom trunk weight length if foreign == 0
        
    collect combine combined = Version1 Version2
    collect layout (collection[Version1]#colname[headroom]#result collection[Version1]#colname[trunk]#result collection[Version2]#colname[headroom]#result collection[Version2]#colname[trunk]#result) (cmdset)
    
    collect style header result, level(hide)
    collect style row stack, spacer
    
    collect stars _r_b _r_p .05 `"*"', attach(_r_b)
    collect style cell result[_r_ci], sformat("[%s]") cidelimiter(",")
    
    collect style cell , valign(center) nformat(%9.2f) 
    collect style cell cell_type[row-header], halign(left)
    collect style cell cell_type[column-header], halign(center)
    collect style cell cell_type[item], halign(center)
    
    collect style cell, border( all, pattern(none) )
    collect style cell result[_r_b] , border( top left right, pattern(single) )
    collect style cell result[_r_ci] , border( bottom left right, pattern(single) )

  • #2

    Thanks for the working example.

    Here is my attempt to shade and border the cells based on my understanding of your description. My edits are in blue.
    Code:
    collect clear
    sysuse auto, clear
    
    collect create Version1, replace
    
        collect _r_b _r_ci : reg price headroom trunk
        collect _r_b _r_ci : reg price headroom trunk weight length
    
    collect create Version2, replace
    
        collect _r_b _r_ci : reg price headroom trunk if foreign == 0
        collect _r_b _r_ci : reg price headroom trunk weight length if foreign == 0
    
    collect combine combined = Version1 Version2
    collect layout (collection[Version1]#colname[headroom]#result collection[Version1]#colname[trunk]#result collection[Version2]#colname[headroom]#result collection[Version2]#colname[trunk]#result) (cmdset)
    
    collect style header result, level(hide)
    collect style row stack, spacer
    
    collect stars _r_b _r_p .05 `"*"', attach(_r_b)
    collect style cell result[_r_ci], sformat("[%s]") cidelimiter(",")
    
    collect style cell , valign(center) nformat(%9.2f)
    collect style cell cell_type[row-header], halign(left)
    collect style cell cell_type[column-header], halign(center)
    collect style cell cell_type[item], halign(center)
    
    * comment-out original border code
    *collect style cell, border( all, pattern(none) )
    *collect style cell result[_r_b] , border( top left right, pattern(single) )
    *collect style cell result[_r_ci] , border( bottom left right, pattern(single) )
    
    * add blank space item with hidden colname level to turn off shading
    collect get _r_b=" ", tags(collection[Version1] colname[_hide] cmdset[1])
    collect get _r_b=" ", tags(collection[Version2] colname[_hide] cmdset[1])
    * use autolevels shorten the layout specification
    collect style autolevels colname _hide headroom trunk, clear
    collect layout (collection#colname#result) (cmdset)
    
    * add item for blank row
    collect get _r_b=" ", tags(collection[_hide] colname[_hide] cmdset[1])
    collect style autolevels collection Version1 _hide Version2, clear
    collect style row stack, nospacer
    collect preview
    collect export try0.html, replace
    
    * add shading
    collect style cell cell_type[row-header item]#collection[Version1 Version2], ///
        shading(background(gray))
    collect style cell ///
        cell_type[row-header item]#colname[headroom trunk] , ///
        shading(background(none))
    collect export try1.html, replace
    
    * add dimension for more control over blank row-header cells
    collect addtags blank[_hide], fortags(_r_b)
    * " " -- the space -- is a valid level, but quotes are necessary
    collect addtags blank[" "], fortags(_r_lb _r_ub)
    collect layout (collection#colname#result#blank) (cmdset)
    collect export try2.html, replace
    
    * fix cell borders
    collect style cell border_block[corner], border( all , pattern(none) )
    collect style cell border_block[corner], border( all , pattern(none) )
    collect style cell border_block[column-header], border( all , pattern(none) )
    collect style cell border_block[row-header], border( right , pattern(none) )
    collect style cell border_block[row-header], border( left , pattern(single) )
    collect style cell cell_type[row-header]#collection[_hide], ///
        border( left right , pattern(none) )
    collect style cell cell_type[row-header]#collection[_hide], ///
        border( top bottom , pattern(single) )
    collect style cell cell_type[item]#collection[_hide], ///
        border( top bottom , pattern(single) )
    collect style cell border_block[item], ///
        border( right , pattern(single) )
    collect style cell cell_type[item]#collection[_hide], ///
        border( right , pattern(none) )
    collect preview
    collect export try3.html, replace
    Here is a screenshot from my browser of the final exported HTML table.

    Click image for larger version

Name:	Screenshot 2024-12-24 at 12.46.46 PM.png
Views:	1
Size:	74.7 KB
ID:	1769965

    Comment

    Working...
    X