使用变量时出错

使用变量时出错
\documentclass{article}
\usepackage{xfp}
\usepackage[english]{babel}
%\usepackage{siunitx}
\usepackage{booktabs,multicol,multirow}
\makeatletter
\def\avgthree#1#2#3{\fpeval{(#1+#2+#3)/3}}
\def\valagre#1#2#3{\fpeval{(#1*#2)+#3}}
\makeatother

\newcommand{\tiempo}{2}\newcommand{\tr}{6}\newcommand{\costo}{2}
\newcommand{\tiempotwo}{3}\newcommand{\trtwo}{5}\newcommand{\costotwo}{4}
\newcommand{\tiempothree}{2.5}\newcommand{\trthree}{4}\newcommand{\costothree}{1.98}
\newcommand{\benifCEE}{0.26}

\newcommand{\pesoone}{\fpeval{round(\avgthree{(\tiempo)}{(\tr)}{(\costo)},2)}}
\newcommand{\pesotwo}{\fpeval{round(\avgthree{(\tiempotwo)}{(\trtwo)}{(\costotwo)},2)}}
\newcommand{\pesothree}{\fpeval{round(\avgthree{(\tiempothree)}{(\trthree)}{(\costothree)},2)}}
\newcommand{\vaone}{\fpeval{round(\valagre{(\pesoone)}{(\benifCEE)}{(\costo)},2)}}
\newcommand{\vatwo}{\fpeval{round(\valagretwo{(\pesotwo)}{(\benifCEE)}{(\costotwo)},2)}}
\newcommand{\vathree}{\fpeval{round(\valagrethree{(\pesothree)}{(\benifCEE)}{(\costothree)},2)}}
\begin{document}
In table \ref{added value process} it shows
\begin{table}[!h]
    \begin{tabular}{|l|c|c|c|c|c|c|}\midrule
        Proceso&tiempo&T\_retraso&costo&peso&beneficio&VAgre\\\midrule
        Abastec.& \tiempo\ & \tr\ & \costo\ & \pesoone\  &\benifCEE & \vaone\\\midrule
        Prep.   & \tiempotwo & \trtwo & \costotwo & \pesotwo & \benifCEE & \vatwo \\\midrule
        Calibr. & \tiempothree\ & \trthree\ & \costothree\ & \pesothree\  &\benifCEE & \vathree\\\midrule
    \end{tabular}\\
    \caption{Added value of process}
\label{added value process}
\end{table}
\end{document}

此 .tex 文件打印表格但出现错误。有人能帮我解决吗?

答案1

我认为对每一行使用一个宏会更好:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}

\usepackage{xfp}
\usepackage{siunitx}
\usepackage{booktabs}

