![Tex 嵌套循环](https://linux22.com/image/238659/Tex%20%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF.png)
我正在尝试在 LaTex 中编写一个简单的嵌套循环,不使用任何特殊包,仅使用 \loop...\if... \repeat 语法。我想我可能缺乏对这个构造规则的基本了解,即是否允许嵌套循环?
我的应用程序是一个简单的日记生成器,下面是代码的简化版本,但它不起作用。它不会打印出 12 个月的 30 天,而是在页面底部给出输出。
\newcounter{currentday}
\newcounter{currentmonth}
\newcommand{thecurrentmonth}{\arabic{currentmonth}}
\newcommand{thecurrentday}{\arabic{currentday}}
\newcommand{\makewholediaryflat}{%
\setcounter{currentday}{1}
\setcounter{currentmonth}{1}
\loop % MONTH
Including a month: \thecurrentmonth
\setcounter{currentday}{1}%
\loop% DAY
Inputting Day \thecurrentday of month \thecurrentmonth
\addtocounter{currentday}{1}
Deciding whether to input day \thecurrentday...
\ifnum\value{currentday}< 30
\repeat% DAY
\addtocounter{currentmonth}{1}
Deciding whether to input month \thecurrentmonth...
\ifnum\value{currentmonth}<13
\repeat% MONTH
}
\begin{document}
\makewholediaryflat
\end{document}
关于为什么这不起作用,我遗漏了什么简单的事情?程序的输出是:
Including a month: 1
Inputting Day 1of month 1
Deciding whether to input day 2...
Inputting Day 2of month 1
Deciding whether to input day 3...
Inputting Day 3of month 1
Deciding whether to input day 4...
Inputting Day 4of month 1
Deciding whether to input day 5...
Inputting Day 5of month 1
Deciding whether to input day 6...
Inputting Day 6of month 1
Deciding whether to input day 7...
Inputting Day 7of month 1
Deciding whether to input day 8...
Inputting Day 8of month 1
Deciding whether to input day 9...
Inputting Day 9of month 1
Deciding whether to input day 10...
Inputting Day 10of month 1
Deciding whether to input day 11...
Inputting Day 11of month 1
Deciding whether to input day 12...
Inputting Day 12of month 1
Deciding whether to input day 13...
Inputting Day 13of month 1
Deciding whether to input day 14...
Inputting Day 14of month 1
Deciding whether to input day 15...
Inputting Day 15of month 1
Deciding whether to input day 16...
Inputting Day 16of month 1
Deciding whether to input day 17...
Inputting Day 17of month 1
Deciding whether to input day 18...
Inputting Day 18of month 1
Deciding whether to input day 19...
Inputting Day 19of month 1
Deciding whether to input day 20...
Inputting Day 20of month 1
Deciding whether to input day 21...
Inputting Day 21of month 1
Deciding whether to input day 22...
Inputting Day 22of month 1
Deciding whether to input day 23...
Inputting Day 23of month 1
Deciding whether to input day 24...
Inputting Day 24of month 1
Deciding whether to input day 25...
Inputting Day 25of month 1
Deciding whether to input day 26...
Inputting Day 26of month 1
Deciding whether to input day 27...
Inputting Day 27of month 1
Deciding whether to input day 28...
Inputting Day 28of month 1
Deciding whether to input day 29...
Inputting Day 29of month 1
Deciding whether to input day 30...
Inputting Day 30of month 1
Deciding whether to input day 31...
Deciding whether to input month 2...
Deciding whether to input month 3...
Deciding whether to input month 4...
Deciding whether to input month 5...
Deciding whether to input month 6...
Deciding whether to input month 7...
Deciding whether to input month 8...
Deciding whether to input month 9...
Deciding whether to input month 10...
Deciding whether to input month 11...
Deciding whether to input month 12...
Deciding whether to input month 13...
Deciding whether to input month 14...
Deciding whether to input month 15...
Deciding whether to input month 16...
Deciding whether to input month 17...
Deciding whether to input month 18...
Deciding whether to input month 19...
Deciding whether to input month 20...
Deciding whether to input month 21...
Deciding whether to input month 22...
Deciding whether to input month 23...
Deciding whether to input month 24...
Deciding whether to input month 25...
Deciding whether to input month 26...
Deciding whether to input month 27...
Deciding whether to input month 28...
Deciding whether to input month 29...
Deciding whether to input month 30...
Deciding whether to input month 31...
答案1
内部\loop
必须用括号括起来
\newcommand{\makewholediaryflat}{%
\setcounter{currentday}{1}%
\setcounter{currentmonth}{1}%
\loop % MONTH
Including a month: \thecurrentmonth
{\loop % DAY
Inputting Day \thecurrentday of month \thecurrentmonth
\addtocounter{currentday}{1}
Deciding whether to input day \thecurrentday...
\ifnum\value{currentday} < 5
\repeat % DAY
}
\setcounter{currentday}{1}%
\addtocounter{currentmonth}{1}
Deciding whether to input month \thecurrentmonth...
}
\ifnum\value{currentmonth} < 13%
\repeat % MONTH
}
但是,您应该小心定义中的空行。TeX 不是“自由格式”,空行会开始一个新段落。
答案2
\loop
通过定义一个宏 来工作,\iterate
该宏保存循环体(直到\repeat
)。当您尝试嵌套\loop
时,会出现两个问题:
第一个
\loop
认为其主体由第一个\repeat
而不是第二个来界定。通过将第一个隐藏\repeat
在括号内可以解决这个问题。内部
\loop
重新定义\iterate
为与自身主体相等。通过将内部\loop...\repeat
构造放入组中可以解决这个问题。
通过将内部括在括号内,可以同时完成这两项操作\loop...\repeat
。这与“将循环主体放在括号内”无关。对于您的特定应用,请参阅 egreg 的答案。
\newcount\X
\newcount\Y
\loop
\Y = 0
\advance \X by 1
{%
\loop
\advance \Y by 1
\message{\the\X,\the\Y}
\ifnum \Y < 10
\repeat
}%
\ifnum \X < 10
\repeat
答案3
主体\loop
需要是一个块:
\newcommand{\makewholediaryflat}{%
\setcounter{currentday}{1}%
\setcounter{currentmonth}{1}%
\loop{ % MONTH
Including a month: \thecurrentmonth
\loop{ % DAY
Inputting Day \thecurrentday of month \thecurrentmonth
\addtocounter{currentday}{1}
Deciding whether to input day \thecurrentday...
}
\ifnum\value{currentday} < 5
\repeat % DAY
\setcounter{currentday}{1}%
\addtocounter{currentmonth}{1}
Deciding whether to input month \thecurrentmonth...
}
\ifnum\value{currentmonth} < 13%
\repeat % MONTH
}