我正在使用csvsimple
导入表格数据。
某些单元格信息太长,我想动态管理列宽。我尝试将tabularx
和tabulary
环境与csvsimple
部分结合起来,但没有成功。我正在使用memoir
文档类。
这是我的固定宽度代码:
\documentclass[pdftex,10pt,letterpaper, openleft, twoside]{memoir}
\usepackage{array,booktabs,csvsimple,longtable}
...
%Type of column
\newcolumntype{P}[1]{>{#1\hspace{0pt}\arraybackslash}p{75pt}}
...
\begin{document}
...
\csvreader[tabular={P{\raggedright}P{\raggedright}P{\raggedright}P{\raggedright}P{\raggedright}}, %
head to column names=true, %
table head=\hline \textbf{Short header} & \textbf{Middle header} & %
\textbf{middle header} & \textbf{long text header} & \textbf{long text header}\\\hline, %
table foot=\hline, filter equal={\csvcoli}{Something}]{mycsv.csv}%
{}%
{\csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi}\\
\end{document}
答案1
tabularx
将其集成到宏的选项列表中会很困难\csvreader
;对于环境来说,会有办法。
但你总是可以tabularx
在 周围放置一个环境\csvreader
。以下示例显示了如何执行此操作:
\begin{filecontents*}{mycsv.csv}
This,is,my,example
Red,Green,This is some very very long text,This is also some very very long text
Blue,Yellow,This is some even longer text without real content,This is also some even longer text without real content
\end{filecontents*}
\documentclass[pdftex,10pt,letterpaper, openleft, twoside]{memoir}
\usepackage{array,booktabs,csvsimple,longtable,tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{llXX}\toprule
\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D}\\\midrule
\csvreader[late after line=\\\midrule,late after last line=\\\bottomrule]
{mycsv.csv}
{}
{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{tabularx}
\end{document}