Announcement

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

  • styletextab: module for restyling default LaTeX tables

    Thanks to Kit Baum, a new user-written module styletextab is up on SSC.

    Stata 17 introduced the collect suite of commands (collect, table, etable, and, as of version 18, dtable), all of which can export tables as LaTeX files. By default, the output is a standalone compilable document, which is useful for quickly inspecting the look of the tables. styletextab improves the appearance and formatting of default LaTeX tables exported by these commands. By default, it replaces \cline with the \cmidrule of the booktabs package for aesthetically pleasing vertical spacing. It also wraps footnotes within the tablenotes environment of the threeparttable package for proper formatting (e.g., notes with the same width as the table). Additionally, styletextab offers an option to rotate tables (i.e., switch to landscape layout) by wrapping tables within the landscape environment of pdflscape package. styletextab also retains the ability to "fragmentize" ex post, i.e., to keep only the table or tabular environments for inclusion in a document via \input macro.

    Typically, styletextab is executed immediately after collect exports. For example, let's say we want to export a table comparing two regression models.
    Code:
    sysuse auto, clear
    
    regress price mpg
    estimates store m1
    
    regress price mpg i.foreign
    estimates store m2
    
    etable, estimates(m1 m2) mstat(N) column(index) ///
        showstars showstarsnote                     ///
        title("Table title")                        ///
        note("Note: Table notes go here.")          ///
        export(mytable.tex, replace)
    fig1.png

    We can execute styletextab immediately after exporting:
    Code:
    styletextab
    fig2.png

    The table is restyled with booktabs and threeparttable.

    We can also switch to landscape mode and back to portrait mode:
    Code:
    styletextab, lscape
    styletextab
    Click image for larger version

