我對定義指令和環境還比較陌生。我正在嘗試製作幾個表格(所有表格都遵循模板),並認為為自己定義一個新命令會簡化程式碼。這是我嘗試過的:
\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}