LaTeX and Stata integration (2): Solving some problems

I have noticed a definite increase in the number of questions I receive regarding my Stata and LaTeX integration post (maybe deadlines are approaching?). I guess it’s a good idea to address some of these questions in a new post and also show some changes that improve the LaTeX code.

Wrapping of column titles

Bert suggests an alternative to my \specialcell command to wrap column headings. The \specialcell requires to set the line break manually, which some might find a bit tedious. He suggest estouts prefix and suffix option to insert a minipage of a fixed width. The idea  is that, by telling LaTeX exactly how wide the column is, it can automatically wrap the text accordingly. The estout command would look something like

mlabels(titles prefix("\begin{minipage}{0.5in}") suffix("\end{minipage}"))

I have tried this and it works, but I am not completely satisfied with the result for two reasons: First, I find that an automatic line-break might be in places where I don’t want it and the column titles might be to close to each other if it is a table with lot’s of information. Second, my preference is to avoid putting LaTeX design code into estout as much as possible. I just prefer to change the layout in LaTeX.

However, it is a good idea to adjust the column-width manually if you are not filling the table to the textwidth. Fortunately, this is extremely easy with siunitx, all you have to do is add the following to your S-Column: table-column-width=20mm, which fixes the width of each data column to 20mm. E.g., the full code for the table would look like:

\begin{table}
\centering
\caption{Table with decimal alignment and fixed column width}
\estauto{sometable}{3}{S[table-format=1.2,table-column-width=20mm]}
\fignote{A little note below the text}
\starnote
\end{table}

Notes that have the same width as the table

Another suggestion from Bert (thank you!). Estout has a serious bug that stretches the first column of the table when you use estout’s note function. This is why I created the custom \fignote, \figsource, \fignote and \starnote commands that just add a custom caption below the table. This has one big weakness, as the picture shows: a long note is set as long as the page dimension allows. This does not look very nice, it would be better if the note would be as wide as the table.

Fortunately, this can be easily solved with the threeparttable package. The package uses different commands for notes that set them at the exact width of the table. Hence, we need to create new custom note commands: \Figtext, \Fignote, \Fixsoure and \Starnote(note the capitalisation!). See the code below to see how the table has been generated. Looks nicer, doesn’t it? Don’t forget to add \usepackage{threeparttable} to your preamble.

...
\usepackage{threeparttable}% Alternative for Notes below table

% Note/Source/Text after Tables
\newcommand{\Figtext}[1]{%
 \begin{tablenotes}[para,flushleft]
 \hspace{6pt}
 \hangindent=1.75em
 #1
 \end{tablenotes}
}

