我可以使用 csvsimple 包在表格环境中自动显示来自 CSV 文件的数据。是否可以以转置形式(行转为列,反之亦然)实现相同的功能,如第二个手工编码的示例所示?
\begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}
\documentclass{article}
\usepackage{booktabs}
\usepackage{csvsimple}
\begin{document}
\centering
\begin{tabular}{rcccc}
\toprule
\# & Subj J & Subj F & Subj G & Subj T \\
\midrule
\csvreader[head to column names,late after line=\\]{transpose.csv}{}%
{\course & \J & \F & \G & \T}
\bottomrule
\end{tabular}
\vspace{2em}
\begin{tabular}{lcccccccccc}
\toprule
\# & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\midrule
Subj J & 4 & . & . & . & & & & & & 4 \\
Subj F & 4 & . & . & & & & & & . & 2 \\
Subj G & 3 & . & & & & & & . & . & 6 \\
Subj T & 5 & & & & & & . & . & . & 2 \\
\bottomrule
\end{tabular}
\end{document}
图形上:顶部显示当前行为,底部显示所需输出:
答案1
我的第一个想法是使用pgfplotstable
columns
。您可以通过宏中的键选择要绘制哪些列(原始表的列)\pgfplotstabletranspose
。colnames from
键表示我希望转置表的列根据course
原始表列中的值命名。该行在每个课程主题之前postproc cell content
添加。Subj
begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}
\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\begin{document}
\pgfplotstableread[
col sep=comma,
]{transpose.csv}\normal
\pgfplotstabletranspose[
colnames from=course,
columns={course,J,F,G,T},
]\transpose\normal
\begin{center}
\pgfplotstabletypeset[
string type,
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule},
every col no 0/.style={
column type={l},
column name={\#},
postproc cell content/.append style={/pgfplots/table/@cell content/.add={Subj }{}},
},
]\transpose
\end{center}
\end{document}
答案2
我的想法是逐步建立每条数据行的转置行。之后,生成表格:
\begin{filecontents*}{transpose.csv}
course,J,Js,F,Fs,G,Gs,T,Ts
1,4,4,4,4,3,3,5,5
2,3,7,2,6,2,5,4,9
3,4,11,3,9,5,10,2,11
4,2,13,7,16,3,13,7,18
5,7,20,3,19,7,20,7,25
6,5,25,3,23,7,27,2,27
7,3,28,5,28,2,29,3,30
8,6,34,4,32,7,36,3,33
9,4,38,2,34,3,39,7,40
10,4,42,2,36,6,45,2,42
\end{filecontents*}
\documentclass{article}
\usepackage{booktabs,etoolbox}
\usepackage{csvsimple}
\begin{document}
\centering
\begin{tabular}{rcccc}
\toprule
\# & Subj J & Subj F & Subj G & Subj T \\
\midrule
\csvreader[head to column names,late after line=\\]{transpose.csv}{}%
{\course & \J & \F & \G & \T}
\bottomrule
\end{tabular}
\vspace{2em}
% These macros save the data
\def\columnAll{}
\def\courseAll{}
\def\JAll{}
\def\FAll{}
\def\GAll{}
\def\TAll{}
% Reading data and store into the auxiliary macros
\csvreader[head to column names]{transpose.csv}{}%
{\edef\columnAll{\columnAll c}
\edef\courseAll{\courseAll&\course}%
\edef\JAll{\JAll&\J}%
\edef\FAll{\FAll&\F}%
\edef\GAll{\GAll&\G}%
\edef\TAll{\TAll&\T}}%
% Finally: output as table
\begin{tabular}{l\columnAll}
\toprule
\# \courseAll\\
\midrule
Subj J \JAll\\
Subj F \FAll\\
Subj G \GAll\\
Subj T \TAll\\
\bottomrule
\end{tabular}
\end{document}