标记某些行并将它们添加到列表中

标记某些行并将它们添加到列表中

我想知道是否可以“标记”某些文本行,然后将它们调用到单独的页面中,如下所示:

可能的考试题目{Lorem Ipsum 的段落有很多变体,但大多数都以某种形式遭到了篡改},注入了幽默或随机的词语,看起来甚至不太可信。可能的考试题目{如果您要使用一段 Lorem Ipsum},您需要确保文本中间没有任何令人尴尬的内容。互联网上的所有 Lorem Ipsum 生成器都倾向于根据需要重复预定义的块,这使得它成为互联网上第一个真正的生成器。它使用超过 200 个拉丁词的词典,结合少量模型句子结构,生成看起来合理的 Lorem Ipsum。可能的考试题目{因此,生成的 Lorem Ipsum 始终不会出现重复的情况。}

当然,“可能的考试题目”不应该是可见的,它只是代码中的一个“标签”。标记所需的行后,我希望将它们放在单独的页面上,如下所示:

以下是可能的考试题目:

  • 有许多不同版本的 Lorem Ipsum 可供使用,但大多数都以某种形式遭到了篡改
  • 如果你要使用 Lorem Ipsum 的一段话
  • 因此,生成的 Lorem Ipsum 始终不存在重复。

提前感谢大家。另外请注意,我是菜鸟,所以如果可以的话,请详细说明您的答案,以便我更好地了解这将如何工作。

答案1

这是一个实现:文本按顺序存储,然后\item在块之间传递。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\peq}{m}
 {
  \edoardo_peq_store:n { #1 }
  #1
 }
\NewDocumentCommand{\printpeq}{}
 {
  Here~are~the~possible~exam~questions
  \begin{itemize}
  \item \seq_use:Nn \g_edoardo_peq_seq { \item }
  \end{itemize}
 }


\seq_new:N \g_edoardo_peq_seq

\cs_new_protected:Nn \edoardo_peq_store:n
 {
  \seq_gput_right:Nn \g_edoardo_peq_seq { #1 }
 }

\ExplSyntaxOff

\begin{document}

\section{The text}

\peq{There are many variations of passages of Lorem Ipsum available, 
but the majority have suffered alteration in some form}, by injected 
humour, or randomised words which don't look even slightly believable. 
\peq{If you are going to use a passage of Lorem Ipsum}, you need to be 
sure there isn't anything embarrassing hidden in the middle of text. 
All the Lorem Ipsum generators on the Internet tend to repeat predefined 
chunks as necessary, making this the first true generator on the Internet. 
It uses a dictionary of over 200 Latin words, combined with a handful of 
model sentence structures, to generate Lorem Ipsum which looks reasonable.
\peq{The generated Lorem Ipsum is therefore always free from repetition.}

\section{The possible exam questions}

\printpeq

\end{document}

在此处输入图片描述

\peq避免在处理目录时存储的实现。

\documentclass{article}
\usepackage{xpatch}

\ExplSyntaxOn

\NewDocumentCommand{\peq}{m}
 {
  \edoardo_peq_store:n { #1 }
  #1
 }
\NewDocumentCommand{\printpeq}{}
 {
  Here~are~the~possible~exam~questions
  \begin{itemize}
  \item \seq_use:Nn \g_edoardo_peq_seq { \item }
  \end{itemize}
 }


\seq_new:N \g_edoardo_peq_seq
\bool_new:N \l_edoardo_peq_toc_bool

\cs_new_protected:Nn \edoardo_peq_store:n
 {
  \bool_if:NF \l_edoardo_peq_toc_bool
   {% don't store if in the toc
    \seq_gput_right:Nn \g_edoardo_peq_seq { #1 }
   }
 }

\makeatletter
\xpatchcmd{\@starttoc}
  {\begingroup}
  {\begingroup\bool_set_true:N \l_edoardo_peq_toc_bool}
  {}{}
\makeatother

\ExplSyntaxOff

\begin{document}

\tableofcontents

\section{The text \peq{with a possible question}}

\peq{There are many variations of passages of Lorem Ipsum available, 
but the majority have suffered alteration in some form}, by injected 
humour, or randomised words which don't look even slightly believable. 
\peq{If you are going to use a passage of Lorem Ipsum}, you need to be 
sure there isn't anything embarrassing hidden in the middle of text. 
All the Lorem Ipsum generators on the Internet tend to repeat predefined 
chunks as necessary, making this the first true generator on the Internet. 
It uses a dictionary of over 200 Latin words, combined with a handful of 
model sentence structures, to generate Lorem Ipsum which looks reasonable.
\peq{The generated Lorem Ipsum is therefore always free from repetition.}

\section{The possible exam questions}

\printpeq

\end{document}

答案2

这是 LaTeX2e 版本。方法是维护一个计数器,peqs统计“可能的考试问题”的当前数量。当使用宏调用标记问题时\peq,问题的副本将保存在连续的宏\peq1\peq2、... 中(当然,由于12不是字母,因此必须以\csname peq1\endcsname等形式访问这些宏)。

稍后,当\printpeqs被调用时,会建立一个\tmp包含的循环\item\peq1 \item\peq2 ...\item\peq<n>\tmp然后在环境中进行设置itemize

所有这些\expandafters都是必需的,因为12、 ...<n>包含在本地循环索引中\z,否则每次计数增加时都会丢失。所以我需要在每次循环时扩展并捕获它。

\documentclass{article}
\usepackage{pgffor}
\newcounter{peqs}
\newcommand\peq[1]{%
  \stepcounter{peqs}%
  \expandafter\def\csname peq\thepeqs\endcsname{#1}%
  #1%
}
\newcommand\printpeqs{%
  \def\tmp{}%
  \foreach\z\ in{1,...,\thepeqs}{%
    \expandafter\expandafter\expandafter\gdef\expandafter\expandafter
      \expandafter\tmp\expandafter\expandafter\expandafter{%
      \expandafter\tmp\expandafter\item\csname peq\z\endcsname.}
  }%
  \begin{itemize}
  \tmp
  \end{itemize}
}

\begin{document}

\section{The text}

\peq{There are many variations of passages of Lorem Ipsum available, 
but the majority have suffered alteration in some form}, by injected 
humour, or randomised words which don't look even slightly believable. 
\peq{If you are going to use a passage of Lorem Ipsum}, you need to be 
sure there isn't anything embarrassing hidden in the middle of text. 
All the Lorem Ipsum generators on the Internet tend to repeat predefined 
chunks as necessary, making this the first true generator on the Internet. 
It uses a dictionary of over 200 Latin words, combined with a handful of 
model sentence structures, to generate Lorem Ipsum which looks reasonable.
\peq{The generated Lorem Ipsum is therefore always free from repetition}.

\section{The possible exam questions}

\printpeqs
\end{document}

在此处输入图片描述

如果愿意使用,那么可以通过重新定义来省去\g@addto@macro几个s:\expandafter

\makeatletter
\newcommand\printpeqs{%
  \def\tmp{}%
  \foreach\z\ in{1,...,\thepeqs}{%
    \expandafter\g@addto@macro\expandafter\tmp\expandafter{%
      \expandafter\item\csname peq\z\endcsname.}
  }%
  \begin{itemize}
  \tmp
  \end{itemize}
}
\makeatother

相关内容