使用表中的值更新文本中的值

使用表中的值更新文本中的值

我正在使用 latex 进行研究,但确实遇到了一个问题。我希望能够使用表中的值自动更新我在文本中提到的值,这样当表中的值发生变化时,我在文本中提到的值也会发生变化。我偶然发现了这篇有用的帖子:通过“网格搜索”访问表中的值 但我仍然很难将它应用于下表

\newcommand{\specialcell}[2][c]{\begin{tabularx}[#1]{@{}c@{}}#2\end{tabularx}}
\footnotesize
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\begin{tabularx} {15cm} {@{} l Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y@{}} \\
\toprule
\toprule
\textbf{province}&\textbf{N}&\textbf{Sum}&\textbf{N}&\textbf{Sum} \\
\cmidrule(l{.75em}){2-5} 
&Built dataset& &Scotts database&  \\
\midrule
Alberta&812&3.9&2790&8.5 \\
British Columbia&2966&14.3&3847&11.8 \\
Manitoba&630&3.0&1014&3.1 \\
New Brunswick&408&2.0&713&2.2 \\
Newfound. \& Labr.&63&0.3&288&0.9 \\
New Scotia&458&2.2&774&2.4 \\
North Territories&1&0.0&7&0.0 \\
Nunavut&3&0.0&6&0.0 \\
Ontario&9040&43.5&13788&42.2 \\
Prince Edward Isl.&30&0.1&145&0.4 \\
Quebec&5968&28.7&8452&25.9 \\
Saskatchewan&393&1.9&844&2.6 \\
Yukon&9&0.0&22&0.1 \\
\textbf{Total}&20781&100.0&32690&100.0 \\
\bottomrule
\bottomrule
\addlinespace[.75ex]
\end{tabularx}
\par
\normalsize

有人能帮助我吗?

答案1

如果表的结构相同,可以执行以下操作:

\documentclass{article}
\usepackage{xparse,siunitx,booktabs}

\newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

\ExplSyntaxOn

\NewDocumentCommand{\definedata}{mm}
 {% #1 = symbolic name, #2 = data
  % data is in the form province string/province name/N1/Sum1/N2/Sum2
  % comma separated
  \prop_new:c { g_bieda_data_#1_prop }
  \clist_map_inline:nn { #2 }
   {
    \__bieda_data_populate:nn { #1 } { ##1 }
   }
 }

\NewDocumentCommand{\printprovince}{mm}
 {
  \tl_set:Nx \l__bieda_data_row_tl
   {
    \prop_item:cn { g_bieda_data_#1_prop } { #2      } &
    \prop_item:cn { g_bieda_data_#1_prop } { #2-N1   } &
    \prop_item:cn { g_bieda_data_#1_prop } { #2-Sum1 } &
    \prop_item:cn { g_bieda_data_#1_prop } { #2-N2   } &
    \prop_item:cn { g_bieda_data_#1_prop } { #2-Sum2 }
   }
  \tl_use:N \l__bieda_data_row_tl
 }

\NewExpandableDocumentCommand{\retrievedata}{mmm}
 {
  \prop_item:cn { g_bieda_data_#1_prop } { #2-#3 }
 }

\tl_new:N \l__bieda_data_row_tl
\seq_new:N \l__bieda_data_row_seq

\cs_new_protected:Nn \__bieda_data_populate:nn
 {
  \seq_set_split:Nnn \l__bieda_data_row_seq { / } { #2 }
  \prop_gput:cxx { g_bieda_data_#1_prop }
   { \seq_item:Nn \l__bieda_data_row_seq { 1 } }
   { \seq_item:Nn \l__bieda_data_row_seq { 2 } }
  \prop_gput:cxx { g_bieda_data_#1_prop }
   { \seq_item:Nn \l__bieda_data_row_seq { 1 } - N1 }
   { \seq_item:Nn \l__bieda_data_row_seq { 3 } }
  \prop_gput:cxx { g_bieda_data_#1_prop }
   { \seq_item:Nn \l__bieda_data_row_seq { 1 } - Sum1 }
   { \seq_item:Nn \l__bieda_data_row_seq { 4 } }
  \prop_gput:cxx { g_bieda_data_#1_prop }
   { \seq_item:Nn \l__bieda_data_row_seq { 1 } - N2 }
   { \seq_item:Nn \l__bieda_data_row_seq { 5 } }
  \prop_gput:cxx { g_bieda_data_#1_prop }
   { \seq_item:Nn \l__bieda_data_row_seq { 1 } - Sum2 }
   { \seq_item:Nn \l__bieda_data_row_seq { 6 } }
 }
\cs_generate_variant:Nn \prop_gput:Nnn { cxx }

\ExplSyntaxOff

\begin{document}

\begin{table}[htp]

\definedata{first}{
  AB/Alberta/812/3.9/2790/8.5,
  BC/British Columbia/2966/14.3/3847/11.8,
  MB/Manitoba/630/3.0/1014/3.1,
  NB/New Brunswick/408/2.0/713/2.2,
  NL/Newfound. Labr./63/0.3/288/0.9,
  NS/New Scotia/458/2.2/774/2.4,
  NT/Northwest Territories/1/0.0/7/0.0,
  NU/Nunavut/3/0.0/6/0.0,
  ON/Ontario/9040/43.5/13788/42.2,
  PE/Prince Edward Isl./30/0.1/145/0.4,
  QC/Quebec/5968/28.7/8452/25.9,
  SK/Saskatchewan/393/1.9/844/2.6,
  YT/Yukon/9/0.0/22/0.1,
  TOT/\textbf{Total}/20781/100.0/32690/100.0
}

\sisetup{group-minimum-digits=4}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  S[table-format=5.0]
  S[table-format=3.1]
  S[table-format=5.0]
  S[table-format=3.1]
  @{}
}
\toprule
\textbf{Province} &
\multicolumn{2}{c}{Built dataset} & \multicolumn{2}{c@{}}{Scotts database} \\
\cmidrule(r){2-3}\cmidrule(l){4-5}
&
{\textbf{N}} &
{\textbf{Sum}} &
{\textbf{N}} &
{\textbf{Sum}} \\
\midrule
\printprovince{first}{AB} \\
\printprovince{first}{BC} \\
\printprovince{first}{MB} \\
\printprovince{first}{NB} \\
\printprovince{first}{NL} \\
\printprovince{first}{NS} \\
\printprovince{first}{NT} \\
\printprovince{first}{NU} \\
\printprovince{first}{ON} \\
\printprovince{first}{PE} \\
\printprovince{first}{QC} \\
\printprovince{first}{SK} \\
\printprovince{first}{YT} \\
\printprovince{first}{TOT} \\
\bottomrule
\end{tabular*}

\end{table}

We can retrieve the data: in Alberta, N1 is \retrievedata{first}{AB}{N1}.

\end{document}

数据被打包在属性列表中,您可以根据需要拥有任意数量的数据。

在此处输入图片描述

答案2

一个选项可能是使用pgfplotstable。这样您可以先使用 读取表格数据\pgfplotstableread,然后使用 排版该表格\pgfplotstabletypeset。可以使用 访问特定单元格\pgfplotstablegetelem(在下面的示例中放入一个新宏)。

可能需要一些时间去适应并弄清楚如何按照您喜欢的方式制作桌子。

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}

\pgfplotstableread[
  col sep=semicolon,
]{
Province;N1;S1;N2;S2
Alberta;812;3.9;2790;8.5
British Columbia;2966;14.3;3847;11.8
Manitoba;630;3.0;1014;3.1
New Brunswick;408;2.0;713;2.2
Newfound. Labr.;63;0.3;288;0.9
New Scotia;458;2.2;774;2.4
Northwest Territories;1;0.0;7;0.0
Nunavut;3;0.0;6;0.0
Ontario;9040;43.5;13788;42.2
Prince Edward Isl.;30;0.1;145;0.4
Quebec;5968;28.7;8452;25.9
Saskatchewan;393;1.9;844;2.6
Yukon;9;0.0;22;0.1
\textbf{Total};20781;100.0;32690;100.0
}\FirstDataset

\pgfplotstableset{
  ints/.style={
    dec sep align,
    column name=#1,
    1000 sep=\,
  },
  ints/.default={},
  1dec/.style={
    dec sep align,
    fixed,
    fixed zerofill,
    precision=1,
    column name=#1
  },
  1dec/.default={}
}

\newcommand\getvaluefromtable[4][]{%
  \pgfplotstablegetelem{#3}{#4}\of#2%
  \pgfmathprintnumber[1000 sep=\,,#1]{\pgfplotsretval}%
}

\begin{document}

\pgfplotstabletypeset[
  every head row/.style={
    before row={
      \toprule
     & \multicolumn{4}{c}{Built dataset} & \multicolumn{4}{c}{Scott's database} \\ \cmidrule(lr){2-5} \cmidrule(lr){6-9}
    },
   after row={\midrule}
  },
  every last row/.style={after row=\bottomrule},
  columns/Province/.style={
    string type,
    column type=l,
    column name=\textbf{Province}
  },
  columns/N1/.style={ints=\textbf{N}},
  columns/S1/.style={1dec=\textbf{Sum}},
  columns/N2/.style={ints=\textbf{N}},
  columns/S2/.style={1dec=\textbf{Sum}}
]{\FirstDataset}

\bigskip

Access values by table name, row number and column name, \textit{e.g.}  \getvaluefromtable{\FirstDataset}{10}{N2}

You can use a numeric index for the column as well: \getvaluefromtable{\FirstDataset}{1}{[index]2}.
Indices are zero-based, hence 14.3 for row 1, column 2.

\end{document}

在此处输入图片描述

相关内容