在 LaTeX 中仅从列表中选择某些项目
我有一个 LaTeX 文档,它基本上是一个大型枚举
环境,包含数百个项目。我希望能够发出这样的命令
\printitems{2,5,12,45-48}
,该命令将仅输出请求的项目。
类似的命令 \onlyslides
是 slides.cls
的一部分,但我无法弄清楚那里发生了什么并使其适应我的需要。
我可以用环境列表替换 item
列表,例如
\begin{myitem}
...
\end{myitem}
\begin{myitem}
...
\end{myitem}
使用 \newcounter
等,如果它有助于实现我的目的 - 只能打印一些具有给定编号的项目,无需剪切和粘贴。如果需要,我可以将这些项目放在一个文件中,并将 \printitems
命令放在另一个文件中。
我无法将数字放入文件中 - 文件不断变化,我需要自动枚举。
I have a LaTeX document which is basically one big enumerate
environment, with a few hundreds of items. I want to be able to issue a command like
\printitems{2,5,12,45-48}
which will output only the requested items.
A similar command \onlyslides
is a part of slides.cls
, but I cannot figure out what goes on there and adapt it to my needs.
I can replace the list of item
's with a list of environments, like
\begin{myitem}
...
\end{myitem}
\begin{myitem}
...
\end{myitem}
with a \newcounter
etc. if it helps to achieve my purpose — being able to print only some items with given numbers without cut-and-pasting. I can have the items in one file, and the \printitems
command in another, if needed.
I can not put the numbers in the file — the file is constantly changing, and I need the automatic enumeration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,那么,我们开始吧。
如下所示,编码的主要部分是解析逗号分隔的范围输入。之后,很容易检查您在枚举环境(或其他环境)中达到的数字并有条件地显示该项目。
您可以从这里复制并粘贴到一个空的
.tex
文档中,它应该可以正常工作:%% 首先,我使用 expl3 包来完成大部分编码。让一些事情变得更容易。
%% 这是循环逗号列表范围输入的函数,如
-2,4-6,8,10-
:%% 以及返回输入参数是否包含在范围内的辅助函数:
%% 这是输入列表中每个项目的命令:
%% 以及带有范围参数的枚举环境:
%% 最后,一个示例:
Alright, then, here we go.
As you can see below, the main part of the coding is parsing the comma separated range input. After that, it's easy to check what number you're up to in the enumerate environment (or whatever) and conditionally display the item.
You can copy and paste from here on into an empty
.tex
document and it should just work:%% First of all, I'm using the expl3 package to do most of this coding. Makes some things easier.
%% Here's the function to loop over comma-list range input like
-2,4-6,8,10-
:%% And the auxiliary function to return whether the input argument is contained within the range:
%% This is the command to input each item of your list:
%% And the enumerate environment with a range argument:
%% Finally, an example:
这样做的困难(或者更确切地说,不平凡)的部分是解析逗号分隔的输入。有几个包已经实现了这个功能;也许 Lipsum 的实现足够简单,可以根据您的目的进行提取。
之后,只需在每次点击某个项目时步进计数器即可,并根据逗号列表的解析方式显示或不显示内容。
The difficult (or rather, non-trivial) part of doing this is parsing the comma-separated input. Several packages have implemented this; perhaps lipsum's implementation is simple enough to extract for your purposes.
After that, it's just a matter of stepping a counter every time you hit an item and either display the contents or not depending on how the comma-list is parsed.
首先使用 foreach 函数分割
Split comma-separated parameters in LaTeX 中 给出的逗号分隔列表
您可以通过删除使用的常量参数 #2 来简化它。
然后按如下方式定义您的项目(这是在 TeX 中构建哈希的方法):
定义要在 foreach 中使用的函数:
最后定义
printitems
First use the foreach function to split a comma separated list given in
Split comma-separated parameters in LaTeX
You can simplify it by removing the constant parameter #2 used.
Then define your items as following (this is the way to build a hash in TeX):
Define a function to be used in foreach:
Last define
printitems