我有以下有效的 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
:
如您所见,_
表格中显示不正确。现在添加行
\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}
及其结果:
如果你使用lualatex
或进行编译,则xelatex
不需要调用 package 。因此,注释掉上面 mwe 中的fontenc
行,然后使用 进行编译,你会得到类似的结果:\usepackage[T1]{fontenc}
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}