LaTeX 有数组数据结构吗?

发布于 2024-08-28 16:17:37 字数 365 浏览 8 评论 0原文

LaTeX 中有数组吗?我的意思不是排版数组的方式。我的意思是数组作为 LaTeX/TeX 中的数据结构作为“编程语言”。我需要在数组中存储多个 vbox-es 或 hbox-es。它可能类似于“宏数组”。

更多细节:我有一个应该排版歌曲的环境。我需要存储一些歌曲的段落作为我的宏 \songparagraph 的参数(所以我不会排版它们,只存储这些段落)。由于我不知道一首特定歌曲可以有多少段落,因此我需要一个数组。当环境关闭时,所有段落都将被排版 - 但将首先测量它们,并计算每个段落的最佳位置(例如,某些段落可以并排放置在两列中,以使歌曲看起来像更紧凑并节省一些空间)。

任何想法都会受到欢迎。 如果您了解 LaTeX 中的数组,请发布一些基本文档、教程的链接或仅说明基本命令。

Are there arrays in LaTeX? I don't mean the way to typeset arrays. I mean arrays as the data structure in LaTeX/TeX as a "programming language". I need to store a number of vbox-es or hbox-es in an array. It may be something like "an array of macros".

More details: I have an environment that should typeset songs. I need to store some songs' paragraphs given as arguments to my macro \songparagraph (so I will not typeset them, just store those paragraphs). As I don't know how many paragraphs can be in one particular song I need an array for this. When the environment is closed, all the paragraphs will be typeset - but they will be first measured and the best placement for each paragraph will be computed (for example, some paragraphs can be put one aside the other in two columns to make the song look more compact and save some space).

Any ideas would be welcome.
Please, if you know about arrays in LaTeX, post a link to some basic documentation, tutorial or just state basic commands.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

平安喜乐 2024-09-04 16:17:37

这是一个如何在 LaTeX 中实现的数组:

\documentclass{article}
\begin{document}

\newcounter{mycounter}
\setcounter{mycounter}{1}

% ary is any prefix you want, it should not exist as a command.

\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{myfirstelement}
\stepcounter{mycounter}
\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{mysecondelement}

\csname ary1 \endcsname

or

\newcounter{index}
\setcounter{index}{2}

\csname ary\the\value{index} \endcsname

\end{document}

通过 LaTeX(latex mydoc.tex 或 pdflatex mydoc.tex)运行它,您会看到输出。

简短说明:这将创建两个命令(使用 newcommand):ary1 和 ary2。需要 \expandafter 是因为 newcommand 不应该定义 \csname 而是由 \csname 创建的命令... \endcsname\expandafter 跳过下一个标记,在本例中是控制序列 \newcommand 并在 TeX 看到 \newcommand 之前执行下一个命令。这意味着,TeX 在新命令行中首先看到的是 \csname...\endcsname 构造,TeX 执行它,然后执行 \newcommand< /code> 与 \csname...\endcsname 构造的结果。 \csname foo\endcsname\foo 相同,但您可以在 \csname 创建的命令中使用任何字符甚至空格。 ..<代码>\endcsname。

这并非小事。请参阅 Victor Eijkhout 的伟大著作“TeX by topic”:http://eijkhout.net/texbytopic/texbytopic。 html

This is an array how it could be implemented in LaTeX:

\documentclass{article}
\begin{document}

\newcounter{mycounter}
\setcounter{mycounter}{1}

% ary is any prefix you want, it should not exist as a command.

\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{myfirstelement}
\stepcounter{mycounter}
\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{mysecondelement}

\csname ary1 \endcsname

or

\newcounter{index}
\setcounter{index}{2}

\csname ary\the\value{index} \endcsname

\end{document}

Run this through LaTeX (latex mydoc.tex or pdflatex mydoc.tex) and you see the output.

A short explanation: this creates two commands (with newcommand): ary1 and ary2. The \expandafter is needed because newcommand should not define \csname but the command created by \csname ... \endcsname. \expandafterjumps over the next token, in this case the control sequence \newcommand and executes the next command before TeX sees the \newcommand. That means, the first thing in the newcommand-lines TeX sees is the \csname...\endcsname construct, TeX executes it and then executes \newcommand with the result of the \csname...\endcsname construct. \csname foo\endcsname is the same as \foo, but you can use any character or even spaces in the command created by \csname...\endcsname.

This is not trivial. See the great book "TeX by topic" from Victor Eijkhout: http://eijkhout.net/texbytopic/texbytopic.html

