我创建了一个动态表,以便将内联 CSV 数据轻松地转换为表,并希望现在将其用作自定义命令,以便我的同事可以轻松使用表命令。
第一个表将生成我想要的表:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}\pgfplotsset{compat=newest}
\usepackage{float}
\begin{document}
\begin{table}[H]
\begin{center}
\pgfplotstabletypeset[
col sep = comma,
string type,
string replace*={_}{\_},
every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline},
every last row/.style={after row=\hline},
display columns/0/.style={string type,column type = {|l}},
every last column/.style={string type,column type = {l|}},
every head row/.append style={
before row={\rowcolor{blue}},
typeset cell/.code={
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}%
\fi
}
}
]{
a,b,c,d
aa,bb,cc,dd
}
\end{center}
\caption{yay}
\label{yayy}
\end{table}
\end{document}
这确实有效,我没有遇到任何问题。但它太大了,我想用以下命令将其包装起来:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}\pgfplotsset{compat=newest}
\usepackage{float}
\newcommand{\csvtable}[2]{%
\begin{table}[H]
\begin{center}
\pgfplotstabletypeset[
col sep = comma,
string type,
string replace*={_}{\_},
every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline},
every last row/.style={after row=\hline},
display columns/0/.style={string type,column type = {|l}},
every last column/.style={string type,column type = {l|}},
every head row/.append style={
before row={\rowcolor{blue}},
typeset cell/.code={
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}%
\fi
}
}
]{%
#2
}%
\end{center}
\caption{#1}
\label{#1}
\end{table}
}
\begin{document}
\csvtable{my caption}{
this, is, csv, data
a, b, c, d
}
\end{document}
现在我收到此错误消息,它对我没有帮助,因为我尝试了所有这些分隔符命令:
results.tex, line 69
Package pgfplots Error: Could not read table file '" this, is, csv, data a, b, c, d "' in 'search path=.'. In case you intended to provide inline data: maybe TeX screwed up your end-of-lines? Try `row sep=crcr' and terminate your lines with `\\' (refer to the pgfplotstable manual for details).
长话短说。我如何才能正确嵌入 csv 数据?我知道还有一种方法,\begin{custom} \end{custom}
但我从未使用过它,到目前为止,newcommand 对我来说工作得很好。
感谢帮助!
编辑:根据 Rmanos 建议更新了示例。
答案1
好吧 --- 错误告诉你使用row sep=crcr
and\\
作为分隔符。传递参数时,新行和空格对于 LaTeX 是相同的,因此你“失去”了行的分隔。它类似于逐字环境的嵌套。可能有更聪明的技巧,但这个有效:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}\pgfplotsset{compat=newest}
\usepackage{float}
\newcommand{\csvtable}[2]{% <- you add an extra space without this
\begin{table}[H]
\begin{center}
\pgfplotstabletypeset[
col sep = comma,
string type,
row sep=crcr,
string replace*={_}{\_},
every head row/.style={before row=\rowcolor{blue}\hline,after row=\hline},
every last row/.style={after row=\hline},
display columns/0/.style={string type,column type = {|l}},
every last column/.style={string type,column type = {l|}},
every head row/.append style={
before row={\rowcolor{blue}},
typeset cell/.code={
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}%
\fi
}
}
]{%
#2
}%
\end{center}
\caption{#1}
\label{#1}
\end{table}%
}
\begin{document}
\csvtable{my caption}{
this, is, csv, data \\
a, b, c, d \\
}
\end{document}
下次,请发布一个完整的示例,以便人们在尝试提供帮助时不必追逐包和选项......