引用输出表的结果

引用输出表的结果

我一直在想是否有一种最佳方式在论文中完成这项任务。例如,在我的领域,通常会展示 R(如线性回归)的输出表,显示估计的 Beta 值、方差……通常,我会在论文中多次引用原始数字,例如,说估计的 Beta 值等于 0.2。一直写这些数字很不方便,更烦人的是,当我因为一个小小的修正而用不同的估计值更新表格时。所以我不得不查看我写这些数字的所有页面,然后手动更改它们……我相信有一种最佳的方式来做到这一点,例如,\ref{tab:output}[row=1,column=3] 将显示该表第 1 行、第 3 列的内容。

另外我想知道是否有可能,例如我想在论文中显示exp(\ ref{tab:output} [row = 1,column = 3]),它会自动计算并显示这个引用数字的指数。

谢谢

答案1

引用 R 输出?您想知道如何使用knitrR 包进行文学编程。

举个例子:如果你更改下面代码第 10 行和第 11 行的值,则 PDF 文档的所有文本在编译时都会相应更新。重要提示:在 R、Rstudio 或 VS Code 中命名文件example.Rnwwhatever.Rnw编译它,但在 Overleaf 中命名它example.Rtex(免责声明:未经测试)。

\documentclass[twocolumn]{article}
\usepackage{booktabs,parskip}
\usepackage[colorlinks]{hyperref}
\begin{document}

<<yourdata,include=FALSE>>=
#   Change the data as you want,  
#   (but lenght of x and y must be equal)

x = c(1,1,2,2,12,3,15,5,4,6,1,7,9,8,1)
y = c(1,4,1,2,2,2,3,5,8,7,12,77,45,97,1)
df <- data.frame(x,y) 

@

So, you constructed a data frame $df$ with the variables $x$, with values
\Sexpr{knitr::combine_words(x)}, while  the variable $y$ have values of \Sexpr{y[1:4]}, etc.

Now, you can show the whole data frame in \LaTeX, without 
typing yourself the table \ref{tbla}. 

<<tbla,echo=F,results='asis'>>=
library(kableExtra)
kable(df, caption="Your data frame \\label{tbla}", 
      label="tbla", booktabs = T)
@

\newpage

Or show in text that the 4th value of $x$ is 
\Sexpr{df$x[4]} and the 1st value of $y$ is 
\Sexpr{df$y[1]}, or  that $x=1$ and $y=1$ happen  \Sexpr{table(df)[1,1]} times  whereas  when $x=1$ but  $y=4$ or $y=5$ was  \Sexpr{combine_words(table(df)[1,4:5])} times, respectively .


<<,include=F>>=
model <- lm(x~y)
pval <- cor.test(x,y)$p.value
sig  <- if (pval<0.05){""} else "{\\bfseries not }"
@

Or that the correlation between $x$ and $y$ is \Sexpr{sig}significant (p=\Sexpr{round(pval,3)}).  

The linear regression model of $x$ and $y$ have an intercept of approximately  
\Sexpr{round(model$coefficients[1],2)} with a  
$R^2=\Sexpr{round(summary(model)$r.squared,2)}$. More details in table \ref{tblb}. 



<<,echo=F,results='asis'>>=
print(xtable::xtable(summary(model), , label="tblb", caption= "The regression model summary."),caption.placement ="top", booktabs = T, size ="\\footnotesize" )
@

\end{document}

姆韦

答案2

随着读取数组包裹来自史蒂文·B·塞格莱特斯实现起来就容易多了:

\documentclass{article}
\usepackage{readarray}% at least version 3.0

% configure \typesetarray
% from the readarray manual
\renewcommand\typesetplanesepchar{\\\hline}
\renewcommand\typesetrowsepchar{\\\hline}
\renewcommand\typesetcolsepchar{&}

% specify table data
% you can read the data also from a separate txt file
% e.g. \readrecordarray{data.txt}\tableOne
% => see readarray manual
\newcommand\tableOne{%
    1.1 2.1 3.1 4.1
    1.2 2.2 3.2 4.2
    1.3 2.3 3.3 4.3
    1.4 2.4 3.4 4.4
    1.5 2.5 3.5 4.5
}
\readarray\tableOne\dataTableOne[-,4]% 4 columns

