我对定义命令和环境还比较陌生。我尝试制作几个表(所有表都遵循模板),并认为定义一个新命令可以简化代码。以下是我尝试的方法:
\newcommand{test}[6]{
\begin{table}
\begin{tabular}{ll}
\centering
\toprule
\textbf{Form} & \textbf{Conjugation} \\
\midrule
\textit{Yo} & \textit{#1} \\
\textit{T\'u} & \textit{#2} \\
\textit{\'El} & \textit{#3} \\
\textit{Nosot@s} & \textit{#4} \\
\textit{Vosotr@s} & \textit{#5} \\
\textit{Ell@s} & \textit{#6} \\
\bottomrule
\end{tabular}
\end{table}
}
基本上,我希望能够输入 6 个参数,并让它填写这些参数并打印相应的表格。
不幸的是,这段代码不起作用。你知道该如何让它工作吗?
谢谢你!
答案1
这是一个更灵活的宏,允许选择使用(当在可选参数中使用table
键时)或仅生成。caption
tabular
该placement
键(默认值htp
)可以与其他选项一起添加。
\documentclass{article}
\usepackage{booktabs}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\conjugation}{O{}m}
{
\group_begin:
\keys_set:nn { froggos/conjugation } { #1 }
\froggos_conjugation:n { #2 }
\group_end:
}
\keys_define:nn { froggos/conjugation }
{
placement .tl_set:N = \l__froggos_conjugation_placement_tl,
placement .initial:n = htp,
caption .tl_set:N = \l__froggos_conjugation_caption_tl,
shortcaption .tl_set:N = \l__froggos_conjugation_shortcaption_tl,
label .tl_set:N = \l__froggos_conjugation_label_tl,
}
\seq_new:N \l__froggos_conjugation_entries_seq
\cs_new_protected:Nn \froggos_conjugation:n
{
\tl_if_empty:NF \l__froggos_conjugation_caption_tl
{
\__froggos_conjugation_table_begin:V \l__froggos_conjugation_placement_tl
\centering
}
\seq_set_split:Nnn \l__froggos_conjugation_entries_seq { \\ } { #1 }
\begin{tabular}{ll}
\toprule
\textbf{Form} & \textbf{Conjugation} \\
\midrule
\textit{Yo} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {1}} \\
\textit{T\'u} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {2}} \\
\textit{\'El} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {3}} \\
\textit{Nosotr@s} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {4}} \\
\textit{Vosotr@s} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {5}} \\
\textit{Ell@s} & \textit{\seq_item:Nn \l__froggos_conjugation_entries_seq {6}} \\
\bottomrule
\end{tabular}
\tl_if_empty:NF \l__froggos_conjugation_caption_tl
{
\tl_if_empty:NTF \l__froggos_conjugation_shortcaption_tl
{
\caption{\l__froggos_conjugation_caption_tl}
}
{
\caption[\l__froggos_conjugation_shortcaption_tl]{\l__froggos_conjugation_caption_tl}
}
\tl_if_empty:NF \l__froggos_conjugation_label_tl
{
\label{\l__froggos_conjugation_label_tl}
}
\end{table}
}
}
\cs_new_protected:Nn \__froggos_conjugation_table_begin:n
{
\begin{table}[#1]
}
\cs_generate_variant:Nn \__froggos_conjugation_table_begin:n {V}
\ExplSyntaxOff
\begin{document}
\listoftables
\section{Main}
\conjugation{soy \\ eres \\ es \\ somos \\ sois \\ son}
\conjugation[
caption=Conjugation of \textit{ser},
label=verb:ser,
]{soy \\ eres \\ es \\ somos \\ sois \\ son}
\conjugation[
caption={Conjugation of \textit{ser}, but with a very long caption that requires a short one},
shortcaption=Conjugation of \textit{ser},
label=verb:ser-again,
]{soy \\ eres \\ es \\ somos \\ sois \\ son}
\end{document}