*! version 0.2 20april2021

Title

stattotex -- Exporting values for inclusion in LaTeX.

Syntax

stattotex
using filename , statistic(statistic) name(string)
[other options]

options

Mandatory
statistic(#) specifies the statistic that you want to output

name(string) assigns a name to your statistic

Replacing
replace replaces a statistic of the same name if it exists. stattotex will throw an error if one exists and this option is not specified

Formatting
format(%fmt) selects the format for the statistic

Documentation
record records the date and time the statistic was created as a comment in LaTeX

comment(string) records a comment of your choosing in LaTeX

Advanced
forcename forces the name you assign without any checks

forcestat forces the statistic you assign without any checks

Description

stattotex is a simple Stata program to name and export a value from Stata into a LaTeX document, for easy entry, updating, checking and replicating with minimal errors.

stattotex is compatible with Stata v10.0+. While it may be compatible with earlier versions, it has not been tested in those environments.

In order to utilize the output of stattotex, you must include the file called in filename at the beginning of your LaTeX document. See section below for more details.

Options

Main

statistic(#) specifies the statistic that you want to output to LaTeX, which can be specified as a number (e.g. 1.234), an expression that evaluates to a number (e.g. 2*3+4), or a Stata macro that evaluates to a number
(e.g. $mystat). The option also accepts a string, so long as the string has numeric contents (e.g. "1.234") or is an expression that evaluates to
a number (e.g. "2*(3+4)"). If for some reason you want to override these limitations, use the forcestat option.

name(string) assigns a name to your statistic. The name can only contain letters in the English alphabet, as this is necessary for LaTeX to compile properly. stattotex also ensures that the name is not a default
LaTeX command, such as \beta, unless the forcename option is also used. NB: while stattotex checks against a list of base LaTeX commands to ensure no conflicts, it cannot control for command names used by
packages in your LaTeX file.

Replacing

replace tells stattotex to replace an existing statistic in the using file with the new statistic. This option is not telling stattotex to replace the using file itself -
stattotex already does that by default so long as it does not encounter any errors. Instead this option is for replacing individual statistics within a file.
If a statistic with the same name already exists and replace is not specified, stattotex will throw an error.

Formatting

format(%fmt) formats the statistic using standard Stata syntax. See formatting help for details. If the option is not used, stattotex will export the statistic as-is.

Documentation

record records the date and time the statistic was created as a comment in the using document, and states that the statistic was produced by stattotex. This is useful mainly for transparency/replication purposes.

comment(string) creates a comment of your choosing in the using document, beside your statistic.

Options record and comment(string) can be specified together without any issues.

Advanced

forcename overrides stattotex's default name checking, which ensures your chosen name isn't an existing LaTeX command.

forcestat overrides stattotex's default statistic checking, which ensures the statistic evaluates to a number. NB: the option format(%fmt) will be ignored if forcestat is specified.

Both forcing options can cause your LaTeX document to break before compiling and thus their use is discouraged.

Examples

The most basic command is this:

. stattotex using "~/Desktop/stattotexexample.tex", statistic(12345) name(statA) replace

This will put the line \newcommand{12345} into the file stattotexexample.tex.

You can choose how you want to format the statistic using normal Stata formatting with the format(%fmt) option:

. stattotex using "~/Desktop/stattotexexample.tex", stat(12345.678) name(statB) replace

This will put the line \newcommand{12345.68} into the file stattotexexample.tex, because the format(%fmt) option was not specified and stattotex rounds to 2 decimal places by default.

. stattotex using "~/Desktop/stattotexexample.tex", stat(12345.678) name(statB) replace format("%9.3f")

Let's try again. This will put the line \newcommand{12345.678} into the file stattotexexample.tex.

. stattotex using "~/Desktop/stattotexexample.tex", stat(12345.003) name(statC) replace f("%9.0fc")

This will put the line \newcommand{12,345} into the file stattotexexample.tex.

You can add the comment of your choice to the statistic using the comment(string) option. This is useful if you want to note where the statistic came from in your code.

. stattotex using "~/Desktop/stattotexexample.tex", stat(12345.67) name(statD) replace comment("Created by the stattotex help file.")

This will put the line \newcommand{12345.678} % Created by the stattotex help file. into the file stattotexexample.tex.

You can also record the exact date and time when your statistic was made using record.

. stattotex using "~/Desktop/stattotexexample.tex", stat(3.14) name(statE) replace record

This will put the line \newcommand{3.14} % Created by stattotex at $S_TIME on $S_DATE. into the file stattotexexample.tex, where $S_TIME and $S_DATE will evaluate to the current time and date.

Including results in LaTeX

To include the output from your stattotex file into your LaTeX document, simply include the line "\input" immediately after "\begin".
Then, if you have a statistic called "maincoef", you can call this statistic in LaTeX using "\maincoef".

Author

Ian Sapollnik

Department of Economics, MIT

ian_s [at] theuniversityabove [dot] edu

https://github.com/isapollnik/stattotex