我想生成一个具有固定列宽的表格。这适用于除最后一列之外的所有列。
工作示例(除最后一列外,所有列的列宽均固定)
\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}
\begin{filecontents*}{grade.csv}
name,givennamex,matriculation,gender,grade
Maier,Hans,12348,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}
\begin{document}
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|c}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\end{document}
有问题的示例(包括最后一列在内的所有列的列宽都是固定的)
\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}
\begin{filecontents*}{grade.csv}
name,givennamex,matriculation,gender,grade
Maier,Hans,12348,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}
\begin{document}
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|>{\raggedright}m{2cm}}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\end{document}
我该怎么做才能设置最后一列的宽度?
编辑
在下面的评论之后我添加了包ragged2e
并更改了第一行
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|>{\raggedright}m{2cm}}%
到这个
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|m{2cm}}%
总的来说,现在的代码如下:
\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}
\usepackage{ragged2e}
\begin{filecontents*}{grade.csv}
name,givennamex,matriculation,gender,grade
Maier,Hans,12348,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}
\begin{document}
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|m{2cm}}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\end{document}
仍有一个问题:我无法将最后一列设置为居中(我尝试写>{\raggedright}m{2cm}
而不是m{2cm}
但是不起作用)。
答案1
总结Ulrike Fischer 的评论:有三种可能性:
- 使用
>{\raggedright\arraybackslash}
代替>{\raggedright}
。 - 定义您自己的列类型,以
\newcolumntype
避免重复输入1中提到的cde。 - 使用
\tabularnewline
而不是\\
来结束表中的一行。
\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}
\newcolumntype{R}[1]{>{\raggedright\arraybackslash}m{#1}}
\begin{filecontents*}{grade.csv}
name,givennamex,matriculation,gender,grade
Maier,Hans,12348,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}
\begin{document}
\begin{tabular}{>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\bigskip
\begin{tabular}{R{2cm}|R{2cm}|R{2cm}}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\bigskip
\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}| >{\raggedright}m{2cm}}%
\bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\tabularnewline\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}
\end{document}