Replicability Best Practices - bblockwood/lab GitHub Wiki

Replicability Best Practices

This page records tools that help ensure research projects are replicable—that is, that others (or your future self!) can run your analysis from raw data to paper with consistent results.

Piping in-text statistics from Stata into LaTeX

To ensure that numbers in your paper (e.g. sample means, regression coefficients) match the most recent results from your analysis code, you can pipe scalar directly from Stata into LaTeX using the following workflow:

  • This approach creates a scalars.tex file with \newcommand{} macros that you can load into your paper. This method was originally shared by Raj Bhargava and is adapted here with permission and formatting edits.

1. Write a Stata program to export scalars as LaTeX commands:

The Stata code snippet below defines a program called latex_nc that takes the name of a scalar (meanage) and writes a corresponding LaTeX \newcommand{} line into a .tex file:

program define latex_nc
    local value = `1'
    local command "\\newcommand{\\\`1'}{`value'}"
    ! echo `command' >> "[directory]/scalars.tex"
end

/* Remove file in case it already exists */
rm "[directory]/scalars.tex"

/* Define and export a scalar */
sum age
scalar meanage = r(mean)
latex_nc meanage

Note: The command name passed to latex_nc should consist only of letters with no special characters or numbers. For example, meanage is a valid name, but mean_age or mean_age_2025 may lead to errors.

2. After running the Stata code, open the generated scalars.tex file to ensure it contains the correct LaTeX commands.

For example, if the mean of age is 25, the file should look like this:

\newcommand{\meanage}{25}

Note: Make sure that the \newcommand in the .tex file has only one backslash (\) before newcommand and the command name (e.g. \meanage).

  • If you see two backslashes (e.g., \\newcommand{\meanage}{25}, this likely means Stata interpreted your string with one too many escape characters. In that case, try modifying the program define section to use a single backslash in the local command line: local command "\newcommand{.... Be aware that behavior might vary slightly across operating systems, so you may need to experiment to find what works best in your environment.

3. Use the Scalars in Your LaTeX Document

To incorporate the exported scalars into your paper:

    a) Load the scalars.tex file into your LaTeX document (usually in the preamble or just before \begin{document}):

\input{[directory]/scalars.tex} 

    b) Use the defined commands anywhere in your LaTeX document, for example:

The average age in the sample is $\meanage$ years.

Each time you re-run your Stata code and regenerate scalars.tex, these values in your paper will update automatically, ensuring your reported statistics always match your latest analysis.

Note: One thing that requires a bit of care: escape characters (\) can behave differently across platforms. This can affect how the LaTeX commands are written to the .tex file by your Stata code and can lead to errors in your LaTeX document.

  • To handle this efficiently, consider using a program that checks the operating system and writes the output in a cross-platform-safe way.

Ready to put this into practice? You can find an implemented example of this approach in this repository. The training provides hands-on practice with the GitHub workflow and familiarizes you with the GitHub interface. It also includes the piping process demonstrated in the attached .do and .tex file.