我试图通过使用的宏来构建一个tabular
自动生成的环境。但我似乎做不到。pgffor
\foreach
以下是生成乘法表的 MWE:
\documentclass{article}
\usepackage[margin=0.5in,showframe]{geometry}
\usepackage{pgfkeys,pgffor}
%% keys for control the dimensions of the table
\pgfkeys
{/ae/multiplication/table/.cd,
xmax/.initial = 9,
ymax/.initial = 9,
}
\def\aeget#1{\pgfkeysvalueof{/ae/multiplication/table/#1}}
%% commands to facilitate controlling when expansion occurs.
\def\aemulttable{\tabular[t]}
\def\aecolcnt{{ c | *{\number\numexpr\xmax\relax}{c}}}
\def\mytablecontents{%%'
\foreach \y in {0,1,...,\ymax} {%%'
\foreach \x in {0,1,...,\xmax} {%%'
\ifnum\y=0\relax
\ifnum\x=0\relax
$\times$ & %%'
\else
$\x$ & %%'
\fi
\else
\ifnum\x=0\relax
$\y$ & %%'
\else
$\number\numexpr\x*\y\relax$ & %%'
\fi
\fi
\ifnum\x=\xmax\relax\\\fi
}}}
\newcommand{\createtable}[1][]
{\pgfkeys{/ae/multiplication/table/.cd,#1}%%'
\def\xmax{\aeget{xmax}}%%'
\def\ymax{\aeget{ymax}}%%'
\noindent
\expandafter\expandafter\expandafter\aemulttable\expandafter\aecolcnt
\mytablecontents
\endtabular
}
\pagestyle{empty}
\begin{document}
\createtable[xmax=4,ymax=25]
\end{document}
我想Pgffor 和对齐字符 (&)会很有帮助。但提供的唯一答案不使用pgffor
。我已经编写了自己的expl3
解决方案,但我真的很想知道如何使用来实现它pgffor
。
答案1
终于明白了。在仔细阅读了 @percusse 建议的链接后,我终于找到了正确的\expandonce
和命令组合,可以在循环内\noexpand
构建环境的内容。tabular
pgffor
\foreach
以下是实际工作的 MWE:
\documentclass{article}
\usepackage{pgfkeys}
\usepackage{pgffor}
\usepackage{etoolbox}
\makeatletter
\def\ae@col@max{9}
\def\ae@row@max{9}
\pgfkeys{/ae/multiplication/table/.cd,
column max/.store in=\ae@col@max,
row max/.store in=\ae@row@max}
\def\ae@my@tabular{}
\newcommand\ae@build@table{%%
\let\ae@my@tabular\relax%%
\foreach \myrow in {0,1,...,\ae@row@max}
{%%
\foreach \mycol in {0,1,...,\ae@col@max}
{%%
%% first row handled differently
%% The first row is a multiplication symbol followed by column headers
\ifnum\myrow=0\relax
\ifx\ae@my@tabular\relax
\xdef\ae@my@tabular{\noexpand\begin{tabular}{c|*{\number\ae@col@max}{c}}}%%
\fi
\ifnum\mycol=0\relax
\xdef\ae@my@tabular{\expandonce\ae@my@tabular$\times$}%%
\else
\xdef\ae@my@tabular{\expandonce\ae@my@tabular & \mycol}
\fi
\else
%% first column handled differently
%% the first column contains a multiplcaiton symbol or row headers
\ifnum\mycol=0\relax
\ifnum\myrow=1\relax
\xdef\ae@my@tabular{\expandonce\ae@my@tabular\noexpand\\\noexpand\hline}%%
\else
\xdef\ae@my@tabular{\expandonce\ae@my@tabular\noexpand\\}%%
\fi
\xdef\ae@my@tabular{\expandonce\ae@my@tabular \myrow}%%
\else
\xdef\ae@my@tabular{\expandonce\ae@my@tabular & \number\numexpr \myrow*\mycol\relax}%%
\fi
\fi
}%%
}
\xdef\ae@my@tabular{\expandonce\ae@my@tabular\noexpand\end{tabular}}%%
}
\newcommand\aemultiplicationtable[1][]{%%
\pgfkeys{/ae/multiplication/table/.cd,#1}%%
\ae@build@table
\ae@my@tabular
}
\makeatother
\pagestyle{empty}
\begin{document}
\aemultiplicationtable[column max=10,row max=15]
\end{document}
生成结果: