I'm writing a paper that involves many values. In order to be consistent in all the papers, I'm using \def and xint to assign and calculate these values. However, in part of this paper I have to present some descriptive statistics about a dataset. And each time that one value in the dataset change, I have to recalculate all descriptive statistics and change many \def. Then, my question is: Is possible to include the dataset in the latex and calculate all descriptive statistics (mean, mediam, min, max, and sum)?

for instance, given the dataset [1,2,3,4,5,6,7]

I could calculate Sample Size: 7, Mean: 4, Minimum: 1, Maximum: 7, Median: 4

  • Welcome to TeX.SX! That is an interesting question.
    – moewe
    Apr 22, 2016 at 13:22
  • I think R program suits for calculation of summaries better. It has knitr or sweave compatible to LaTeX
    – Olga K
    Apr 22, 2016 at 14:06
  • r-project.org, rstudio.com
    – Olga K
    Apr 22, 2016 at 14:15
  • Do you always supply the data set in non-decreasing order?
    – Werner
    Apr 22, 2016 at 14:34
  • Hello Olga, initially I solve my problem with the sagemath. However, Sagemath cloud site is not enoht to my needs. Then I could know that the Sharelatex now support natively the Knitr. And I did all calculation using this package. May 25, 2016 at 19:37

1 Answer 1


If you're mixing math and LaTeX you should consider looking into the sagetex package which gives you access to a computer algebra system, called Sage, to handle the math. Documentation on basic statistics is here. You'll need Sage installed locally on your computer or, better yet, you use the free Sagemath Cloud site. In that case, no Sage to download and install.

MyData = [1,2,3,4,5,6,7]

\noindent My data set is $S = \sage{MyData}$. For this data:\\
The sample size is $\sage{len(MyData)}$.\\
The mean is $\sage{mean(MyData)}$.\\
The median is $\sage{median(MyData)}$.\\
The minimum value is $\sage{min(MyData)}$.\\
The maximum value is $\sage{max(MyData)}$.\\
The standarad deviation of the sample is $\sage{std(MyData)}$.\\
The sum of the data values is $\sage{sum(MyData)}$.

The output is shown running in Sagemath Cloud; as you can see, the code is short and easily understood. enter image description here

EDIT: I forgot to compute the median. That is easily accomplished with an extra line.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .