你能用 LaTeX 迭代吗?
我是 LaTeX 的新手,我必须说我真的很挣扎。 我发现 \newcommand 命令有点像常规编程语言中的函数/方法。 你可以给它论据和一切。
但我想知道,我可以以某种方式在 LaTeX 中迭代吗? 基本上,我想做的是创建一个包含 N+1 列的表格,其中第一行仅包含一个空白单元格,然后其他列中包含数字 1, 2, ..., N。 我只想将 N 作为这个“函数”(newcommand)的参数。
这是一个可能看起来像我正在寻找的东西的示例(尽管显然这不起作用):
\newcommand{\mytable}[2]{
\begin{tabular}{l|*{#1}{c|}} % table with first argument+1 columns
for(int i = 1; i <= #1; i++) "& i" % 'output' numbers in different columns
\\\hline
letters & #2 % second argument should contain actual content for row
\\\hline
\end{tabular}
}
调用它:
\mytable{3}{a & b & c}
输出应该是:
| 1 | 2 | 3 | --------+---+---+---+ letters | a | b | c | --------+---+---+---+
有谁知道这样的事情是否可能?
谢谢!
I'm new to LaTeX and I must say that I am really struggling with it. I discovered the \newcommand command that is kind of like a function/method in regular programming languages. You can give it arguments and everything.
I was wondering though, can I somehow iterate in LaTeX? Basically, what I would like to do is create a table with N+1 columns where the first row just contains a blank cell and then the numbers 1, 2, ..., N in the other columns. I only want to give N as an argument to this 'function' (newcommand).
Here is an example of something that might look like what I'm looking for (although obviously this won't work):
\newcommand{\mytable}[2]{
\begin{tabular}{l|*{#1}{c|}} % table with first argument+1 columns
for(int i = 1; i <= #1; i++) "& i" % 'output' numbers in different columns
\\\hline
letters & #2 % second argument should contain actual content for row
\\\hline
\end{tabular}
}
Call it with:
\mytable{3}{a & b & c}
Output should be:
| 1 | 2 | 3 | --------+---+---+---+ letters | a | b | c | --------+---+---+---+
Does anyone know if something like this is possible?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将以下内容放入新命令中,并确保使用包
ifthen
即可。Just make the following into a new command and be sure to use package
ifthen
.您可以使用
\loop
或\repeat
标记。 或者multido
包。You can use the
\loop
or\repeat
tokens. Or themultido
package.当然有可能。 也可以复发。 eplain 中有迭代宏,请参见此处。
Sure it's possible. You can also recur. eplain has iteration macros in it, see, eg, here.
另一种可能性(如果你像我一样懒)是
Another possibility (if you're lazy like me) is perltex