Name:	fig4.png
Views:	1
Size:	73.6 KB
ID:	1719432

    We can add a label marker, and some text before and after the table:
    Code:
    styletextab,                                           ///
        label(fig:reg1)                                    ///
        before(Table~\ref{fig:reg1} presents regressions.) ///
        after(This text comes after Table~\ref{fig:reg1}.)
    fig3.png

    Finally, we can retain the table or tabular environments as follows:
    Code:
    styletextab, tableonly
    styletextab, fragment
    Hope this is useful for quickly inspecting LaTeX table exports. Please feel free to report any issues or suggestions for improvement on my GitHub.
    Last edited by Gorkem Aksaray; 05 Jul 2023, 09:33.

  • #2
    Dear Statalisters,

    Thanks to Kit Baum, a new version of styletextab is now available on SSC.

    This version (1.2.1) introduces several improvements, the most prominent of which is to allow users to add LaTeX packages to the document preamble at will. I will provide a simple example to illustrate the increased extensibility of the tool. Say we want to create and export a table comparing two logistic regression models.
    Code:
    webuse lbw, clear
    
    logit low age lwt i.race smoke ptl ht ui
    estimates store m1
    
    logit low age lwt i.race smoke ptl ht ui ftv
    estimates store m2
    
    etable, estimates(m1 m2) column(index) center   ///
        mstat(N) mstat(chi2)                        ///
        showstars showstarsnote                     ///
        title("Table title")                        ///
        note("Note: Table notes go here.")          ///
        export(mytable.tex, replace)
    table1


    Not only do we want to restyle this table, but we also want to display the default unicode chi-square (χ2) symbol, which is not rendered by pdfLaTeX out of the box. One way to achieve this as follows.
    Code:
    styletextab,                                    ///
        usepackage(ucs, opt(mathletters) pre)       ///
        usepackage(inputenc, opt(utf8x) pre)
    table2


    We added two LaTeX packages, ucs and inputenc, with appropriate options to render unicode math characters. The option usepackage() specifies the package to be added, and can be repeated to add any number of packages. opt() specifies comma-separated package options. pre determines the order of placement: it specifies that the \usepackage{} command should be inserted at the beginning of the document preamble, right after the document class declaration (the default behavior is to insert it at the end of the document preamble). The output is cleaner, and chi-squared symbol is now rendered nicely.

    Let's add several other packages to further customize the export (this may seem like overkill, but it illustrates the point): geometry to set page margins, setspace to increase line spacing, and endfloat to place the table on a separate page at the end of the document and leave a marker in its place. I will also modify the default in-text marker.
    Code:
    styletextab,                                                        ///
        usepackage(ucs, opt(mathletters) pre)                           ///
        usepackage(inputenc, opt(utf8x) pre)                            ///
        usepackage(geometry, opt(margin=1in) pre)                       ///
        usepackage(setspace, opt(onehalfspacing))                       ///
        usepackage(endfloat, opt(nofiglist,notablist) pre               ///
                   n("\renewcommand\floatplace[1]{%"                    ///
                     "  \begin{center}"                                 ///
                     "    [Insert \csname #1name\endcsname~%"           ///
                     "     \csname thepost#1\endcsname\ about here.]"   ///
                     "  \end{center}}"))
    twopage1


    The option n()—short for nextlines()—can take a list of strings delimited by double quotes, each placed on a separate line.

    Next, let's add some text before and after the table. We can use beforetext() and aftertext() options, which can be repeated to add multiple paragraphs separated by an empty line. The new version comes with two improvements. First, multiple lines of text can be added by using double quotes. Second, either option, when specified, automatically defines two new LaTeX macros \sq{} and \dq{} for single quotation and double quotation, respectively, allowing for using quotations within text. Otherwise, local and global macros can be used within text as usual. I will also add two more LaTeX packages: parskip to add extra space between paragraphs, and lipsum to insert dummy text.
    Code:
    styletextab,                                                        ///
        label(fig:reg1)                                                 ///
        usepackage(ucs, opt(mathletters) pre)                           ///
        usepackage(inputenc, opt(utf8x) pre)                            ///
        usepackage(geometry, opt(margin=1in) pre)                       ///
        usepackage(setspace, opt(onehalfspacing))                       ///
        usepackage(endfloat, opt(nofiglist,notablist) pre               ///
                   n("\renewcommand\floatplace[1]{%"                    ///
                     "  \begin{center}"                                 ///
                     "    [Insert \csname #1name\endcsname~%"           ///
                     "     \csname thepost#1\endcsname\ about here.]"   ///
                     "  \end{center}}"))                                ///
        usepackage(parskip) usepackage(lipsum)                          ///
        before(\section*{Regression models})                            ///
        before("Table~\ref{fig:reg1} presents regressions."             ///
               "These regressions are very interesting." \lipsum[1])    ///
        before(Let's see how \dq{they} look:)                           ///
        after(This text comes after Table~\ref{fig:reg1}. \lipsum[2])   ///
        after(\lipsum[3])
    twopage2


    Finally, notice that the minus signs are not the usual hyphens. To ensure proper typographic representation of minus signs, styletextab now converts hyphens in cells of the form \multicolumn{}{}{-...} to \multicolumn{}{}{$-$...}, which covers most (but not all) scenarios.

    I hope that you find these changes useful. Any feedback and suggestions are welcome in the project GitHub page.
    Last edited by Gorkem Aksaray; 12 Oct 2023, 11:25.

    Comment


    • #3
      Dear Statalisters,

      Thanks to Kit Baum, a new version of styletextab is now available on SSC.

      This version (1.3) adds the capability to change font and page sizes. There are three new options:
      • pt(#) sets the size of the main font used in the document. For example, if pt(11) is specified, the main text will be set to 11pt with correspondingly appropriate vales applied to other document elements.
      • paper(papersize) sets the paper size of the document. For example, specify paper(a4) to change the paper size to A4.
      • tabsize(fontsize) sets the size of the font used in the table. For example, tabsize(small) adds a \small macro within the table environment to make the table look a bit smaller than \normalsize text.
      To illustrate, I will use the example from my previous post. Let's say I want to use A4 paper size, make the main text bigger but keep the table font significantly smaller.
      Code:
      webuse lbw, clear
      
      logit low age lwt i.race smoke ptl ht ui
      estimates store m1
      
      logit low age lwt i.race smoke ptl ht ui ftv
      estimates store m2
      
      etable, estimates(m1 m2) column(index) center   ///
          mstat(N) mstat(chi2)                        ///
          showstars showstarsnote                     ///
          title("Table title")                        ///
          note("Note: Table notes go here.")          ///
          export(mytable.tex, replace)
      
      styletextab,                                                        ///
          label(fig:reg1)                                                 ///
          usepackage(ucs, opt(mathletters) pre)                           ///
          usepackage(inputenc, opt(utf8x) pre)                            ///
          usepackage(geometry, opt(margin=1in) pre)                       ///
          usepackage(setspace, opt(onehalfspacing))                       ///
          usepackage(endfloat, opt(nofiglist,notablist) pre               ///
                     n("\renewcommand\floatplace[1]{%"                    ///
                       "  \begin{center}"                                 ///
                       "    [Insert \csname #1name\endcsname~%"           ///
                       "     \csname thepost#1\endcsname\ about here.]"   ///
                       "  \end{center}}"))                                ///
          usepackage(parskip) usepackage(lipsum)                          ///
          before(\section*{Regression models})                            ///
          before("Table~\ref{fig:reg1} presents regressions."             ///
                 "These regressions are very interesting." \lipsum[1])    ///
          before(Let's see how \dq{they} look:)                           ///
          after(This text comes after Table~\ref{fig:reg1}. \lipsum[2])   ///
          after(\lipsum[3])                                               ///
          pt(12) paper(a4) tabsize(footnotesize)
      I only had to add the last line pt(12) paper(a4) tabsize(footnotesize) to make those changes, and ended up with the following output:

      Click image for larger version

Name:	styletextab_1_3_fig.png
Views:	1
Size:	868.6 KB
ID:	1750470


      Any feedback and suggestions are welcome in the project GitHub page as always.

      Comment

      Working...
      X