pgfplotstable:根据输入数据更改为多列行

pgfplotstable:根据输入数据更改为多列行

假设我有大量以简单结构呈现的数据行,.csv并且这些数据行可以分为不同的数据类型。

在 MWE 中我刚刚添加了两种类型(typA& typB)。

如果行包含typA,则应按原样打印表格并显示在我的结果中。

如果行包含typB,则该行应该是多列行,其中只有字段的内容Description打印在中心。

我如何使用 LaTeX 实现这一点?:)

平均能量损失

\documentclass{article}
\usepackage[a4paper, margin=10mm]{geometry}
\usepackage{pgfplotstable}
\usepackage{mwe}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.csv}
Type, Description, X-Pos, Y-Pos
typA, PowerSupply, 3590,  2000
typA, PowerSupply, 3590,  2500
typB, PowerSupply, 4250,  6070
typA, singleSwitch,5700,  6070
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\begin{document}
\begin{center}
\pgfplotstabletypeset[%
font=\ttfamily,%
col sep=comma,%
columns={Type,Description,X-Pos,Y-Pos},%
columns/Type/.style={%
column name=Type,%
string type%
},%
columns/Description/.style={%
column name=Description,%
string type%
},%
columns/X-Pos/.style={%
column name=X-Pos%
},%
columns/Y-Pos/.style={%
column name=Y-Pos,%
column type/.add={}{|}%
},%
column type/.add={|}{},%
after row={\hline},%
every head row/.style={before row=\hline},
]{\csvdata}%
\end{center}
\end{document}

结果

在此处输入图片描述

预期结果

在此处输入图片描述

编辑

我的想法是添加这样的内容:

every column/.style={%
    assign cell content/.code={%
        \pgfplotstablegetelem{\pgfplotstablerow}{Type}\of{\csvdata}
        \ifthenelse{\equal{\pgfplotsretval}{typB}}{%
            \ifthenelse{\equal{\pgfplotstablecol}{2}}{%=> Just do this once!
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{2}{c|}{Content Description}\\\hline}
            }{}
        }{}
    },
},

第二轮

\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.csv}
Type, Description, X-Pos, Y-Pos
typA, PowerSupply, 3590,  2000
typA, PowerSupply, 3590,  2500
typB, PowerSupply, 4250,  6070
typA, singleSwitch,5700,  6070
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\begin{document}
\pgfplotstabletypeset[%
font=\ttfamily,%
col sep=comma,%
columns={Type,Description,X-Pos,Y-Pos},%
columns/Type/.style={%
column name=Type,%
string type%
},%
every column/.style={%
    assign cell content/.code={%
        \pgfplotstablegetelem{\pgfplotstablerow}{Type}\of{\csvdata}
        \ifthenelse{\equal{\pgfplotsretval}{typB}}{%
            \ifthenelse{\equal{\pgfplotstablecol}{2}}{%=> Just do this once!
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{2}{c|}{Content Description}\\\hline}
            }{}
        }{}
    },
},
columns/Description/.style={%
    column name=Description,%
    string type%
},%
columns/X-Pos/.style={%
    column name=X-Pos%
},%
columns/Y-Pos/.style={%
    column name=Y-Pos,%
    column type/.add={}{|}%
},%
column type/.add={|}{},%
after row={\hline},%
every head row/.style={before row=\hline},
]{\csvdata}%
\end{document}

结果

在此处输入图片描述

但我怎样才能

  1. 从第一列开始的多列
  2. 隐藏第二行的 Y-Pos 信息
  3. Content Description在这种情况下,用描述的真实内容替换PowerSupply

答案1

csvsimple-l3和的解决方案tabularray您能接受吗?

\documentclass{article}
\usepackage[a4paper, margin=10mm]{geometry}

\usepackage{csvsimple-l3}

\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\sisetup{
  group-digits=integer,
  group-minimum-digits={3},
  group-separator={,}
  }

\begin{filecontents*}{data.csv}
Type, Description, X-Pos, Y-Pos
typA, PowerSupply, 3590,  2000
typA, PowerSupply, 3590,  2500
typB, PowerSupply, 4250,  6070
typA, singleSwitch,5700,  6070
\end{filecontents*}

\begin{document}
\csvreader[no head,
centered tabularray =
{
colspec={cc*2{S[table-format=4]}}, 
columns={font=\ttfamily},
hlines,
vlines,
},
]{data.csv}{}{%
\ifcsvfirstrow{% header
  \csvcoli & \csvcolii
  & {{{\csvcoliii}}}
  & {{{\csvcoliv}}}
    }{% rows
  \IfCsvsimStrEqualTF{\csvcoli}{typB}{% multicolumn
    \SetCell[c=4]{c}\csvcolii&&&}{% other rows
    \csvcoli & \csvcolii
    & \csvcoliii
    & \csvcoliv}
        }
    }
\end{document}

在此处输入图片描述

您还可以使用csvsimple-l3普通的tabular,但要使用技巧,即第五个虚拟列,以避免siunitx最后一列出现列类型问题:

\documentclass{article}
\usepackage[a4paper, margin=10mm]{geometry}

\usepackage{csvsimple-l3}
\usepackage{array}
\renewcommand{\arraystretch}{1.3}
\usepackage{siunitx}
\sisetup{
  group-digits=integer,
  group-minimum-digits={3},
  group-separator={,},
    table-number-alignment=center
  }

\begin{filecontents*}{data.csv}
Type, Description, X-Pos, Y-Pos
typA, PowerSupply, 3590,  2000
typA, PowerSupply, 3590,  2500
typB, PowerSupply, 4250,  6070
typA, singleSwitch,5700,  6070
\end{filecontents*}

\begin{document}
\csvreader[
  head to column names,
  before reading = {\begin{center}\ttfamily},
  tabular = {|c|c|S[table-format=4]|S[table-format=4]@{}c|},% the last dummy column is necessary
    % due to csvreader-l3 problems, see documentation, Section 7.4 Tables with Number Formatting
  table head = {\hline Type & Description & {X-Pos} & {Y-Pos}&\\\hline},
  late after line = \\\hline,
  after reading = \end{center}
  ]{data.csv}{}{%
  \IfCsvsimStrEqualTF{\csvcoli}{typB}{% multicolumn
    \multicolumn{5}{|c|}{\csvcolii}
        }{% other rows
    \csvcoli & \csvcolii
    & \csvcoliii
    & \csvcoliv &
        }
  }
\end{document}

在此处输入图片描述

NB = 该宏\IfCsvsimStrEqualTF非常新。如果您的发行版不是最新的,请使用\ifcsvstrcmp。在上面两个例子中,结果不会因旧宏而改变。

答案2

以下是针对 OpTeX 用户的解决方案:

\createfile {data.csv}
Type, Description, X-Pos, Y-Pos
typA, PowerSupply, 3590,  2000
typA, PowerSupply, 3590,  2500
typB, PowerSupply, 4250,  6070
typA, singleSwitch,5700,  6070
\endfile

\def\p #1,#2,#3,#4#5 {%
   \ifx^#1^\else
      \isequal{typB}{#1}
      \iftrue
         \mspan4[|c|]{#2} \crl
      \else
         #1 & #2 & #3 & #4#5 \crl
      \fi
      \ea\p
   \fi
}

{\everyeof{,,,{} }
\table{|c|c|c|c|}{\crl
   \ea\p\input data.csv
}}

\bye

相关内容