1

I'm writing a class for a document that formats a lot of simple paragraphs as tables. I know, that all tables follow a fixed number of columns, but have a variable number of rows. What I would like to do is writing a macro, that if issued once creates a single-row table and if issued n-times creates an n-row table. I'm interested in a general solution, that allows to insert any piece of code before the first instance of a macro and after the last but not in-between repeated calls to the same macro. Below is a minimal example of what output I want to achieve:

\documentclass[a4paper]{scrarticle}

\usepackage{tabularx}

\begin{document}

One row example: 

\begin{tabular}{|c|c|}
\hline
Some & Text\\
\hline
\end{tabular}

Two row example:

\begin{tabular}{|c|c|}
\hline
Some & Text\\
\hline
Other & Line\\
\hline
\end{tabular}


\end{document}

I want to have a command that allows me to just write the table rows and everything else is added automatically. So with a hypothetic command autotable the above example would look like:

[...]
\begin{document}
One row example: 

\autotable{Some}{Text}

Two row example:

\autotable{Some}{Text}
\autotable{Other}{Line}

\end{document}

A concise answer explaining why that is not possible (if so) would also be quite helpful as I could achieve something like this using python/jinja instead if needed. Not completely unrelated questions: Look-ahead on token stream?, Macro: Expand differently depending on pattern?

Edit

This is the code taken from the accepted solution by David with added \hlines so the output created matches the exact output from the initial minimal example:

\documentclass[a4paper]{scrarticle}

\makeatletter

\def\autotable{\par\begin{tabular}{|c|c|}
\hline
\autotable@row}

\def\autotable@row#1#2{%
#1&#2
\@ifnextchar\autotable\autotable@gobble{\\\hline\end{tabular}\par}}

\def\autotable@gobble#1{\\\hline\autotable@row}

\makeatother
\begin{document}

One row example: 

\autotable{Some}{Text}

Two row example:

\autotable{Some}{Text}
\autotable{Other}{Line}


\end{document}

1 Answer 1

2

You can use \@ifnextchar to lookahead, skipping over the white space from end of line

\documentclass[a4paper]{scrarticle}

\makeatletter

\def\autotable{\par\begin{tabular}{|c|c|}\autotable@row}

\def\autotable@row#1#2{%
#1&#2%
\@ifnextchar\autotable\autotable@gobble{\end{tabular}\par}}

\def\autotable@gobble#1{\\\autotable@row}

\makeatother
\begin{document}

One row example: 

\autotable{Some}{Text}

Two row example:

\autotable{Some}{Text}
\autotable{Other}{Line}


\end{document}

enter image description here

6
  • Nice, thank you David. Any way to get the \hline working as well?
    – Dschoni
    May 3, 2021 at 9:35
  • 1
    @Dschoni you mean to place it where you explicitly not in-between repeated calls to the same macro. said nothing would be placed? :-) If you always want \hline just add it after the \\ in autotable@gobbe, if only sometimes then it depends on the syntax perhaps easiest to make it an optional argument so \autotable[\hline]{this}{that} although an option which only allows \hline is a bit odd May 3, 2021 at 10:31
  • 1
    @Dschoni or you could decide the fact that \autotable{\hline this}{that} works and call it a feature. May 3, 2021 at 10:54
  • Thanks David, I've figured out where to place my hlines and will update my question with it! A lot learned by this...
    – Dschoni
    May 3, 2021 at 11:05
  • David, could you please give me a hint where to find more info on this "aliased" function names using the @ syntax? I seem to be to dumb to google that...
    – Dschoni
    May 10, 2021 at 7:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .