Including numerical results and tables¶

At this point you may be thinking “That’s so cool that I can now include figures into my manuscripts! But how exactly does this work for numerical results?”

The reproducible paper linked above shows one way of doing it: After the results are computed, they are written out in the form of a LaTeX table. Here is how one of these tables looks like right after it was computed:

\begin{tabular}{lrrr|rrrr}
Property & HypoParsr & Sniffer & Suitability & Pattern & Type & No Tie & Full\\
\hline
Delimiter & 87.48 & 86.82 & 65.41 & 92.61 & 88.33 & 91.38 & \textbf{94.92}\\
Quotechar & 82.90 & 92.36 & 44.60 & 95.23 & 90.10 & 93.80 & \textbf{97.36}\\
Escapechar & 87.96 & 94.37 & 74.85 & 97.95 & 96.26 & 95.44 & \textbf{99.25}\\
Overall & 80.60 & 85.45 & 38.19 & 90.99 & 83.61 & 90.61 & \textbf{93.75}\\
\hline
\end{tabular}


To include this table into your manuscript, you can use LaTeX’s \input{} function. If the file with the table is called mytable.tex, this command can insert it into your manuscript:

\begin{table}
\input{mytable}
\end{table}


An alternative is to make use of variables. Instead of creating a table in a separate file, you can write a table skeleton and populate it with variables. The results you compute are associated with the variables, and once your manuscript is compiled, variables are exchanged for real numerical results. Here is how such a table looks like in your manuscript:

\begin{table}
\begin{tabular*}{ccc}
\textbf{Variable} & \textbf{Mean}   & \textbf{Std. deviation} \
\hline
Variable 1        & \var1mean       & \var1std                \
Variable 2        & \var2mean       & \var2std                \
\end{tabular*}
\end{table}


Ỳou may notice that \var1mean is no standard LaTeX command: It is a variable that you can define yourself! How is this done? Have your script print the results you compute within a \newcommand{}{} definition into a file, for example like this (simplified Python example):

# this loops to data vectors of two variables (data1, data2), compute the
# mean and standard deviation, and print the results together with the
# variable name ('var1', 'var2')
for name, data in (['var1', data1], ['var2', data2]):
mean = np.mean(data)
std = np.mean(data)
print('\\newcommand{\\%s }{ %f }' % (name + 'mean', mean))
print('\\newcommand{\\%s }{ %f }' % (name + 'std', std))


Let’s say the mean of the first dataset is 9.2, the definition would look like this: \newcommand{\var1mean}{9.2}. Note that this example uses Python, but you can use any language or method you are familiar with to print definitions like this. With this definition, LaTeX exchanges the table cell with \var1mean with the numeric result from the computation. You can capture the definitions and write them to a file using redirection with >. In this example, we write it to a file called results_def.tex

results_def.tex: code/make_summary_stats.py
python code/make_summary_stats.py > results_def.tex


As an alternative to >, you could also redirect the results using the Unix pipe and the tee command (python code/make_summary_stats.py | tee results_def.tex). This will not only redirect the output of the script to a file, but also print them into your terminal. This helpful trick can help you observe whether everything works as expected during the execution of your script.

Finally, use the input{} command to include the new variables in your manuscript and the variables in the tables:

\begin{document}
\input{results_def.tex}


The examples shown here are simplistic, but with a bit of thinking, you can make sure to include results into your manuscript just as they are computed. This helps you (no mistakes copying results to tables, yay!) and makes your research more accessible and reproducible.