下面是我的代码
普通表:
\documentclass{article}
\begin{document}
\begin{table}
\caption{Sample Table.\label{tab:active_distance}}
\begin{center}
\begin{tabular}{lcccc}
\hline
H1 & H2 & H3& H4 & H5\\
\hline
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}
我需要使用我的自定义标签(如下面提到的标签)来实现转换和重新使用的目的,自定义表:
\documentclass{article}
\begin{document}
\begin{table}
\caption{Sample Table.\label{tab:active_distance}}
\begin{center}
\begin{tabular}{lcccc}
\hline
\thead{
\tr{\td{H1}\td{H2}\td{H3}\td{H4}\td{H5}}}
\hline
\tbody{
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}}
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}}
}
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}
是否可以定义以下内容来创建表格,请指导
\thead{} - Table Head
\tbody{} - Table Body
\tr{} - Table row
\td{} - Table Cell
这可能吗?提前致谢,
答案1
实现如下expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{itabular}{O{c}m}% 'implicit' tabular
{% 1. change the meaning of \thead, \tbody, \tr and \td
% actually \thead and \tbody are the same, while \tr and \td are just markers
\cs_set_eq:NN \thead \itabular_tbody:n
\cs_set_eq:NN \tbody \itabular_tbody:n
% 2. make the tabular
\begin{tabular}[#1]{#2}
}
{
\end{tabular}
}
\tl_new:N \l_itabular_body_tl
\seq_new:N \l_itabular_rows_seq
\seq_new:N \l_itabular_one_row_seq
\cs_new_protected:Nn \itabular_tbody:n
{
\tl_clear:N \l_itabular_body_tl
% divide at \tr, the first item is empty and discarded
\seq_set_split:Nnn \l_itabular_rows_seq { \tr } { #1 }
\seq_pop_left:NN \l_itabular_rows_seq \l_tmpa_tl
\seq_map_inline:Nn \l_itabular_rows_seq
{
% divide at \td, the first item is empty and discarded
\seq_set_split:Nnn \l_itabular_one_row_seq { \td } { ##1 }
\seq_pop_left:NN \l_itabular_one_row_seq \l_tmpa_tl
% fill in the body, items are separated by &, add \\ at the end
\tl_put_right:Nx \l_itabular_body_tl
{
\seq_use:Nn \l_itabular_one_row_seq { & }
\exp_not:N \\
}
}
% deliver the body
\tl_use:N \l_itabular_body_tl
}
\ExplSyntaxOff
\begin{document}
\begin{tabular}{lcccc}
\hline
H1 & H2 & H3& H4 & H5\\
\hline
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
\hline
\end{tabular}
\bigskip
\begin{itabular}{lcccc}
\hline
\thead{
\tr{\td{H1}\td{H2}\td{H3}\td{H4}\td{H5}}
}
\hline
\tbody{
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}
}
\hline
\end{itabular}
\end{document}