爱殇璃 2024-09-04 16:17:37

Luatex 有 Lua 的表,它概括了数组。如果标准 Latex 对您来说并不重要,请考虑将 Luatex 与 Latex 一起使用。然后,您可以执行以下操作:

\def\lookup#1{\directlua {
    local array={1,2,4,8}; tex.print(array[#1])}}
\[ 2 \mapsto \lookup{2} \]

Luatex 与 Latex 相比有点不稳定,因为需要转义 Latex 代码中的各种 Lua 字符。 Context 有 \startluacode ... \stopluacode 宏来处理 Lua 代码定义,以及 \ctxlua 用于 Lua 代码调用,我看不出为什么会出现这样的情况无法为 Latex 定义。

Luatex has Lua's tables, which generalise arrays. If having standard Latex is not important to you, consider using Luatex with Latex. You can then do such things as:

\def\lookup#1{\directlua {
    local array={1,2,4,8}; tex.print(array[#1])}}
\[ 2 \mapsto \lookup{2} \]

Luatex is a bit flaky with Latex, because of the need to escape all kinds of Lua characters in the Latex code. Context has \startluacode ... \stopluacode macros to handle Lua code definitions, and \ctxlua for Lua code calls, and I can't see any reason why something like these couldn't be defined for Latex.

递刀给你 2024-09-04 16:17:37

pgfkeys 和 pgffor 可能也适合您。它们是 pgf (便携式图形格式)包的一部分,但它们可以独立于所有包使用图形的东西。

pgfkeys and pgffor might also work for you. They're part of the pgf (portable graphics format) package but they can be used independently of all the graphics stuff.

芯好空 2024-09-04 16:17:37

为了扩展帕特里克的答案,简短的答案是“不”。然而,由于它具有宏扩展,因此可以编程为具有数组。

这是另一个示例,该示例使用 push 和 pop 来表示“数组”。当调用 \type@pushcolour 时,它将当前颜色保存到堆栈中。 \type@popcolour 采用顶部颜色定义,并使用它:(

\newcount\type@count
\def\type@pushcolour{%
  \xglobal\colorlet{foo\the\type@count}{.}%
\global\advance\type@count by1\relax}
\def\type@popcolour{%
  \global\advance\type@count by-1\relax%
\color{foo\the\type@count}}

我从 beamer 包的源代码中改编了此代码)

To expand on Patrick's answer, the short answer is "No". However, as it has macro expansion, it can be programmed to have arrays.

Here's another example, this one using push and pop for "arrays". When \type@pushcolour is called, it saves the current colour on to the stack. \type@popcolour takes the top colour definition, and uses it:

\newcount\type@count
\def\type@pushcolour{%
  \xglobal\colorlet{foo\the\type@count}{.}%
\global\advance\type@count by1\relax}
\def\type@popcolour{%
  \global\advance\type@count by-1\relax%
\color{foo\the\type@count}}

(I adapted this code from the source for the beamer package)

软糖 2024-09-04 16:17:37

您还可以查看 datatool 包或 expl3 编程系统以及“属性列表”数据类型之类的内容。

You could also look at something like the datatool package or the expl3 programming system, and the "property list" data type.

雨后咖啡店 2024-09-04 16:17:37

查看Arrayjob,它为 LaTeX 实现了数组。诚然,我只是看了一眼,所以我不知道它的效果如何。但是,如果您不必自己编写......

Check out Arrayjob which implements arrays for LaTeX. Admittedly, I've only peeked at it, so I don't know how effective it will be. But, if you don't have to write it yourself ...

以往的大感动 2024-09-04 16:17:37

readarray 包允许将格式化数据输入到元素中
2-D 或 3-D 数组(或 1-D 文件记录数组)。

\documentclass{standalone}
\usepackage{readarray}

\def\data{% the data
1 15 14 4
10 11 8 5
7 6 9 12
16 2 3 13
}
\readarray\data\dataA[4,4] %read the data to \dataA

\begin{document}
value at (2,1) = \dataA[2,1] %access a specific field
\end{document}

The readarray package allows one to input formatted data into elements
of a 2-D or 3-D array (or a 1-D file-record array).

\documentclass{standalone}
\usepackage{readarray}

\def\data{% the data
1 15 14 4
10 11 8 5
7 6 9 12
16 2 3 13
}
\readarray\data\dataA[4,4] %read the data to \dataA

\begin{document}
value at (2,1) = \dataA[2,1] %access a specific field
\end{document}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文