% Lua
\usepackage{luacode}
\begin{luacode*}
    function computeExponential2 (value)
        tex.print(string.format("%.5f", math.exp(value)))
    end
\end{luacode*}

\newcommand\computeExp[1]{\luadirect{tex.print(math.exp(#1))}}
\newcommand\computeExpTwo[1]{\luadirect{computeExponential2(#1)}}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} & \textbf{Column 4}\\
        \hline
        \typesetarray\dataTableOne\\
        \hline
    \end{tabular}
    \caption{Example table with some data.}
\end{table}

\noindent
Table One (row 2, column 2): \dataTableOne[2,2]\\
Table One (row 4, column 1): $e^{\dataTableOne[4,1]} = \computeExp{\dataTableOne[4,1]}$\\
Table One (row 4, column 1): $e^{\dataTableOne[4,1]} = \computeExpTwo{\dataTableOne[4,1]}$
\end{document}

第一次尝试:

\documentclass{article}
\usepackage{pgffor}   % foreach
\usepackage{ifthen}   % ifthenelse
\usepackage{etoolbox} % other hacks

% table data via foreach
% based on: https://tex.stackexchange.com/a/165153
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand\eaddtabtoks[1]{\edef\mytmp{#1}\expandafter\addtabtoks\expandafter{\mytmp}}
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother

% specify table data
\newcommand\tableOne{{%
    % entryOne/entryTwo/entryFour/entryFive
    {1.1,2.1,3.1,4.1},%
    {1.2,2.2,3.2,4.2},%
    {1.3,2.3,3.3,4.3},%
    {1.4,2.4,3.4,4.4},%
    {1.5,2.5,3.5,4.5}%
}}
% specify number of rows and columns of the table
% (Perhaps this can also be determined automatically. But I do not know how.
\newcommand\tableOneNumRows{5}
\newcommand\tableOneNumColumns{4}

% Lua
\usepackage{luacode}
\begin{luacode*}
    function computeExponential (value)
        tex.print(math.exp(value))
    end

    function computeExponential2 (value)
        tex.print(string.format("%.5f", math.exp(value)))
    end
\end{luacode*}

\begin{document}
\resettabtoks%
\pgfmathtruncatemacro{\numRows}{\tableOneNumRows-1}%
\pgfmathtruncatemacro{\numColumns}{\tableOneNumColumns-1}%
\foreach \row in {0,...,\numRows} {%
    \foreach \column in {0,...,\numColumns} {%
        \pgfmathsetmacro{\value}{\tableOne[\row][\column]}%
        \eaddtabtoks{\value}%
        \ifthenelse{\column < \numColumns} {%
            \addtabtoks{&}%
        }{%
            \addtabtoks{\\\hline}%
        }%
    }%
}%
\begin{table}
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} & \textbf{Column 4}\\
        \hline
        \printtabtoks
    \end{tabular}
    \caption{Example table with some data.}
\end{table}

\noindent
Table One (row 2, column 2): \pgfmathparse{\tableOne[1][1]}\pgfmathresult{}\\
Table One (row 4, column 1): $e^{\pgfmathparse{\tableOne[3][0]}\pgfmathresult{}} = \pgfmathparse{exp(\tableOne[3][0])}\pgfmathresult{}$\\
% For higher accuracy, we can use Lua, for example.
% Generation with LuaLaTex is necessary!
Table One (row 4, column 1): $e^{\pgfmathparse{\tableOne[3][0]}\pgfmathresult{}} = \pgfmathparse{\tableOne[3][0]}\luadirect{computeExponential("\pgfmathresult")}$\\
Table One (row 4, column 1): $e^{\pgfmathparse{\tableOne[3][0]}\pgfmathresult{}} = \pgfmathparse{\tableOne[3][0]}\luadirect{computeExponential2("\pgfmathresult")}$
\end{document}

输出

相关内容