pgfplotstable 中多行环境内的换行

pgfplotstable 中多行环境内的换行

我想将两个字符串列合并为一列。列的内容应为两行,即第一列的内容必须位于合并列的第一行,第二列的内容必须位于合并列的第二行。我必须使用multirow环境,因为属于ColAColB对的行数是可变的。例如,在下面的示例表中,它是 3 table.txt

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}
\begin{filecontents}{table.txt}
ColA    ColB ColX
A   C   1
A   C   2
A   C   3
B   D   4
B   D   5
B   D   6
\end{filecontents}
\begin{document}
\def\numps{3}
\def\numpsMONE{2}
\pgfplotstableread{table.txt}\table
\pgfplotstabletypeset[columns={Combined, ColX}, columns/ColA/.style={string type},columns/ColB/.style={string type},create on use/Combined/.style={string type, column name={Combined}, column type=l, create col/assign/.code={
    \xdef\entry{\thisrow{ColA} \thisrow{ColB}} 
        % I cannot put \\ between thisrow{ColA} and thisrow{ColB} 
    \edef\empty{}
    \pgfmathparse{Mod(\pgfplotstablerow,\numps) == 0 ? 1 : 0}
    \ifnum\pgfmathresult=1
%       \pgfkeyslet{/pgfplots/table/create col/next content}\entry
            % If I use \pgfkeyslet, A C is printed, however I cannot use multirow
        \pgfkeyssetvalue{/pgfplots/table/create col/next content}{\multirow{\numpsMONE}{*}{\entry}}
            % If I use \pgfkeyssetvalue,B D is printed but A C is not printed
    \else
        \pgfkeyslet{/pgfplots/table/create col/next content}\empty
    \fi
}},columns/Combined/.style={string type},]\table 
\end{document}

此代码打印:

Combined ColX
         1
B D      2
         3
         4
B D      5
         6

我需要的是:

Combined ColX
         1
A        2
C        3
         4
B        5
D        6

提前致谢!

答案1

在此处输入图片描述

与往常一样,要将多行内容放入l表格列中,您需要嵌套表格或 parbox:

    \xdef\entry{\noexpand\begin{tabular}{@{}l@{}}\thisrow{ColA}\noexpand\\
                         \thisrow{ColB}\noexpand\end{tabular}} 

答案2

我找到了一种模仿的解决方法multirow

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}
\begin{filecontents}{table.txt}
ColA    ColB ColX
A   C   1
A   C   2
A   C   3
B   D   4
B   D   5
B   D   6
\end{filecontents}
\begin{document}
\def\numps{3}
\def\numpsMONE{2}
\pgfplotstableread{table.txt}\table
\pgfplotstabletypeset[columns={Combined, ColX}, columns/ColA/.style={string type},columns/ColB/.style={string type},create on use/Combined/.style={string type, column name={Combined}, column type=l, create col/assign/.code={
\xdef\entrya{\thisrow{ColA}}
\xdef\entryb{\thisrow{ColB}}
\edef\empty{}
\pgfmathparse{Mod(\pgfplotstablerow,\numps) == 1 ? 1 : 0}
\ifnum\pgfmathresult=1
   \pgfkeyslet{/pgfplots/table/create col/next content}\entrya
\else
    \pgfmathparse{Mod(\pgfplotstablerow,\numps) == 2 ? 1 : 0}
    \ifnum\pgfmathresult=1
       \pgfkeyslet{/pgfplots/table/create col/next content}\entryb
    \else
        \pgfkeyslet{/pgfplots/table/create col/next content}\empty
    \fi
\fi
}},columns/Combined/.style={string type},]\table 
\end{document}

相关内容