# Define an environment that automatically produces some of a table

Is there a way to define an environment which will automatically produce the first row and first column of a table, leaving you to fill in the rest of the entries as usual?

I'll show you what I mean.

I still occasionally work with Sanskrit, and anybody who has worked with fusional languages will be familiar with the idea of a paradigm. Each noun can occur in a variety of different cases and numbers, with a specific form for each one, showing the role that the word plays in a sentence. So for example, agni- means "fire". If I want to say "in the fire," I will use agnau, and if I want to say "from the fires", I will use agnibhyaḥ.

All possible forms of a noun can be shown in a table. This is the paradigm. Thus:

\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}
\usepackage{array}

\begin{document}

\begin{tabular}{>{\scshape}l|*{3}{>{\itshape}c}}

& \normalfont{\textsc{sg}} & \normalfont{\textsc{du}} &
\normalfont{\textsc{pl}} \\ \hline

nom & agni\d{h} & agn\={\i} & agnaya\d{h} \\

acc & agnim & agn\={\i} & agn\={\i}n \\

ins & agnin\={a} & agnibhy\={a}m & agnibhi\d{h} \\

dat & agnaye & agnibhy\={a}m & agnibhya\d{h} \\

abl & agne\d{h} & agnibhy\={a}m & agnibhya\d{h} \\

gen & agne\d{h} & agnyo\d{h} & agn\={\i}n\={a}m \\

loc & agnau & agnyo\d{h} & agni\d{s}u \\

voc & agne & agn\={\i} & agnaya\d{h} \\

\end{tabular}

\end{document}


Now every noun has a paradigm (although as an aside, many of Sanskrit's theoretical forms are not attested in actual usage). You can imagine that the skeleton, the first row and the first column, is unchanging, but it's the contents of the table that change each time.

What I would like is an environment that would define the outside of the table, and leave me to input the contents, like this:

\begin{paradigm}
k\={u}pa\d{h} & k\={u}pau & k\={u}p\={a}\d{h} \\

k\={u}pam & k\={u}pau & k\={u}p\={a}n \\

k\={u}pena & k\={u}p\={a}bhy\={a}m & k\={u}pai\d{h} \\

k\={u}p\={a}ya & k\={u}p\={a}bhy\={a}m & k\={u}pebhya\d{h} \\

k\={u}p\={a}t & k\={u}p\={a}bhy\={a}m & k\={u}pebhya\d{h} \\

k\={u}pasya & k\={u}payo\d{h} & k\={u}p\={a}n\={a}m \\

k\={u}pe & k\={u}payo\d{h} & k\={u}pe\d{s}u \\

k\={u}pa & k\={u}pau & k\={u}p\={a}\d{h} \\
\end{paradigm}


Which would produce:

I simply don't know how to produce this without defining a 24-argument command (is that even possible), which would require braces around each argument and be a lot more trouble than it's worth. But I present this as what I hope might be an interesting challenge for the wizards.

## 1 Answer

Here is an elementary implementation of paradigm that sets up the tabular as well as the first column, based on a redefinition of \\. Note that you should avoid using a terminal \\ before \end{tabular} (or \end{paradigm}):

\documentclass{article}

\usepackage{array}

\newcounter{paradigmlinecnt}
\makeatletter
\newcommand{\paradigmnewline}{%
\paradigmnewline@% Regular tabular newline
\stepcounter{paradigmlinecnt}%
\ifcase\value{paradigmlinecnt} %
\or %nom % 1 (nom already set as part of \begin{paradigm}
\or acc % 2
\or ins % 3
\or dat % 4
\or abl % 5
\or gen % 6
\or loc % 7
\or voc % 8
\else ???
\fi
&
}

\newenvironment{paradigm}
{\global\let\savenewline\\% Store meaning of \\ outside of tabular
\setcounter{paradigmlinecnt}{1}%
\begin{tabular}{ >{\scshape}l | *{3}{>{\itshape}c} }% Default paradigm tabular specification
& \normalfont{\textsc{sg}} & \normalfont{\textsc{du}} &
\normalfont{\textsc{pl}} \\ \hline % Header rule
nom & % First row start
\global\let\paradigmnewline@\\ % Redefine the way \\ works
\global\let\\\paradigmnewline
}
{\end{tabular}
\global\let\\\savenewline}% Restore meaning of \\ outside tabular
\makeatother

\begin{document}

\begin{tabular}{ >{\scshape}l | *{3}{>{\itshape}c} }
& \normalfont{\textsc{sg}} & \normalfont{\textsc{du}} &
\normalfont{\textsc{pl}} \\ \hline
nom & agni\d{h} & agn\={\i} & agnaya\d{h} \\
acc & agnim & agn\={\i} & agn\={\i}n \\
ins & agnin\={a} & agnibhy\={a}m & agnibhi\d{h} \\
dat & agnaye & agnibhy\={a}m & agnibhya\d{h} \\
abl & agne\d{h} & agnibhy\={a}m & agnibhya\d{h} \\
gen & agne\d{h} & agnyo\d{h} & agn\={\i}n\={a}m \\
loc & agnau & agnyo\d{h} & agni\d{s}u \\
voc & agne & agn\={\i} & agnaya\d{h}
\end{tabular}

\begin{paradigm}
k\={u}pa\d{h} & k\={u}pau & k\={u}p\={a}\d{h} \\
k\={u}pam & k\={u}pau & k\={u}p\={a}n \\
k\={u}pena & k\={u}p\={a}bhy\={a}m & k\={u}pai\d{h} \\
k\={u}p\={a}ya & k\={u}p\={a}bhy\={a}m & k\={u}pebhya\d{h} \\
k\={u}p\={a}t & k\={u}p\={a}bhy\={a}m & k\={u}pebhya\d{h} \\
k\={u}pasya & k\={u}payo\d{h} & k\={u}p\={a}n\={a}m \\
k\={u}pe & k\={u}payo\d{h} & k\={u}pe\d{s}u \\
k\={u}pa & k\={u}pau & k\={u}p\={a}\d{h}
\end{paradigm}

\end{document}

• You know that's mighty clever, I hope this garners the upvotes it deserves! – Au101 Jul 19 '16 at 5:40
• @Au101: Let the rumours spread far and wide! :) – Werner Jul 19 '16 at 5:50