1

I'm relatively new to defining commands and environments. I'm trying to make several tables (all of which follow a template), and thought that defining myself a new command would simplify the code. Here's what I tried:

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

Basically, I'd like to be able to enter 6 parameters, and have it fill in those parameters and print the corresponding table.

Unfortunately, this code doesn't work. Do you know how to get it to work?

Thank you!

  • 3
    And what is your question? – Ulrike Fischer Aug 8 '19 at 6:11
  • 3
    Move \centering from inside tabular to before tabular. If you like to have centered cells' contents, than replace l columns type with c. – Zarko Aug 8 '19 at 8:00
  • 3
    Trust me: you want neither table nor centering in your code. – egreg Aug 8 '19 at 9:41
  • 2
    Do not write T\'u or \'El. This is the way of type Spanish in prehistoric ages, that is no longer necessary. In a file with UTF8 (the usual today) you can type just and Él (but note that the correct for a verb conjugation is "Tu" and "El", without accents). By the way, note also that the RAE do not accept the inclusive language usig the @. – Fran Aug 8 '19 at 11:10
  • @Fran I'm using LInux, and I don't think my keyboard supports accent marks (I don't think there's an easy way to support accent marks) – Froggos Aug 8 '19 at 14:24
4

Here's a more flexible macro, that allows to choose between using table (when the key caption is used in the optional argument) or just producing the tabular.

The placement key (default value htp) can be added along with the other options.

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

enter image description here

  • Thank you! One more question: I tried to adjust the table positioning with the [H] parameter, right after the \begin{table}. The table placement was fixed, but the letters [htp] are printed to the left of the table. Do you know why that is/ how to fix the issue? – Froggos Aug 8 '19 at 19:39
  • 1
    @Froggos Avoid the H specifier. That's all. Anyway, if you insist in bad typography, use the option placement=H, – egreg Aug 8 '19 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.