Usando booktabs com newcommand

Usando booktabs com newcommand

Sou relativamente novo na definição de comandos e ambientes. Estou tentando fazer várias tabelas (todas seguindo um modelo) e pensei que definir um novo comando para mim simplificaria o código. Aqui está o que eu tentei:

\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}
}

Basicamente, gostaria de poder inserir 6 parâmetros, preencher esses parâmetros e imprimir a tabela correspondente.

Infelizmente, esse código não funciona. Você sabe como fazer isso funcionar?

Obrigado!

Responder1

Aqui está uma macro mais flexível, que permite escolher entre usar table(quando a chave captioné usada no argumento opcional) ou apenas produzir o arquivo tabular.

A placementchave (valor padrão htp) pode ser adicionada junto com as outras opções.

\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}

insira a descrição da imagem aqui

informação relacionada