带标题的表格(生成整个表格)的 csvreader

带标题的表格(生成整个表格)的 csvreader

我有以下有效的 MWE:

\begin{filecontents*}{\jobname.csv}
    Project, Size
    A_b,-0.1237466
    b,0.04632737
    \end{filecontents*}

    \documentclass{article}
    \usepackage{csvsimple}

    \begin{document}

    \csvreader[tabular=l|r,
    respect underscore=true,
    table head=\hline Project & Size \\\hline\hline,
    late after line=\\\hline]%
    {\jobname.csv}{}%
    {\csvcoli & \csvcolii}%

\end{document}

然而,我无法提出\caption。我试过

\begin{filecontents*}{\jobname.csv}
    Project, Size
    A_b,-0.1237466
    b,0.04632737
    \end{filecontents*}

    \documentclass{article}
    \usepackage{csvsimple}

    \begin{document}

    \csvreader[tabular=l|r,
    respect underscore=true,
    table head=\caption{Sample table} 
    \hline Project & Size \\\hline\hline,
    late after line=\\\hline]%
    {\jobname.csv}{}%
    {\csvcoli & \csvcolii}%

\end{document}

\caption{}但我遇到了各种无法轻松调试的错误。以这种方式生成的表的放在哪里?

注意:我不想csvreader在里面使用,\begin{table}...因为respect underscore=true当我这样做时似乎无法正常工作,并且我的数据中有下划线。

答案1

抱歉,但respect underscore=true在 内运行正常\begin{table} ... \end{table}

不过,还有一个使用的解决方案longtable,这里是它们两个:

\begin{filecontents*}{\jobname.csv}
  Project, Size
  A_b,-0.1237466
  b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{float}
\usepackage{caption}

\begin{document}
\listoftables

\begin{table}[htb]\centering
\caption{Sample table}\label{tab:test}
\csvreader[%
  tabular=l|r,
  respect underscore=true,
  table head=\hline Project & Size \\\hline\hline,
  late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{table}

As you can see in table~\ref{tab:test}, \texttt{respect underscore=true} works perfectly within \texttt{\textbackslash begin\{table\}\dots\textbackslash end\{table\}} 

\csvreader[
    longtable=l|r,
    respect underscore=true,
    table head=\caption{Sample table with \texttt{longtable}}\label{tab:test2}\\\hline Project & Size \\\hline\hline\endhead
        \hline\endfoot,
    late after line=\\
]{\jobname.csv}{}{\csvcoli & \csvcolii}

\end{document}

在此处输入图片描述

答案2

例如,您可以使用环境center并在其中添加\captionof(需要加载包caption)。您需要一个环境才能使用\captionof,并且在您的例子中,您可以使用环境center使表格居中...

参见以下代码

\begin{filecontents*}{\jobname.csv}
  Project, Size
  A_b,-0.1237466
  b,0.04632737
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}
\usepackage{caption} % <================================= for \captionof


\begin{document}

\listoftables

\begin{center} % <======================================================
\captionof{table}{Test}\label{tab:test} % <=============================
\csvreader[%
  tabular=l|r,
  respect underscore=true,
  table head=\hline Project & Size \\\hline\hline,
  late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{center} % <========================================================

As you can see in table~\ref{tab:test} \dots

\end{document}

使用以下命令进行编译,结果如下pdflatex

生成的 pdf

如您所见,_表格中显示不正确。现在添加行

\usepackage[T1]{fontenc} % <======================== needed for pdflatex

回到序言,您会_在表格中获得很好的打印。您需要[T1]{fontenc}使用 正确打印文档pdflatex。然后,您还会获得_使用 环境 打印的文档table

请看下面的代码

\begin{filecontents*}{\jobname.csv}
  Project, Size
  A_b,-0.1237466
  b,0.04632737
\end{filecontents*}


\documentclass{article}

\usepackage[T1]{fontenc} % <======================== needed for pdflatex
\usepackage{csvsimple}
\usepackage{caption} % <================================= for \captionof


\begin{document}

\listoftables

\begin{center} % <======================================================
\captionof{table}{Test}\label{tab:test} % <=============================
\csvreader[%
  tabular=l|r,
  respect underscore=true,
  table head=\hline Project & Size \\\hline\hline,
  late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{center} % <========================================================

As you can see in table~\ref{tab:test} \dots

\begin{table}[hb] % <===================================================
\caption{Test1}\label{tab:test1} % <====================================
\centering % <==========================================================
\csvreader[%
  tabular=l|r,
  respect underscore=true,
  table head=\hline Project & Size \\\hline\hline,
  late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{table} % <========================================================

As you can see in table~\ref{tab:test1} \dots

\end{document}

及其结果:

使用 fontenc 和 pdflatex 的结果

如果你使用lualatex或进行编译,则xelatex不需要调用 package 。因此,注释掉上面 mwe 中的fontenc行,然后使用 进行编译,你会得到类似的结果:\usepackage[T1]{fontenc}lualatex

使用 lualatex 的结果

答案3

\captionof使用包中的解决方法caption

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}
\usepackage{caption}

\begin{document}

\begin{minipage}{\textwidth}
\captionof{table}{caption text}
\csvreader[tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{minipage}
\end{document}

相关内容