\newcommand{\proceso}[5]{%
  #1 & % description
  #2 & % time
  #3 & % cost
  #4 & % delay
  \fpeval{round((#2+#3+#4)/3,2)} & % weight
  #5 & % benefit
  \fpeval{round((#2+#3+#4)*#5/3+#4,2)}
}

\begin{document}

\begin{tabular}{l*{6}{S[table-format=1.2]}}
\toprule
Proceso & {tiempo} & {T\_retraso} & {costo} & {peso} & {beneficio} & {VAgre} \\
\midrule
\proceso{Abastec.}{2}  {6}{2}   {0.26} \\
\proceso{Prep.}   {3}  {5}{4}   {0.26} \\
\proceso{Calibr.} {2.5}{4}{1.98}{0.26} \\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

答案2

将 \valagretwo 和 \valagrethree 更改为 \valagre。

\documentclass{article}
\usepackage{xfp}
\usepackage[english]{babel}
%\usepackage{siunitx}
\usepackage{booktabs,multicol,multirow}
\makeatletter
\def\avgthree#1#2#3{\fpeval{(#1+#2+#3)/3}}
\def\valagre#1#2#3{\fpeval{(#1*#2)+#3}}
\makeatother

\newcommand{\tiempo}{2}\newcommand{\tr}{6}\newcommand{\costo}{2}
\newcommand{\tiempotwo}{3}\newcommand{\trtwo}{5}\newcommand{\costotwo}{4}
\newcommand{\tiempothree}{2.5}\newcommand{\trthree}{4}\newcommand{\costothree}{1.98}
\newcommand{\benifCEE}{0.26}

\newcommand{\pesoone}{\fpeval{round(\avgthree{(\tiempo)}{(\tr)}{(\costo)},2)}}
\newcommand{\pesotwo}{\fpeval{round(\avgthree{(\tiempotwo)}{(\trtwo)}{(\costotwo)},2)}}
\newcommand{\pesothree}{\fpeval{round(\avgthree{(\tiempothree)}{(\trthree)}{(\costothree)},2)}}
\newcommand{\vaone}{\fpeval{round(\valagre{(\pesoone)}{(\benifCEE)}{(\costo)},2)}}
\newcommand{\vatwo}{\fpeval{round(\valagre{(\pesotwo)}{(\benifCEE)}{(\costotwo)},2)}}
\newcommand{\vathree}{\fpeval{round(\valagre{(\pesothree)}{(\benifCEE)}{(\costothree)},2)}}
\begin{document}
In table \ref{added value process} it shows
\begin{table}[!h]
    \begin{tabular}{|l|c|c|c|c|c|c|}\midrule
        Proceso&tiempo&T\_retraso&costo&peso&beneficio&VAgre\\\midrule
        Abastec.& \tiempo\ & \tr\ & \costo\ & \pesoone\  &\benifCEE & \vaone\\\midrule
        Prep.   & \tiempotwo & \trtwo & \costotwo & \pesotwo & \benifCEE & \vatwo \\\midrule
        Calibr. & \tiempothree\ & \trthree\ & \costothree\ & \pesothree\  &\benifCEE & \vathree\\\midrule
    \end{tabular}\\
    \caption{Added value of process}
\label{added value process}
\end{table}
\end{document}

答案3

如果你对不同的方法感兴趣,这里有一个pgfplotstable。这避免了必须定义那么多新的宏。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplotstable} % added
\usepackage{xfp}
\usepackage[english]{babel}
%\usepackage{siunitx}
\usepackage{booktabs}

% don't need \makeatletter (no @), and \newcommand is recommended
\newcommand\avgthree[3]{\fpeval{(#1+#2+#3)/3}}
\newcommand\valagre[3]{\fpeval{(#1*#2)+#3}}


\pgfplotstableread{
Proceso  tiempo Tr costo beneficio
Abastec. 2 6 2 0.26
Prep. 3 5 4 0.26
Calibr. 2.5 4 1.98 0.26
}\datatab

% create peso column
\pgfplotstablecreatecol[
  create col/assign/.code={
    \edef\entry{\avgthree{\thisrow{tiempo}}{\thisrow{Tr}}{\thisrow{costo}}}
    \pgfkeyslet{/pgfplots/table/create col/next content}\entry
  }]{peso}\datatab

% create VAgre column
\pgfplotstablecreatecol[
  create col/assign/.code={
    \edef\entry{\valagre{\thisrow{peso}}{\thisrow{beneficio}}{\thisrow{costo}}}
    \pgfkeyslet{/pgfplots/table/create col/next content}\entry
  }]{VAgre}\datatab

\begin{document}


In table \ref{added value process} it shows
\begin{table}
\centering

\pgfplotstabletypeset[
 % set column order
 columns={Proceso,tiempo,Tr,costo,peso,beneficio,VAgre},
 columns/Proceso/.style={
   string type, % don't parse as numbers
   column type=l % set alignment
 },
 columns/Tr/.style={
   column name={T\_retraso} % change displayer column name from Tr
 },
 every head row/.style={
  before row={\toprule},
  after row={\midrule}
 },
 every last row/.style={
  after row=\bottomrule
 }
]\datatab

\caption{Added value of process}
\label{added value process}
\end{table}
\end{document}

相关内容