我正在使用 csvsimple 包读取 csv,并在 Latex 中进行一些最小格式化。csv 由格式化为百分比的字符串组成。
我已经创建了一些技巧来解决大多数问题,但现在我的表格旁边有一个 % 符号。这是我的代码:
\documentclass[titlepage]{report}
\usepackage{graphicx}
\usepackage{csvsimple} % Generates table from .csv
\newcommand{\csvloopx}[2][]{\csvloop{#1,#2}}
\newcommand{\csvautotabularx}[2][]{\csvloopx[#1]{autotabular={#2}}}
\newcommand{\respectpercent}{\catcode`\%=12\relax}
\begin{document}
\begin{table}[H]
\begin{center}
\begingroup\catcode`"=9
\csvreader[no head, tabular=|c|r|r|r|r|,%
before table=\respectpercent,%
table head=\hline \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares}\\\hline,%
late after line = \\\hline,%
filter expr={
test{\ifnumgreater{\thecsvinputline}{1}}}]%
{Figures/Document/Cost.csv}%
{1=\one, 2=\two, 3=\three, 4=\four, 5=\five}%
{$\one$ & $\two$ & $\three$ & $\four$ & $\five$}%
\endgroup
\end{center}
\caption{Market Cost Assumption}
\label{ tab:Slippage}
\end{table}
\end{document}
结果表为
任何帮助我都非常感谢。谢谢。
这是 csv 文件的头部:
"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
答案1
问题发生的原因在于您告诉 LaTeX 这%
不是注释字符。删除所有注释字符(代码中不需要任何注释字符)即可解决问题。
\begin{filecontents*}{\jobname.csv}
"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
\end{filecontents*}
\documentclass[titlepage]{report}
\usepackage{csvsimple} % Generates table from .csv
\newcommand{\respectpercent}{\catcode`\%=12\relax}
\newcommand{\ignoredblquote}{\catcode`\"=9\relax}
\begin{document}
\begin{table}
\centering
\csvreader[
no head,
tabular=|c|r|r|r|r|,
before table=\respectpercent\ignoredblquote,
table head={
\hline
\multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year} &
\multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base} &
\multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares} &
\multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share} &
\multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares} \\
\hline
},
late after line = \\\hline,
filter expr={
test{\ifnumgreater{\thecsvinputline}{1}}
},
]{\jobname.csv}%
{1=\one, 2=\two, 3=\three, 4=\four, 5=\five}
{$\one$ & $\two$ & $\three$ & $\four$ & $\five$}
\caption{Market Cost Assumption}
\label{tab:Slippage}
\end{table}
\end{document}
我更改了 csv 文件的名称,只是为了使示例自成一体。出于同样的原因,我还删除了未使用的包和命令。
避免该[H]
选项。我也进行了\ignoredblquote
类似定义\respectpercent
,因此不需要组。
请注意,中的空格\label
不会被忽略。
答案2
救援服务:
\begin{filecontents*}{\jobname.csv}
"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
\end{filecontents*}
\documentclass[titlepage]{report}
\usepackage{graphicx}
\usepackage{csvsimple} % Generates table from .csv
\newcommand{\csvloopx}[2][]{\csvloop{#1,#2}}
\newcommand{\csvautotabularx}[2][]{\csvloopx[#1]{autotabular={#2}}}
\newcommand{\respectpercent}{\catcode`\%=12\relax}
\usepackage{float}
\newcommand{\me}[1]{#1}
\begin{document}
\begin{table}[H]
\begin{center}
\me{\begingroup\catcode`"=9
\csvreader[no head, tabular=|c|r|r|r|r|,%
before table=\respectpercent,%
table head=\hline \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share}%
& \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares}\\\hline,%
late after line = \\\hline,%
filter expr={
test{\ifnumgreater{\thecsvinputline}{1}}}]%
{\jobname.csv}%
{1=\one, 2=\two, 3=\three, 4=\four, 5=\five}%
{$\one$ & $\two$ & $\three$ & $\four$ & $\five$}%
\endgroup}%
\end{center}
\caption{Market Cost Assumption}
\label{ tab:Slippage}
\end{table}
\end{document}
话虽如此,我只修复了这个%
问题。有关进一步需要改进的内容,请参阅@egreg 的回答。 出于同样的原因,我添加了包以使示例可编译,并从@egreg 的回答中float
复制了 用法。filecontents*
至于解释,这是@egreg给出的。%
当 csvtable 开始做表格时,设置为不是注释字符。
较早的%
在获取其参数时仍是注释字符\csvreader
,因此表现如此。使用该\me
方法,我冻结了最后一个的 catcode %
,只是为了说明。
但更简单的是使用\relax
代替有问题的%
来吞噬行尾空间(嗯...无论如何,在关闭段落时,TeX 会删除额外的空格标记,而且你的表格太宽了
Overfull \hbox (70.65941pt too wide) in paragraph at lines 40--42
)
@egreg 指出,前面的那些是不需要的。当 LaTeX 命令有 N 个以括号形式给出的参数时,中间的空格将被忽略……除非以某种特殊方式定义;此外,csvreader 在解析逗号分隔的选项时可能会忽略逗号后的空格。