从LaTeX的列表中仅选择一些项目 [英] Select only some items from a list in LaTeX

查看:140
本文介绍了从LaTeX的列表中仅选择一些项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LaTeX文档,它基本上是一个很大的enumerate环境,其中包含数百个项目.我希望能够发出类似

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.

类似的命令\onlyslidesslides.cls的一部分,但我无法弄清楚那里发生了什么并使其适应我的需求.

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.

我可以将环境列表替换为item列表,例如

I can replace the list of item's with a list of environments, like

\begin{myitem}
...
\end{myitem}

\begin{myitem}
...
\end{myitem}

\newcounter等,以帮助实现我的目的—能够仅打印具有给定编号的某些项目而无需剪切和粘贴.如果需要,我可以将项目放在一个文件中,而\printitems命令放在另一个文件中.

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.

推荐答案

好的,那么,我们开始吧.

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.

您可以从此处复制并粘贴到一个空的.tex文档中,它应该可以正常工作:

You can copy and paste from here on into an empty .tex document and it should just work:

%%首先,我正在使用expl3包来执行大多数此类编码.使某些事情变得更容易.

%% First of all, I'm using the expl3 package to do most of this coding. Makes some things easier.


\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn

%%这是循环遍历逗号列表范围输入(如-2,4-6,8,10-)的功能:

%% Here's the function to loop over comma-list range input like -2,4-6,8,10-:


\prg_new_conditional:Nnn \i_in_range:nn {TF,T,F} {
  \bool_set_false:N \l_tmpa_bool
  \clist_map_inline:nn {#2} {
    \parse_range:w ##1 - \q_marker - \q_nil #1 \q_nil
  }
  \bool_if:NTF \l_tmpa_bool \prg_return_true: \prg_return_false:
}

%%和辅助函数,用于返回输入参数是否包含在以下范围内:

%% And the auxiliary function to return whether the input argument is contained within the range:

\cs_set:Npn \parse_range:w #1 - #2 - #3 \q_nil #4 \q_nil {
  \tl_if_eq:nnTF {\q_marker}{#2}{
    \intexpr_compare:nT {#4=#1} {\bool_set_true:N \l_tmpa_bool}
  }{
    \tl_if_empty:nTF {#2}{
      \intexpr_compare:nT {#4>=#1} {\bool_set_true:N \l_tmpa_bool}
    }{
      \tl_if_empty:nTF {#1}{
        \intexpr_compare:nT {#4<=#2} {\bool_set_true:N \l_tmpa_bool}
      }{
        \intexpr_compare:nT {#4>=#1} {
          \intexpr_compare:nT {#4<=#2}
            {\bool_set_true:N \l_tmpa_bool}
        }
      }
    }
  }
}
\cs_generate_variant:Nn \i_in_range:nnTF {nV}

%%这是用于输入列表中每个项目的命令:

%% This is the command to input each item of your list:


\newcommand\numitem[1]{
  \i_in_range:nVTF {\value{enumi}+1}{\l_item_range_tl}{
    \item #1
  }{
    \stepcounter{enumi}
  }
}

%%以及带有range参数的枚举环境:

%% And the enumerate environment with a range argument:


\newenvironment{someitems}[1]{
  \tl_set:Nn \l_item_range_tl {#1}
  \begin{enumerate}
}{
  \end{enumerate}
}
\ExplSyntaxOff

%%最后,举一个例子:

%% Finally, an example:


\begin{document}
\begin{someitems}{-2,4-6,8,10-}
\numitem{one}\numitem{two}\numitem{three}
\numitem{four}\numitem{five}\numitem{six}
\numitem{seven}\numitem{eight}\numitem{nine}
\numitem{ten}\numitem{eleven}
\end{someitems}
\end{document}

这篇关于从LaTeX的列表中仅选择一些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