我正在使用格式一致的表格。有时表格是 3X3、3X4、4X4 等,一般是 nXm。事实上,我为每个表格都设置了一个宏:\matrixIIIC
,\matrixIIID
等等。有没有办法定义一个宏来涵盖所有这些情况,使其语法看起来像:\matrix{3}{3}
,\matrix{3}{4}
等等?
下面是一个 MWE,显示了我目前对其中一个宏所拥有的内容。理想情况下,列和行编号也将是自动的。
\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}
产生输出
哦,行名也需要自动居中!
答案1
\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}