以编程方式用概率数据填充表格

以编程方式用概率数据填充表格

我有以下包含基本表格的期刊文章模板:


在此处输入图片描述


代码如下所示:

\documentclass[jou,apacite]{apa6}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}       

\title{Title of the article}
\author{Author Name}
\affiliation{Affiliation}

\abstract{This is the abstract.}

\begin{document}

    \maketitle    
    \section{This is the heading for the first section of the article.}
        Lorem ipsum.

        Results are presented in Table~\ref{tab1}.

        \begin{table}[!htb]
            \caption{Sample table.}\label{tab1}
            \begin{tabular}{ccc}
                \hline\\[-1.5ex]
                AAA & BBB & CCC \\[0.5ex]
                \hline\\[-1.5ex]
                1.0 & 2.0 & 3.0\\[0.5ex]
                1.0 & 2.0 & 3.0\\[0.5ex]
                \hline
            \end{tabular}
        \end{table}

    \section{This is the heading for the second section of the article.}
        Lorem ipsum.

\end{document}

我想知道的是:LaTeX 是否能够帮我计算数学运算,而不需要手动输入表格中的每个值,并且可能会在这里或那里输入错误的数字或小数点?

考虑一下抛硬币连续获胜的几率或概率,例如:

每次抛掷的概率都是 50:50(1:1):获胜概率为 50%,失败概率为 50%

连续两次掷骰子获胜的概率为 75:25(3:1):
获胜概率为 25%,失败概率为 75%。

连续三次掷骰子获胜的概率为 87.5:12.5(7:1):
获胜概率为 12.5%,
失败概率为 87.5%。

连续四次掷骰子获胜的概率为 93.75/6.25(15:1):
获胜概率为 6.25%,
失败概率为 93.75%。

..等等。

获胜概率n连续翻转是2^(-n) * 100.
不获胜的概率n连续翻转是100-(2^(-n)) * 100

当然,我可以手动计算每个值并输入每个单元格,但我可能想显示十次或更多次翻转。无论如何,这是 LaTeX 可以做到的吗?

答案1

这是一个基于 LuaLaTeX 的解决方案,用于解决打印运行概率表的问题——如果底层实验是抛出一枚“公平”的硬币。

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass[jou,apacite]{apa6}
\usepackage{amsmath,booktabs,lipsum}  
\let\Pr\relax % undefine "\Pr"
\DeclareMathOperator{\Pr}{Pr}

\usepackage{unicode-math} % choose suitable math and text fonts
\setmainfont{Stix Two Text}[Ligatures={TeX,Common}]
\setmathfont{Stix Two Math}

\title{Title of the article}
\shorttitle{Title}
\author{Author Name}
\affiliation{Affiliation}
\abstract{This is the abstract.}

\usepackage{luacode}
\begin{luacode}
function run_prob ( j )
   return (0.5)^j
end
function printrows ( n )
   for i=1,n do
      runprob     = 100*run_prob(i)
      runprobcomp = 100-runprob  
      tex.sprint ( i .. "&" .. runprob .. "&" .. runprobcomp .. "\\\\" )
   end
end
\end{luacode}

\begin{document}
\maketitle    

\section{In the beginning}
Lorem ipsum.

Results are presented in Table~\ref{tab1}.

\begin{table}[!htb]
\caption{Fair coin tosses: Probabilities of runs of length $n$, in percent.}\label{tab1}

$\begin{array}{@{}lll@{}}
\toprule
n & \Pr(\text{Run}) & 100-\Pr(\text{Run})\\
\midrule
%% Invoke the 'printrows' Lua function:
\directlua{printrows(12)}
\bottomrule
\end{array}$
\end{table}

\lipsum[1-3]
\end{document}

答案2

下面给出了使用 CAS、Sage 的解决方案。CTANsagetex上的文档是这里使用 Sage/LaTeX/sagetex 的示例如下这里。Sage 不是 LaTeX 发行版的一部分,因此需要将其安装在您的机器上或打开免费 Cocalc 帐户(最简单的方法)。以下是代码。

\documentclass[jou,apacite]{apa6}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}       
\usepackage{sagetex}
\title{Title of the article}
\author{Author Name}
\affiliation{Affiliation}
\abstract{This is the abstract.}

\begin{document}
\begin{sagesilent}
output = r"\begin{table}[!htb]"
output += r"\caption{Sample table.}\label{tab1}"
output += r"\begin{tabular}{ccc}"
output += r"\hline"
for i in range(1,13):
    output += r"%s & %s & %s \\"%(i,(100*.5^i).n(digits=7),(100-100*.5^i).n(digits=7))
output += r"\hline"
output += r"\end{tabular}"
output += r"\end{table}"
\end{sagesilent}

\maketitle
\section{This is the heading for the first section of the article.}
The probability of $6$ consecutive flips is given by $\sage{(100*(.5)^6).n(digits=7)}$\%.
The probability of $7$ consecutive flips is given by $\sage{(100*(.5)^7).n(digits=7)}$\%.

Applying this to other values we can create the Table below:
Results are presented in Table~\ref{tab1}.
\sagestr{output}
\section{This is the heading for the second section of the article.}
Lorem ipsum.
\end{document}

Cocalc 中运行的输出如下所示: 在此处输入图片描述 为了创建带有循环的表格,Sage 必须运行。在 Cocalc 中构建文件时,LaTeX 先运行,然后是 Sage,然后是 LaTeX(Sage 运行的结果插入其中)。这意味着,为了编译第一遍 LaTeX,表格不在那里。它会在 Sage 中创建为字符串,然后插入到最终的 LaTeX 编译中。\sagestr{} 获取字符串数据,而 \sage{} 获取数值数据。Sage 有两个不错的功能:能够即时进行计算,例如使用The probability of $6$ consecutive flips is given by $\sage{(100*(.5)^6).n(digits=7)}$\%.where digits=7 设置答案中使用的有效数字的数量,并且 Sage 还可以处理大量概率分布,请参阅这里。最后,Sage 允许您在编码中使用 Python。使用 CAS、Python 和 LaTeX,您几乎可以做任何事情。

相关内容