表中不同列的精度

表中不同列的精度

我有以下 MWE,其中我构建了包含 3 列的表格。第二列是 x 值,第三列是 y=f(x) 值。问题是:如何更改第 2 列和第 3 列之间的精度(定义为小数点后六位)?例如,我希望第 2 列只有两位小数,而第 3 列有 6 位小数。

\documentclass[tikz,border=5]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{math}
\usepackage{etoolbox}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usepackage{booktabs}
\usetikzlibrary{calc,fpu}


\begin{document}

\tikzset{fixed point arithmetic}

\tikzmath{
    function f(\x) {
        \y =  \x*exp(\x);
    return \y; 
    };
   \n=10;%nU deve ser par, Número de intervalos 1/3 Simpson
    \a=0;
    \b=1;
    \h=(\b-\a)/\n;
     int \i;
    for \i in {0,...,\n}{
        \x{\i} = \a+\i*\h;
        \y{\i} = f(\a+\i*\h);
    };
}



\pgfkeys{/pgf/number format/.cd,fixed,precision=6,zerofill}%

\def\tmp{$i$ & $x_i$ & $y_i=f(x_i)$ \\ \midrule}%
\edef\k{0}%
\loop
\xappto\tmp{$\k$ & $\noexpand\pgfmathprintnumber{\x{\k}}$ &  $\noexpand\pgfmathprintnumber{\y{\k}}$}%
\edef\k{\the\numexpr\k+1}%
\gappto\tmp{\\}%
\ifnum\k<\fpeval{\n+1}\repeat

\begin{tikzpicture}
\node at (0,0) {\parbox{10cm}{

Primeiro, montamos a tabela a seguir:\\

\begin{tabular}{r|r|r}
\toprule
\tmp
\bottomrule
\end{tabular}\\[0.5cm]
Faremos os cálculos do seguinte modo: no intervalo $[x_0,x_{\n}]$ aplicaremos a $1/3$ de Simpson com $\n$ subintervalos. 
}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

有人能帮助我吗?

答案1

我建议您使用该siunitx包及其S列类型来完成这项工作。

在此处输入图片描述

\documentclass[tikz,border=5]{standalone}
\usepackage{booktabs,siunitx}
\usepackage{tikz,fp}
\usetikzlibrary{math}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{calc,fpu}

\begin{document}

\tikzset{fixed point arithmetic}
\tikzmath{
    function f(\x) {
        \y =  \x*exp(\x);
    return \y; 
    };
   \n=10;%nU deve ser par, Número de intervalos 1/3 Simpson
    \a=0;
    \b=1;
    \h=(\b-\a)/\n;
    int \i;
    for \i in {0,...,\n}{
        \x{\i} = \a+\i*\h;
        \y{\i} = f(\a+\i*\h);
    };
}

%% not needed anymore:
%\pgfkeys{/pgf/number format/.cd,fixed,precision=6,zerofill}

% initialize "\tmp", then fill
\def\tmp{$i$ & {$x_i$} & {$y_i=f(x_i)$} \\ \midrule}
\def\k{0}
\loop
  \xappto\tmp{\k & \x{\k} & \y{\k}}
  \edef\k{\the\numexpr\k+1}
  \gappto\tmp{\\}
  \ifnum\k<\fpeval{\n+1}
\repeat

\begin{tikzpicture}
\node at (0,0) {\parbox{10cm}{%
Primeiro, montamos a tabela a seguir:
\begin{center}
\sisetup{round-mode=places,group-digits=false}
\begin{tabular}{@{} 
   r
   S[table-format=1.1,round-precision=1]
   S[table-format=1.6,round-precision=6] 
   @{}}
\toprule
\tmp
\bottomrule
\end{tabular}
\end{center}
Faremos os cálculos do seguinte modo: no intervalo $[x_0,x_{\n}]$ aplicaremos a $1/3$ de Simpson com $\n$ subintervalos. 
}};
\end{tikzpicture}

\end{document}

附录:我不得不说,加载tikz包来执行一些简单的计算和基本循环似乎有点过分。一个好的(而且实现起来更简单的)替代方案是通过\directlua调用来使用 LuaLaTeX:

\[
\begin{array}{@{} 
  c c
  S[group-digits=false, table-format=1.6,
    round-mode=places, round-precision=6]
  @{}}
\toprule
i & x_i & {f(x_i)} \\
\midrule
\directlua{
   for i = 0 , 10 do
      xi = i/10
      yi = xi * math.exp(xi)
      tex.sprint ( i .. "&" .. xi .. "&" .. yi .. "\\\\" )
   end
}
\bottomrule
\end{array}
\]

不需要加载tikzfp包,也不需要各种\usetikzlibrary指令。:-)

我不会发布截图,因为输出与之前显示的相同。

相关内容