我有一個像這樣的 .csv 檔案:
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
根據type
列的內容,我想區分多列和單列。
typB
在這個簡單的 MWE 中,除了提到的行之外,應該總是有單列。在這種情況下,只需將說明列印在行的中央,如圖所示。
我認為這與此類似https://tex.stackexchange.com/a/459848/104358問題,但就我而言,我不想對特定行進行硬編碼。應透過檢查類型列的內容來動態計算它們。
我試圖在 的幫助下實現這一點,\pgfplotstablegetelem
但這似乎沒有在環境中定義typeset cell/.code
。
\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1}
\pgfplotstableset{
typeset cell/.code={%
%\pgfplotstablegetelem{\pgfplotstablerow}{type}\of{\csvdata}
%\ifthenelse{\equal{\pgfplotsretval}{typB}}{
% Equivalent to: \ifnum\pgfplotstablerow = 2
%}{
% Equivalent to: \ifnum\pgfplotstablerow != 2
%}
\ifnum\pgfplotstablerow=2 %=> This row depends on the content of column type!
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
\else%
\ifnum\pgfplotstablecol=2%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{3}{c}{#1}}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
\fi
\else%
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
\fi
\fi
},
}
\begin{document}
\pgfplotstabletypeset[col sep = comma,
string type,
column type = l,
multicolumn names,
]{\csvdata}
\end{document}
答案1
我找到了解決我的問題的方法!也許它不是最好的,但它仍然有效!
訣竅是需要有一個特殊情況,因為\pgfplotstablerow=-1
這是需要的,因為從(包括頭行)\pgfplotstablerow
開始計數,但從(頭行後面)開始計數。-1
\pgfplotstablegetelem{}{}\of{\csvdata}
0
\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
Type,Description,Value
typA,descriptionA0,value0
typB,descriptionA1,value1
typA,descriptionB2,value2
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1}
\newcounter{endRowCounter}
\pgfplotstableset{
col sep=comma,
string type,
typeset cell/.code={%
\setcounter{endRowCounter}{\pgfplotstablerows}
\addtocounter{endRowCounter}{1}
\ifthenelse{\pgfplotstablerow=-1}{
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}
\fi
}{
\ifthenelse{\pgfplotstablerow<\value{endRowCounter}}{
\pgfplotstablegetelem{\pgfplotstablerow}{Type}\of{\csvdata}
\ifthenelse{\equal{\pgfplotsretval}{typB}}{
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
\else
\ifnum\pgfplotstablecol=1
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{3}{c}{#1}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
\fi
}{
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
\fi
}
}{}
}
},
}
\begin{document}
\pgfplotstabletypeset{\csvdata}
\end{document}
結果