\newcommand{\Fignote}[1]{\Figtext{\emph{Note:~}~#1}}
\newcommand{\Figsource}[1]{\Figtext{\emph{Source:~}~#1}}
\newcommand{\Starnote}{\Figtext{* p < 0.1, ** p < 0.05, *** p < 0.01. Standard errors in parentheses.}}% Add significance note with \starnote

\begin{document}

\begin{table}\centering
\begin{threeparttable}
\caption{Table with better notes and decimal alignment}
\estauto{sometable}{3}{S[table-format=1.2,table-column-width=20mm]}
\Fignote{This is a very long row. As you can see it is longer than the actual width of the table. This does not look very nice.}
\Starnote
\end{threeparttable}
\end{table}

\end{document}

Problem with caption position

Maria has the problem that the caption is “too high” and cuts in to the table. When I generated the note-commands I used the scrartcl class and adjusted margins accordingly. If you use something like article then all notes will be too high.

That’s easily solved: You can either use the new approach to create notes with the threeparttable package (see above). Or, if you prefer the “caption” approach just adjust the vspace that I insert. For the article class you may comment the vspace completely, and everything looks fine.

\newcommand{\figtext}[1]{%
 %\vspace{-1.9ex}% Comment here or adjust accordingly
 \captionsetup{options=figtext}
 \caption*{\hspace{6pt}\hangindent=1.5em #1}
 }

Ugly significance stars

I made a mistake in the original post and set the significance stars in mathmode. This sometimes causes the significance stars to be too large and not raised. To solve just change the \sym command to the one below (before #1 was wrapped in $#1$ which sets stars into mathmode).

\newcommand{\sym}[1]{\rlap{#1}}

 Print very long tables on several pages

Torben has a problem with very long descriptive tables that do not fit on one page. There are two ways to solve this in LaTeX: the longtable package or the longtabu environment from the tabu package. The tabu package is a bit more modern and flexible, so this might be the way to go.

I have been playing around for a bit, but it is a bit more complex than I anticipated. So, unfortunately, I cannot provide a solution until I encounter a long table myself :(. If anyone has a suggestion, I am happy to hear it!

Further enhancements

In another post I will write something about improving the typography of the table further. This involves some character substitution so that brackets ( ) and the minus sign do not cause any overfull hbox warnings in LaTeX.

Links

17 thoughts on “LaTeX and Stata integration (2): Solving some problems

  1. Hi, I got a question. I try to output the estimates I got from gmm and there’re 16 estimates. When I output them, they’re always displayed in a row where only first three or four would fit into an A4 pdf. Do you have any idea to solve this problem?

  2. Dear Jorg, thank you very much for this very helpful thread! I have a problem when I try to display a twoway table generated by esttout.

    estpost tabulate year var, mi
    esttab using summary_all.tex,replace cell(colpct(fmt(2)) b(fmt(g) par )) unstack

    this table has 9 data columns but when I compile my .tex document I get the error

    ! Misplaced \noalign.
    \bottomrule ->\noalign
    {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
    l.157 ...ATADO/paper/output/summary_all.tex}{9}{c}

    What do you think? Many many thanks

  3. Dear Jörg,
    Thanks for the insightful documentation.

    would you mind posting your “naked” template of latex? I’m having difficulties compilling the information with my standard paper template I’ve been using for the last 4-5 seminar papers.

    • Sebastian,
      what do you mean with “naked template of latex”? The example I posted is an MWE (a “minimum working example”) and is fully compilable. If you have issues incorporating it into your existing template it is most likely some incompatibility with the packages that you load (although I tried it with most common ones).

      • Dear Jörg,

        I’m back to fiddling with latex and still haven’t been able to get the simplest output from stata into latex.

        I’ve deleted every package from my preamble to take one step at a time and already run into the xkeyval error after including the code from Carlisle:

        [removed long code example]

        Result:
        ! Package xkeyval Error: `detect-mode' undefined in families `key'. ….

        Hence my previous question. I’m not unexperienced in latex, but so far never had to insert outputs from stata :/

        your output examples look very appealing and it would be great to implement that.

        Any help would be greatly appreciated.

        • The example code you send compiles without any errors on my machine. There must be an error with your distribution, and I would strongly suggest updating all your packages!

          Edit: Ah, the error message shows that you are using an old version of siunitx!

          • ah that might be it, i ll update my tex and everything else i can. thanks

  4. Any idea why the \Starnote command would cause the Underfull \hbox (badness (10000) error ?
    Otherewise the code works neatly at it looks ok.

    • Actually, What I meant by that is that my table fits the sheet, but it is not centered. I tried to insert \centering, but it didn’t help.

  5. Dear Jorg,

    I join the others in thanking you for your great help/service/guidance on how to integrate estout and LaTex. Thank you.

    Anyhow, I’ve managed to follow your examples without much trouble, however now I’m looking for away to fit everything onto a 1 A4 landscape-wide page (I’m trying to display 14 different regression models).

    I’ve used \resizebox in the past to make my 14 models fit, however I’m not sure where to place that within your scheme (my guess is that it would have to be specified in the “Estout related things” section of the new commands), but I don’t know how to make it work.

    Any additional guidance/suggestion is welcome.

    Thank you in advance.

    • You should use the adjustboxpackage to resize, but this creates a problem with threeparttables. You can use \adjustbox{max width=\columnwidth}{...} with \estauto or \estwide as is if you do not use threeparttable. With threeparttable it becomes more complicated, a solution is provided here: https://tex.stackexchange.com/a/205525

Leave a Reply to Sebastian Cancel reply