7

My goal is to produce numbers in the form below.

enter image description here

Minimal code:

\documentclass{article}
\usepackage{tikz} 

\begin{document}

\begin{tabular}{l@{\hspace*{1cm}}l}
if n=1 & 1\\
if n=2 & 12, 21\\
if n=3 & 123, 231, 312\\
if n=4 & 1234, 2341, 3412, 4123\\
if n=5 & 12345, 23451, 34512, 45123, 51234\\
$\vdots$ &  $\vdots$
\end{tabular}

Writing with:

\foreach \i in {1,2, ...,5}{
\i
}

\end{document}

Thanks for any help

  • 2
    Since the code you have produces the output you want, could you explain what the question is? – cfr Nov 8 at 15:39
  • At the output I wrote the numbers. Suppose n = 7 with the loop I want to output. For example with \foreach – M.Ahmadi Nov 8 at 15:55
11

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}

enter image description here

7

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}

enter image description here

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}

enter image description here

6

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}

enter image description here

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

enter image description here

1

A LuaLaTeX-based solution. It defines a LaTeX macro called \DoArray, which takes a single argument, an integer n. (If you're dealing with base-10 numbers, n should be no greater than 9, right?) \DoArray calls a Lua function called do_array, which does virtually all of the work.

The tabular environment is set up to perform automatic line wrapping and hanging indentation in case the n strings don't all fit in a single row.

enter image description here

\documentclass{article} 
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
function do_array ( n )
for i=1,n do
  k = ""
  for j=1,i do k=k..j end
  tex.sprint ( k )
  if i>1 then
    for j=2,i do tex.sprint ( ", " .. k:sub(j) .. k:sub(1,j-1) ) end
  end
  tex.sprint ( "\\\\" )
  end
end
\end{luacode}
%% LaTeX-side code
\newcommand\DoArray[1]{\directlua{do_array(#1)}}

\usepackage{array} % for '\newcolumntype' macro
\newcolumntype{P}{>{\raggedright\arraybackslash\hangafter=1\hangindent=1em}p{\textwidth}}

\begin{document}
\noindent
\begin{tabular}{@{}P@{}}
\DoArray{9}
\end{tabular}
\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.