Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to combine two string columns into one column. The contents of the columns should be in two lines, i.e., first column's content must be in the first row of the combined column and second column's content must be in the second row of the combined column. I have to use multirow environment since the number of rows belonging to a ColA and ColB pair is variable. For example, it is 3 in the following sample table, 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}

This code prints:

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

What I need is:

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

Thanks in advance!

share|improve this question
add comment

2 Answers

enter image description here

As always to put multi-line stuff in an l tabular column you need to nest a table or parbox:

    \xdef\entry{\noexpand\begin{tabular}{@{}l@{}}\thisrow{ColA}\noexpand\\
                         \thisrow{ColB}\noexpand\end{tabular}} 
share|improve this answer
add comment
up vote 0 down vote accepted

I have found a workaround that imitates 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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.