如何让 pgfplotstable 使用宏读取表格

如何让 pgfplotstable 使用宏读取表格

我想将我的表格迁移到 pgfplotstables。我以前有一个用于表格行的宏,部分原因是 Kile 以前也这样做。

因此目前,我的表格或多或少看起来像:

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}

我想将其迁移到类似以下的内容:

newtable:
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}

但此操作失败并出现以下错误:

./main.tex:28: Package pgfplots Error: input table '<inline_table>' has an unba
lanced number of columns in row '1' (expected '3' cols; got '1'). Maybe the inp
ut table is corrupted? If you need unbalanced data, consider using 'nan' in emp
ty cells (perhaps combined with 'unbounded coords=jump').

------- PGFPLOTSTABLE DEBUG MODE: --------
./main.tex:28: Undefined control sequence.
\\  ->\let \reserved@e 
                       \relax \let \reserved@f \relax \@ifstar {\let \reserv...
l.28 }

我想这是因为宏没有展开。我不一定想编辑所有表格。所以我的问题是:如何让 pgfplotstables 读取我的表定义?

完整示例:

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

% global settings
\pgfplotstableset{
    col sep = &,
    row sep=\\,
    %string type, %% The error changes when enabling this type
}

\newcommand{\myrow}{foo & bar & baz\\}
\newcommand*{\rowentry}[6]{#1 & #2 & #3\\} %& #4}\\}
\begin{document}

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}

newtable:
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}

\end{document}

答案1

在此处输入图片描述

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

% global settings
\pgfplotstableset{
    col sep = &,
    row sep=\\,
    string type, %% The error changes when enabling this type
}

\newcommand{\myrow}{foo & bar & baz\\}
\newcommand*{\rowentry}[6]{#1 & #2 & #3\\} %& #4}\\}

\def\foo#1[#2]#3{{
\let\\\relax
\gdef\tmpa{#1[#2]}%
\xdef\tmpb{\noexpand\tmpa{#3}}}%
\tmpb}

\begin{document}

oldtable:
\begin{tabular}{ccc}
1&2&3\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
\end{tabular}

\foo
\pgfplotstabletypeset[debug]{
one & two & three\\
\myrow{}
\rowentry{1}{2}{3}{4}{5}{6}
}

\end{document}

相关内容