Announcement

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

  • Table formatting via collect (problem with border lines)

    I've been learning how to format tables via the command collect.
    I've got a general grasp of how to use it, but I haven't been able to successfully edit borders.
    I tried different alternative commands to have a double border at the top and bottom of the table and below column headers, but they all seem to be doing nothing at all.
    Here's what I got so far.
    Code:
    use https://www.stata-press.com/data/r17/lifeexp
    collect: reg lexp safewater
    collect: reg lexp gnppc
    collect get checkmark="YES", tag(cmdset[1])
    collect get checkmark="NO", tag(cmdset[2])
    collect layout (colname#result[_r_b _r_se] result[checkmark]) (cmdset)
    collect style cell border_block[corner row-header], border(right, pattern(nil))
    collect style cell result[checkmark], border(top, pattern(single))

  • #2
    The default style defines the border block patterns with:
    Code:
    collect style cell border_block[corner], border(top right)
    collect style cell border_block[column-header], border(top)
    collect style cell border_block[row-header], border(top bottom right)
    collect style cell border_block[item], border(top bottom)
    So you need to redefine the patterns for these border blocks.

    Here is the code I added to change the pattern to double.
    Code:
    collect style cell border_block[corner], border(top, pattern(double))
    collect style cell border_block[column-header], border(top, pattern(double))
    collect style cell border_block[row-header], border(top bottom, pattern(double))
    collect style cell border_block[item], border(top bottom, pattern(double))
    This change will not show up in the output of collect preview because SMCL does not have a double line pattern, so I exported to HTML. Here is a screen-shot of my browser showing the resulting table.

    Click image for larger version

Name:	Screenshot 2024-10-23 at 11.34.14 AM.png
Views:	1
Size:	20.8 KB
ID:	1766275

    Comment


    • #3
      It seems like double lines are not exported to LaTeX. Is there a way to do that via the collect commands?

      Comment


      • #4

        Standard LaTeX does not have styled rules (borders) in tables. Most people appear to use extra LaTeX packages, like booktabs, to customize their table borders.

        You will have to hand edit or write some post-processing code to edit the file produced by collect export. Here is an example where I use filefilter to tanslate the \cline macros to double \hlines.
        Code:
        use https://www.stata-press.com/data/r17/lifeexp
        collect: reg lexp safewater
        collect: reg lexp gnppc
        collect get checkmark="YES", tag(cmdset[1])
        collect get checkmark="NO", tag(cmdset[2])
        collect layout (colname#result[_r_b _r_se] result[checkmark]) (cmdset)
        collect style cell border_block[corner row-header], border(right, pattern(nil))
        collect style cell result[checkmark], border(top, pattern(single))
        collect preview
        collect export table.tex, replace
        
        * convert \cline{1-3} to \hline\hline
        filefilter table.tex table-mod.tex, replace from("\BScline{1-3}") to("\BShline\BShline")
        If you want more than one type of style for your table's borders, then you will have to hand edit or write more sophisticated code to accommodate this. Here is some code where I loop over the lines of the original file produced by collect export. It adds a LaTeX package and uses some of its rule drawing commands.
        Code:
        * more custom filtering
        file open ii using table.tex, read text
        file open oo using table-mod2.tex, write text replace
        file read ii line
        local rule_pos 1
        while r(eof) == 0 {
            if strmatch(`"`line'"', "*begin{document}*") {
                * use package that defines top/middle/bottom rules 
                file write oo `"\usepackage{booktabs}"' _n
            }
            else if strmatch(`"`line'"', "*cline{*") {
                * customize the horizontal rules
                if `rule_pos' < 3 {
                    * double rules for column headers
                    local line "\toprule\toprule"
                }
                else if `rule_pos' == 3 {
                    * think rule for middle of table
                    local line "\midrule[2pt]"
                }
                else {
                    * double rules for bottom
                    local line "\bottomrule\bottomrule"
                }
                local ++rule_pos
            }
            file write oo `"`line'"' _n
            file read ii line
        }
        file close oo
        file close ii
        Here are some screen-shots from the Preview pdf viewer on my mac.


        Preview from filefilter.
        Click image for larger version

Name:	Screenshot 2024-11-04 at 11.22.49 AM.png
Views:	1
Size:	28.8 KB
ID:	1766989


        Preview from more custom loop.
        Click image for larger version

Name:	Screenshot 2024-11-04 at 11.22.22 AM.png
Views:	1
Size:	30.1 KB
ID:	1766990

        Comment

        Working...
        X