在表格中顯示 csv 檔案時出現規則問題

在表格中顯示 csv 檔案時出現規則問題

我正在嘗試在表格內顯示 csv 檔案中的值。為此,我使用巨集\csvreader。我希望表格喜歡這樣:

期望的結果

其中 a 為toprule列頭, amidrule為不含行的值,以及 a bottomrule

這是我製作的程式碼

\begin{tabular}{*{4}{c}}%
    \toprule
    \textbf{Mesh} & $h$ & \textbf{nDof $\P_1$} & \textbf{nDof $\P_2$}\\
    \midrule
    \csvreader[head to column names]{fig/meshInformation3D.csv}{}
    {\texttt{\mesh} & \pgfmathprintnumber{\hAvg} & \pgfmathprintnumber{\nDofPUn} & \pgfmathprintnumber{\nDofPDeux}\\}
    \\\bottomrule
\end{tabular}

對於每一行,我顯示 csv 行的信息,然後插入一個\\.透過這段程式碼,我得到以下結果

結果

還有一個額外的空白行。我嘗試刪除\\前面的\bottomrule,但結果出現錯誤Misclaced \noalign. \bottomrule->\noalign

我怎麼刪除這條多餘的線?

答案1

使用該選項late after line=\\可以解決這個問題:

\documentclass{article}
\usepackage{pgf}
\usepackage{amssymb}
\usepackage{csvsimple}
\usepackage{booktabs}

\begin{filecontents}[noheader]{mylist.csv}
mesh, hAvg, nDofPUn, nDofPDeux 
M0  , 0.86, 47384  , 327000    
M1  , 0.74, 68993  , 473000    
\end{filecontents}

\begin{document}
\begin{tabular}{*{4}{c}}%
    \toprule
    \textbf{Mesh} & $h$ & \textbf{nDof $\mathbb{P}_1$} & \textbf{nDof $\mathbb{P}_2$} \\
    \midrule
    \csvreader[head to column names, late after line=\\]{mylist.csv}{}{
        \texttt{\mesh} & 
        \pgfmathprintnumber{\hAvg} & 
        \pgfmathprintnumber{\nDofPUn} & 
        \pgfmathprintnumber{\nDofPDeux}
    }
    \bottomrule
\end{tabular}
\end{document}

在此輸入影像描述

相關內容