\pgfplotstabletypeset 包装器宏内部

\pgfplotstabletypeset 包装器宏内部

我目前正在尝试编写一个包装器,\pgfplotstabletypeset但很快就陷入了困境。

这是我的 MWE:

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{filecontents}


\newcommand{\insertTable}[1]{
    \pgfplotstabletypeset[%
        every head row/.style={output empty row},
        every first row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule},
        header = false,
        /pgfplots/table/assign cell content/.code={
            \IfDecimal{#1}{ 
                % format as number 
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{#1}}}
            }{ 
                % use as String -> has to span two columns if col sep align is activated
                \ifnum\pgfplotstablepartno=0%
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1}
                \fi
            }
        },
        col sep = comma, 
        use comma,
    ]{#1}
}

\begin{document}
    \begin{filecontents}{Sample.csv}
        Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7
        1.34, 2.73, 358000, 0.003, 5, 1500, 0.000374
        1.34, 2.73, 358000, 0.003, 7, 1325.4, 0.012356
        1.34, 2.734, 358000, 0.003, 12, 998.331, 0.12345
        1.34, 5.073, 358000, 0.003, 17, 12353.274583, 0.0505134567
        1.34, 22.77, 358000, test, 356, 9999.99, 0.3
    \end{filecontents}

    \pgfplotstabletypeset[%
        every head row/.style={output empty row},
        every first row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule},
        header = false,
        /pgfplots/table/assign cell content/.code={
            \IfDecimal{#1}{ 
                % format as number 
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{#1}}}
            }{ 
                % use as String -> has to span two columns if col sep align is activated
                \ifnum\pgfplotstablepartno=0%
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1}
                \fi
            } 
        },
        col sep = comma, 
        use comma,
    ]{Sample.csv}

    \insertTable{Sample.csv}
\end{document}

如您所见,我正在将其定义\insertTable为我的包装器宏。但是,我遇到了一种我无法真正向自己解释的行为:
编译上述文档时,我得到了两个表格(如预期的那样)。第一个表格(直接使用生成)\pgfplotstabletypeset包含我想要的数据。然而,第二个表格的行数和列数完全相同,但每个单元格都填充了文本Sample.csv
如您在上面的代码中所见,\pgfplotstabletypeset在两种情况下使用的调用是相同的。

我能够将问题追溯到我自己分配单元格内容。如果我删除该/pgfplots/table/assign cell content/.code = {...}块,一切都会按预期工作(我得到了预期的错误,因为我没有明确使用string type)。

当前的行为表明,传递给单元格内容分配代码的参数在两种情况下是不同的。在情况 1 中,这是预期的未格式化单元格内容,在情况 2 中,在每种情况下都是文件名。

为什么会这样?我该如何解决这个问题?

答案1

这是一个常见的错误。#1用作您定义的宏所给参数的占位符。如果您需要在此宏内定义宏(或访问应提供给宏的参数),则必须使用#额外的 来转义 ,因此#1变成##1

您必须使用##1应该考虑的参数\pgfplotstabletypeset,而不是的参数\insertTable

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{filecontents}


\newcommand{\insertTable}[1]{
    \pgfplotstabletypeset[%
        every head row/.style={output empty row},
        every first row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule},
        header = false,
        /pgfplots/table/assign cell content/.code={
            \IfDecimal{##1}{ 
                % format as number 
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{##1}}}
            }{ 
                % use as String -> has to span two columns if col sep align is activated
                \ifnum\pgfplotstablepartno=0%
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}
                \fi
            }
        },
        col sep = comma, 
        use comma,
    ]{#1}
}

\begin{document}
    \begin{filecontents}{Sample.csv}
        Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7
        1.34, 2.73, 358000, 0.003, 5, 1500, 0.000374
        1.34, 2.73, 358000, 0.003, 7, 1325.4, 0.012356
        1.34, 2.734, 358000, 0.003, 12, 998.331, 0.12345
        1.34, 5.073, 358000, 0.003, 17, 12353.274583, 0.0505134567
        1.34, 22.77, 358000, test, 356, 9999.99, 0.3
    \end{filecontents}

    \pgfplotstabletypeset[%
        every head row/.style={output empty row},
        every first row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule},
        header = false,
        /pgfplots/table/assign cell content/.code={
            \IfDecimal{#1}{ 
                % format as number 
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{#1}}}
            }{ 
                % use as String -> has to span two columns if col sep align is activated
                \ifnum\pgfplotstablepartno=0%
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1}
                \fi
            } 
        },
        col sep = comma, 
        use comma,
    ]{Sample.csv}

    \insertTable{Sample.csv}
\end{document}

相关内容