How to write numbers in the form of using \foreach or ...?
Just \foreach
command and an \ifnum
statement
\documentclass{article}
\usepackage{tikz}
\begin{document}
\noindent\foreach \n in {1,...,7}{%
n=\n \quad \foreach \j in{\n,...,1}
{\foreach \i in {1,...,\n}{%
\ifnum\i>\j\relax\the\numexpr-\j+\i\else\the\numexpr\n-\j+\i\fi},}\\}
Writing with:
\foreach \i in {1,2, ...,7}{
\i
}
\end{document}
How about
\documentclass{article}
\usepackage{pgffor}
\def\PermuteLeft#1#2|{#2#1}
\newcounter{pft}
\newcounter{pfft}
\newcounter{pffft}
\newcommand{\LstInt}[2]{\edef#2{}%
\setcounter{pft}{0}%
\loop\stepcounter{pft}%
\edef#2{#2\number\value{pft}}%
\ifnum\value{pft}<#1\repeat}
\begin{document}
\setcounter{pfft}{1}
\loop
\stepcounter{pfft}%
\number\value{pfft}:%
\bgroup\LstInt{\number\value{pfft}}{\mylist}%
\mylist
\foreach \X in {1,...,\the\numexpr\number\value{pfft}-1}
{\xdef\mylist{\expandafter\PermuteLeft\mylist|}
\mylist}
\egroup\par
\ifnum\value{pfft}<7\repeat
\end{document}
Or completely without packages.
\documentclass{article}
\def\PermuteLeft#1#2|{#2#1}
\newcounter{pft}
\newcounter{pfft}
\newcounter{pffft}
\newcommand{\LstInt}[2]{\edef#2{}%
\setcounter{pft}{0}%
\loop\stepcounter{pft}%
\edef#2{#2\number\value{pft}}%
\ifnum\value{pft}<#1\repeat}
\newcommand{\Row}[1]{\setcounter{pffft}{0}%
\edef#1{}%
\loop\stepcounter{pffft}%
\edef\mylist{\expandafter\PermuteLeft\mylist|}%
\edef#1{#1, \mylist}%
\ifnum\value{pffft}<\value{pfft}%
\repeat}%
\newcommand{\AhmadiTable}[2][2]{%
\setcounter{pfft}{\the\numexpr#1-1}%
\loop
\stepcounter{pfft}%
\begin{minipage}[t]{2em}
\number\value{pfft}:%
\end{minipage}
\begin{minipage}[t]{\the\dimexpr\linewidth-3em}
\bgroup\LstInt{\number\value{pfft}}{\mylist}%
\mylist
\Row{\myrow}%
\myrow
\egroup\end{minipage}\par\smallskip\noindent
\ifnum\value{pfft}<#2\repeat}
\begin{document}
\subsection*{Table up to 7}
\AhmadiTable{7}
\subsection*{Table from 4 to 12}
\AhmadiTable[4]{11}
\end{document}
Better than with \foreach
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\makecyclic}{m}
{
\begin{tabular}{@{}ll@{}}
\int_step_function:nN { #1 } \__ahmadi_makecyclic:n
\vdots & \vdots \\
\end{tabular}
}
\seq_new:N \l__ahmadi_cycle_seq
\tl_new:N \l__ahmadi_cycle_tl
\cs_new_protected:Nn \__ahmadi_makecyclic:n
{
If~$n=#1$: & \__ahmadi_cycle:e { \__ahmadi_generate:n { #1 } } \\
}
\cs_new:Nn \__ahmadi_generate:n
{
\int_step_function:nN { #1 } \use:n
}
\cs_new_protected:Nn \__ahmadi_cycle:n
{
\seq_clear:N \l__ahmadi_cycle_seq
\seq_put_right:Nn \l__ahmadi_cycle_seq { #1 }
\tl_set:Nn \l__ahmadi_cycle_tl { #1 }
\prg_replicate:nn { \tl_count:N \l__ahmadi_cycle_tl - 1 }
{
\__ahmadi_swap:
}
\seq_use:Nn \l__ahmadi_cycle_seq { ,~ }
}
\cs_generate_variant:Nn \__ahmadi_cycle:n { e }
\cs_new_protected:Nn \__ahmadi_swap:
{
\tl_set:Nx \l__ahmadi_cycle_tl
{
\tl_tail:N \l__ahmadi_cycle_tl \tl_head:N \l__ahmadi_cycle_tl
}
\seq_put_right:NV \l__ahmadi_cycle_seq \l__ahmadi_cycle_tl
}
\ExplSyntaxOff
\begin{document}
\makecyclic{3}\qquad
\makecyclic{5}
\end{document}
Actually, it's longer to write the macros than to type directly the tables. However, I found it interesting how to generate the sequence containing the cyclic permutations.
If you also define
\NewDocumentCommand{\cyclic}{m}
{
\__ahmadi_cycle:n { #1 }
}
then typing \cyclic{abcdef}
will print