我用它pgfplotstable
来将我的 CSV 表输入到 LaTeX 中。问题是我使用两行作为标题,第一行介绍我的变量,第二行介绍它们的单位。下面的代码MWE
完全可以满足我的目标,但让我烦恼的是,我必须重复every row #1 column 0/.append style={#2}
很多次才能确保在输入具有任意列数的表时此代码仍然有效。我试图将这一行放在循环中,但即使检查了这里的问题和其他来源,我每次都失败了。
\documentclass[a4paper]{article}
\usepackage{filecontents}
\begin{filecontents*}{mytable.csv}
Number,$\mathrm{f}$,$\Delta\mathrm{f}$
,\si{\kHz},\si{\kHz}
1,1000,0.1
2,3000,0.2
3,5000,0.3
\end{filecontents*}
\usepackage{pgfplotstable,booktabs,array,pgfgantt,
pgfplots,colortbl,gensymb,siunitx}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\pgfplotsset{compat=1.12}
\pgfplotstableset{
rowstyle/.style 2 args={
every row #1 column 0/.append style={#2},
every row #1 column 1/.append style={#2},
every row #1 column 2/.append style={#2},
every row #1 column 3/.append style={#2},
every row #1 column 4/.append style={#2},
},
colstyle/.style 2 args={
every col no #1/.append style={#2},
},
header=false,
col sep = comma,
every head row/.style={output empty row},
every first row/.style={before row=\toprule},
every row no 1/.style ={after row=\midrule},
every even row/.style={before row=\rowcolor{lightgray}},
rowstyle={0}{string type},
rowstyle={1}{string type},
}
\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
fixed, fixed zerofill, precision=3,
columns/0/.append style={precision=0},
]
{mytable.csv}
\caption{Caption of my table}
\end{table}
\end{document}
所以,最后我有两个问题:
假设我输入一个大数作为最大列数(例如 50),我怎样才能将其放入循环中而不必重复 50 次?
有没有办法让宏遍历 CSV 文件、检查列数并对它们进行循环?
答案1
一个选项如何设置first 2 rows as string
?但它必须在每个设置单元格类型的东西之后使用string type
,numeric type
等等,否则它将被覆盖。优点:不需要知道有多少列。
这用于.add code
将 的原始代码包装assign cell content
到\else
的部分中\ifcase
,该代码检查行号(\pgfplotstablerow
)并将前两行的单元格内容设置为原始输入而不是格式化的数字。
\documentclass[a4paper]{article}
\usepackage{filecontents}
\begin{filecontents*}{mytable.csv}
Number,$\mathrm{f}$,$\Delta\mathrm{f}$
,\si{\kHz},\si{\kHz}
1,1000,0.1
2,3000,0.2
3,5000,0.3
\end{filecontents*}
\usepackage{pgfplotstable,booktabs,array,pgfgantt,
pgfplots,colortbl,gensymb,siunitx}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\pgfplotsset{compat=1.12}
\pgfplotstableset{
% must be used after everything setting type (string type, numeric type, etc.),
% otherwise it will be overwritten
first 2 rows as string/.style={%
assign cell content/.add code={%
% prepend to original code
\ifcase\pgfplotstablerow
% row 0: take raw contents
\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}%
\or
% row 1: take raw contents
\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}%
\else
% other rows: original code
}{%
% append to original code
\fi
}%
},
colstyle/.style 2 args={
every col no #1/.append style={#2},
},
header=false,
col sep = comma,
every head row/.style={output empty row},
every first row/.style={before row=\toprule},
every row no 1/.style ={after row=\midrule},
every even row/.style={before row=\rowcolor{lightgray}},
}
\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
fixed, fixed zerofill,precision=3,
columns/0/.append style={precision=0},
first 2 rows as string
]
{mytable.csv}
\caption{Caption of my table}
\end{table}
\end{document}