1

I am working with tabulars formatted in a consistent way. Sometimes the tables are 3X3, 3X4, 4X4 etc nXm in general. As it is I have a macro for each of these tables: \matrixIIIC, \matrixIIID etc. Is there a way to define a single macro that would cover all these cases such that as syntax it would look like: \matrix{3}{3}, \matrix{3}{4} etc?

Below is a MWE that shows what I so far have for one of the macros. Ideally, the column and row numbering would also be automatic.

\documentclass{article}

\usepackage{xparse}
\usepackage{multirow}
\usepackage{graphicx}

\DeclareDocumentCommand{\matrixIVD}{ O{RowName} O{ColumnName} }{%
\begin{tabular}{c|*{5}{c|}}
\multicolumn{2}{c}{}  &\multicolumn{4}{c}{#2} \\\cline{3-6}
\multicolumn{1}{c}{}& & a&b&c&d\\\cline{2-6}
\multirow{8}{*}{\rotatebox[origin=c]{90}{#1}} 
&1&&&&\\    
&2&&&&\\
&3&&&&\\    
&4&&&&\\\cline{2-6} 
\end{tabular}%
}%

\begin{document}

\matrixIVD

\end{document}

which produces as output

enter image description here

Oh, and the rowname needs to be automatically centered too!

  • The image is not produced by the posted code. – David Carlisle Nov 11 '14 at 19:43
  • Aha, yes, you're right. I minimalised the code further. I'll repost a new pic. – Geoff Nov 11 '14 at 19:44
  • In particular (with the original image) it wasn't clear if you wanted the content filled in automatically. I assume not now. – David Carlisle Nov 11 '14 at 19:57
  • You are right. My idea was to have macro holders (imaginatively called like \cellIA) and these I would appropriately redfine befoe the call of \matrix{}{}. – Geoff Nov 11 '14 at 20:00
  • Ok, perhaps it makes sense in context, it seems a lot more work to define 16 macros to fill in a 4x4 table than to simply place the 16 values in a tabular environment body? – David Carlisle Nov 11 '14 at 20:03
3

enter image description here

\documentclass{article}
\usepackage{xparse}
\usepackage{graphicx}

\DeclareDocumentCommand{\zmatrix}{ O{RowName} O{ColumnName} mm}{%
\rotatebox[origin=c]{90}{\hspace{-20pt}#1}%
\kern7pt
\begin{tabular}{|c|*{#3}{c|}}
\multicolumn{1}{c}{} &\multicolumn{#3}{c}{#2} \\\cline{2-\numexpr#3+1\relax}
\multicolumn{1}{c|}{} \mhead{1}{#3}\\\hline
\mrow{1}{#3}{#4}
\hline 
\end{tabular}%
}%

\makeatletter
\def\mhead#1#2{%
\ifnum#1>#2
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{&\@alph{#1}\mhead{\numexpr#1+1\relax}{#2}}}


\def\mrow#1#2#3{%
\ifnum#1>#3
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\number#1\mbody{1}{#1}{#2}\\\mrow{\numexpr#1+1\relax}{#2}{#3}}}

\def\mbody#1#2#3{%
\ifnum#1>#3
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{&\csname cellX\@alph{#1}X\@alph{#2}\endcsname\mbody{\numexpr#1+1\relax}{#2}{#3}}}

\makeatother 
\begin{document}

\def\cellXaXb{a-b}

\def\cellXcXa{one}
\def\cellXcXb{two}
\def\cellXcXc{three}

\def\cellXdXe{the end}
\zmatrix{4}{5}

\end{document}

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.