我正在尝试编写一个可以生成包含员工姓名和薪水的表格的类。我需要该类的用户能够在 tex 文件中写入员工姓名和薪水。最小示例如下:
\ProvidesClass{Employee}[2016/07/11 version 1.00 Employee's Salaries]
\NeedsTeXFormat{LaTeX2e}
\LoadClass{article}
%%%
\RequirePackage{tabu,url}
\newenvironment{salary}{
\begin{center}
\begin{tabular}{|c|c|}
\hline
Name & Salary \\ \hline
John & 40,000\$\\ \hline
%%% Here I do not want a pre--defined data
\end{tabular}
\end{center}
}{%}
}
我的 tex 文件如下所示
\documentclass[9pt,a4paper,fullpage]{SXClass}
\begin{document}
\begin{salary}
%%%% I would like to add the names here as the following:
%%%% \employee{empName}{empSalary}
%%%% \employee{Joseph}{40,000}
%%%% \employee{Bob}{70,000}
%%%% and they will appear directly into the table.
\end{salary}
\end{document}
答案1
我建议使用longtable
默认居中并且能够分页的。
\ProvidesClass{Employee}[2016/07/11 version 1.00 Employee's Salaries]
\NeedsTeXFormat{LaTeX2e}
\LoadClass{article}
%%%
\RequirePackage{tabu}
\RequirePackage{longtable}
\RequirePackage{url} % Why?
\newcommand{\employee}[2]{%
#1 & #2\tabularnewline
\hline
}
\newenvironment{salary}{%
\begin{longtable}{|p{6cm}|>{\raggedleft\arraybackslash}p{5cm}|}
\hline
\textbf{Name} & \textbf{Salary} \tabularnewline
\hline
\endhead
}{%
\end{longtable}
}
\endinput
这是司机
\documentclass[9pt,a4paper,fullpage]{Employee}
\begin{document}
\begin{salary}
\employee{Wonder Woman}{500,000}
\employee{Joseph}{40,000}
\employee{Bob}{70,000}
\employee{John}{10,000}
\employee{Paul}{210,000}
\end{salary}
\end{document}
更新siunitx
具有列格式的版本:
\ProvidesClass{Employee}[2016/07/11 version 1.00 Employee's Salaries]
\NeedsTeXFormat{LaTeX2e}
\LoadClass{article}
%%%
%\RequirePackage{tabu}
\RequirePackage{longtable}
\RequirePackage{siunitx}
\RequirePackage{url} % Why?
\newcommand{\employee}[2]{%
#1 & #2\tabularnewline
\hline
}
\newenvironment{salary}{%
\begin{longtable}{|p{6cm}|S[table-alignment=right]|}
\hline
\textbf{Name} & \multicolumn{1}{c|}{\textbf{Salary}} \tabularnewline
\hline
\endhead
}{%
\end{longtable}
}
\endinput
修改后的驱动程序:
\documentclass[9pt,a4paper,fullpage]{Employee}
\begin{document}
\sisetup{copy-decimal-marker=true}
\begin{salary}
\employee{Wonder Woman}{500,000}
\employee{Joseph}{40,000}
\employee{Bob}{70,000}
\employee{John}{10,000}
\employee{Paul}{210,000}
\end{salary}
\end{document}