ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Otared Kavian <otared@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Clickable question/answer numbers
Date: Sun, 6 Nov 2016 15:46:24 +0100	[thread overview]
Message-ID: <F3DC2C36-D331-40EF-B2A6-76C179B6925D@gmail.com> (raw)
In-Reply-To: <581F1A49.1080001@gmail.com>

Hi Wolfgang, Hi Mikael,

Thanks Wolfgang! I didn’t know that \beginBLOCKNAME \endBLOCKNAME cannot be used in other macros… There is some mystery in this for me.

Now with 
	\doifnotmode{*trialtypesetting}
everything works as expected. 

Since I don’t know whether, nor where on the wiki, this can be wikified, for the archives and for other possible users, I copy below the entire example which allows to have interactive Questions and Answers. One can also easily add a chapter for Hints, if necessary.

Best regrads: OK

%%%% begin interactive-question-answer.tex
%%%% Here, following Wolfgang Schuster's ideas, 
%%%% we define some macros allowing to couple
%%%% Questions and Answers in an automatic way
%%%% Upon clicking on an interactive title for Question or Answer
%%%% one goes to the corresponding Answer or Question

\setupinteraction[state=start]

% We define two counters which follow the numbers
% appearing in Question and Answer

\newcounter\QuestionCounter
\newcounter\AnswerCounter

% We create two commands to be used in the
% enumeration environments
% Note that the check for trial typestting 
% \doifnotmode{*trialtypesetting}
% is necessary in order to avoid unwanted incrementation
\define[1]\QuestionTextCommand
  {\doifnotmode{*trialtypesetting}
     {\doglobal\increment\QuestionCounter
      \pagereference[question:\QuestionCounter]}%
   \doifreferencefoundelse{answer:\QuestionCounter}
     {\goto{#1}[answer:\QuestionCounter]}
     {#1}}

\define[1]\AnswerTextCommand
  {\doifnotmode{*trialtypesetting}
     {\doglobal\increment\AnswerCounter
      \pagereference[answer:\AnswerCounter]}%
   \doifreferencefoundelse{question:\AnswerCounter}
     {\goto{#1}[question:\AnswerCounter]}
     {#1}}

% We define here two enumeration environments for
% Questions and Answers
\defineenumeration[question]
	[text=Question,
	headcommand=\QuestionTextCommand,
	number=yes,
	prefix=yes,
	prefixsegments=chapter,
	coupling=answer]

\defineenumeration[answer]
	[text=Answer,
	headcommand=\AnswerTextCommand,
	number=yes,
	prefix=yes,
	prefixsegments=chapter,
	coupling=question]

% Each question is followed immediately by its answer.
% The answers are put in a block which will be used later
\defineblock[answer]
\hideblocks[answer]

% We define a command used after a  
% Question for which no Answer is provided
% Since the block commands 
% \beginanswer ... \endanswer
% cannot be used directly in a macro definition
% we use a trick...
% In the buffer the two counters associated to Answer
% are incremented...
\startbuffer[noanswer]
    \beginanswer
    \doglobal\increment\AnswerCounter
    \incrementcounter[answer]
    \endanswer
\stopbuffer

% ...and then the above buffer is invoked
\define\noanswer
  {\getbuffer[noanswer]}

% example of use:
\starttext
\startchapter[title=Questions]

\startquestion[q:1]
Prove that ${\rm e}\sim 2.73$ is irrational.
    
This is the first question, with its own reference, for later use.
\stopquestion
    
\beginanswer
\startanswer
This is the answer to the first question (to \in{Question}[q:1]).
\stopanswer
\endanswer

\startquestion 
This is the second question, without its own reference.
\stopquestion
    
\beginanswer
\startanswer[a:Test]
This is the answer to the second question. 

(Note that this answer has a reference named \type{a:Test}).
\stopanswer
\endanswer

\startquestion[q:Obvious]
This is the third question, an easy one, without a given solution.
\stopquestion

% we increment here the counters for Answer
\noanswer

\startquestion[q:2]
This is the fourth question with its own reference.
\stopquestion

\beginanswer
\startanswer
This is the answer to the fourth question: use the result of \in{question}[q:1].
\stopanswer
\endanswer

\startquestion 
This is the fifth question, without its own reference. 

({\it Hint:} look again at \in{Question}[q:Obvious]).
\stopquestion
    
\beginanswer
\startanswer
This is the answer to the fifth question. Read again \in{Answer}[a:Test].
\stopanswer
\endanswer  

\stopchapter

% Here we say ownnumber=1, in order to match the prefix 
% of numbers associated to Answer
\startchapter[ownnumber=1,title=Answers and solutions]
\useblocks[answer]
\stopchapter

\stoptext
%%%% end interactive-question-answer.tex

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2016-11-06 14:46 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-02 11:43 Mikael P. Sundqvist
2016-11-02 12:52 ` Otared Kavian
2016-11-02 15:16   ` Mikael P. Sundqvist
2016-11-02 20:04     ` Otared Kavian
2016-11-02 20:58       ` Mikael P. Sundqvist
2016-11-03  9:34         ` Hans Hagen
2016-11-03 11:58           ` Mikael P. Sundqvist
2016-11-03 15:14             ` Mikael P. Sundqvist
2016-11-04  8:10               ` Otared Kavian
2016-11-04 11:50               ` Wolfgang Schuster
2016-11-04 15:33                 ` Mikael P. Sundqvist
2016-11-05 12:38                   ` Wolfgang Schuster
2016-11-05 12:43                     ` Mikael P. Sundqvist
2016-11-05 17:34                     ` Otared Kavian
2016-11-05 18:14                       ` Wolfgang Schuster
2016-11-06 10:43                         ` Otared Kavian
2016-11-06 11:55                           ` Wolfgang Schuster
2016-11-06 14:46                             ` Otared Kavian [this message]
     [not found] <mailman.245.1478488929.2018.ntg-context@ntg.nl>
2016-11-07 12:19 ` Jeong Dal
2016-11-07 12:46   ` Otared Kavian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=F3DC2C36-D331-40EF-B2A6-76C179B6925D@gmail.com \
    --to=otared@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).