我有许多数据表,其中穿插着文本,我希望这些文本的格式相同。现在,我正在手动设置表格中每个表格的宽度:
\documentclass{article}
\usepackage[no-math]{fontspec}
\setmainfont{NotoSerif}
\usepackage[margin=1in]{geometry}
\usepackage{setspace}
\usepackage{lipsum}
\usepackage{gb4e,cgloss}
\begin{document}
\begin{tabular}{p{3cm} >{\em} p{3.5cm} @{\hskip 1in} p{3cm} >{\em} p{3.5cm}}
word1 & description & word1a & description \\
word2 & description & word2a & description \\
\end{tabular}
\end{document}
我不喜欢为每个表手动设置宽度,尤其是因为这会使更改宽度变得很麻烦。如何在序言中定义具有特定列宽的自定义表格环境?
实际上,可以更简单地列出数据的解决方案(例如使用空格或括号)也很好,如下所示:
\begin{customtable}
word1 description word1a description \\
word2 description word2a description \\
\end{customtable}
答案1
customtable
只需定义一个包含以下内容的环境tabular
:
\documentclass[]{article}
\usepackage{array}
\newenvironment{customtable}
{%
\begin{tabular}
{p{3cm} >{\itshape} p{3.5cm} @{\hskip 1in} p{3cm} >{\itshape} p{3.5cm}}%
}
{%
\end{tabular}%
}
\begin{document}
\begin{customtable}
word1 & description & word3 & description \\
word2 & description & word4 & description \\
\end{customtable}
\end{document}
可以使用以下示例引入空格分隔语法(原始实现,毫无疑问可以实现更好的语法):
\documentclass[]{article}
\usepackage{array}
\usepackage{environ}
\makeatletter
\NewEnviron{CustomTable}
{%
\expandafter\parseRows\BODY\\\endparseRows
\begin{tabular*}{\linewidth}
{
>{\raggedright\arraybackslash}p{4em}
>{\raggedright\arraybackslash\itshape} p{8em}
@{\extracolsep{\fill}}
>{\raggedright\arraybackslash}p{4em}
>{\raggedright\arraybackslash\itshape} p{8em}
}%
\CustomTableBody
\end{tabular*}%
}
\newcommand\parseRows{}
\newcommand\CustomTableBody{}
\newcommand\parseRow{}
\def\parseRows#1\\#2\endparseRows
{%
\if\relax\detokenize{#1}\relax
\expandafter\@gobble
\else
\parseRow#1 \endparseRow
\edef\CustomTableBody{\unexpanded\expandafter{\CustomTableBody\\}}%
\expandafter\@firstofone
\fi
{%
\expandafter\parseRows\@firstofone#2\\\endparseRows
}%
}
\def\parseRow #1 #2 #3 #4\endparseRow
{
\edef\CustomTableBody
{\unexpanded\expandafter{\CustomTableBody#1}}%
}
\begin{document}
\noindent
\begin{CustomTable}
word1 description word3 description \\
word2 description word4 description \\
word3 {good multi word description} word4 description \\
word3 {good multi word description} \\
\end{CustomTable}
\end{document}
语法可能不太可靠,使用时请自负风险。
答案2
您可能会喜欢这种允许按顺序插入项目的实现。
默认情况下tabular
使用,但有一个可选参数用于指定longtable
。
\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xparse}
\usepackage{longtable,array}
\ExplSyntaxOn
\NewDocumentCommand{\customtable}{O{tabular}m}
{
\makenna_customtable:nn { #1 } { #2 }
}
\seq_new:N \l_makenna_customtable_rows_seq
\seq_new:N \l__makenna_customtable_item_seq
\tl_new:N \l_makenna_customtable_body_tl
\int_new:N \l_makenna_customtable_length_int
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\cs_new_protected:Nn \makenna_customtable:nn
{
\seq_set_split:Nnn \l_makenna_customtable_rows_seq { \\ } { #2 }
\int_set:Nn \l_makenna_customtable_length_int
{
\int_div_round:nn { \seq_count:N \l_makenna_customtable_rows_seq } { 2 }
}
\tl_clear:N \l_makenna_customtable_body_tl
\int_step_inline:nnnn
{ 1 } % start
{ 1 } % step
{ \l_makenna_customtable_length_int } % end
{
\seq_set_split:Nnx \l__makenna_customtable_item_seq { ~ }
{ \seq_item:Nn \l_makenna_customtable_rows_seq { ##1 } }
\tl_put_right:Nx \l_makenna_customtable_body_tl
{
\seq_use:Nn \l__makenna_customtable_item_seq { & } &
}
\seq_set_split:Nnx \l__makenna_customtable_item_seq { ~ }
{ \seq_item:Nn \l_makenna_customtable_rows_seq { ##1 + \l_makenna_customtable_length_int } }
\tl_put_right:Nx \l_makenna_customtable_body_tl
{
\seq_use:Nn \l__makenna_customtable_item_seq { & } \exp_not:N \\
}
}
\begin{#1}{p{3cm} >{\em} p{3.5cm} @{\hspace{2.5cm}} p{3cm} >{\em} p{3.5cm}}
\l_makenna_customtable_body_tl
\end{#1}
}
\ExplSyntaxOff
\begin{document}
\section{Six items}
\customtable{
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
}
\section{Nine items}
\customtable{
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word7 description \\
word8 description \\
word9 description \\
}
\section{Many items}
\customtable[longtable]{
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
word6 {three word description} \\
word1 description \\
word2 description \\
word3 description \\
word4 description \\
word5 description \\
}
\end{document}