我的目标是生成下面形式的数字。
最小代码:
\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}
谢谢你的帮助
答案1
仅\foreach
命令和\ifnum
声明
\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}
答案2
比以下更好\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}
实际上,编写宏比直接输入表格要长。但是,我发现如何生成包含循环排列的序列很有趣。
如果你还定义
\NewDocumentCommand{\cyclic}{m}
{
\__ahmadi_cycle:n { #1 }
}
然后输入\cyclic{abcdef}
将会打印
答案3
怎么样
\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}
或者完全没有包裹。
\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}
答案4
基于 LuaLaTeX 的解决方案。它定义了一个名为 的 LaTeX 宏\DoArray
,该宏接受一个参数,即一个整数n
。(如果您处理的是十进制数字,n
则不应大于 9,对吗?)\DoArray
调用一个名为 的 Lua 函数do_array
,该函数几乎可以完成所有工作。
当字符串不能全部放在一行中时,环境tabular
设置为执行自动换行和悬挂缩进。n
\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}