5

I would like to know if it's possibile to "tag" certain lines of text and then recall them into a separate page, like this:

Possible exam question{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. Possible exam question{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. Possible exam question{The generated Lorem Ipsum is therefore always free from repetition.}

Of course the "possible exam question" should not be visible, it's just a "tag" into the code. After tagging desired lines I would like to have them on a separate page like this:

Here's the possible exam questions:

  • There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form
  • If you are going to use a passage of Lorem Ipsum
  • The generated Lorem Ipsum is therefore always free from repetition.

Thank's in advance to everybody. Also please note I'm a noob so please, if you can, elaborate your answer so I can understand more how this would work.

5

Here's an implementation: the texts are stored in a sequence that's then delivered with \item in between chunks.

\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}

enter image description here

An implementation that avoids storing \peq when processing the table of contents.

\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}
9
  • Thank's so much. Could you please explain me, in a simply way (for how simple it can be), what does mean from "\ExplSyntaxOn" to "\ExplSyntaxOff"? I would like to understand these commands in order to get better with latex. Thank's. – Edoardo Mar 8 at 15:58
  • Also, I'm getting several errors when pasting your commands. Do I need to install some packages? – Edoardo Mar 8 at 16:20
  • @Edoardo You probably have an older TeX distribution. Add \usepackage{xparse}. About the syntax, you should consult texdoc xparse and texdoc interface3 – egreg Mar 8 at 18:02
  • Yes, I resolved it adding \usepackage{amsthm,xparse}. I'll try to do so even if it seem too complex for my level, are these a website texdoc xparse and texdoc interface3? – Edoardo Mar 8 at 18:13
  • hi @egreg sorry for necroposting but I have an issue with this, if I "tag" an element of text contained in a chapter title or subsections I get the item typed two times in the \printpeq list. Why? – Edoardo Mar 14 at 8:28
1

Here is a LaTeX2e version. The approach is to maintain a counter peqs tabulating the current count of "possible exam questions". When a question is flagged with an invocation of the \peq macro, a copy of the question is saved in successive macros \peq1, \peq2, ... (of course, since 1 and 2 are not letters, these macros must be accessed as \csname peq1\endcsname etc.).

Later, when \printpeqs is invoked, a loop is set up build \tmp containing \item\peq1 \item\peq2 ...\item\peq<n>. This \tmp is then set within an itemize environment.

All the \expandafters are needed because the 1, 2, ...<n> are contained in the local loop index \z, which is otherwise lost with each advance in count. So I need to expand and capture it with each pass through the loop.

\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}

enter image description here

If one is willing to use \g@addto@macro, then a few \expandafters could be spared with this redefinition:

\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
2
  • Thank's. Could you some comments in the code where you show how it works? Or could you simply tell me where can I find a simple explaination? – Edoardo Mar 10 at 8:54
  • @Edoardo I have added descriptive text to my answer. – Steven B. Segletes Mar 10 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.