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 11:43:08 +0100	[thread overview]
Message-ID: <13F55FB9-088F-4245-8DD0-D493B636829D@gmail.com> (raw)
In-Reply-To: <581E2183.6050600@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 7150 bytes --]

Hi Wolfgang,

Thanks for the solution you sent yesterday.
Your solutions solves the problem I was fighting with, but when I try to apply it to my real document there is an issue when some questions don’t have answers. This prevents the synchronization of Questions and Answers if one doesn't issue the commands
 
	\beginanswer
	\incrementcounter[answer]
	\endanswer

right after a question without an answer. And when the above commands are issued, the last Question does not show the link to its corresponding Answer.

Below is your modified example where one can see the problem (please see the fifth Question in the source below).

Best regards: OK
%%%% begin example-ws.tex

\setupinteraction[state=start]

\newcounter\QuestionCounter
\newcounter\AnswerCounter

\define[1]\QuestionTextCommand
  {\doglobal\increment\QuestionCounter
   \pagereference[question:\QuestionCounter]%
   \doifreferencefoundelse{answer:\QuestionCounter}
     {\goto{#1}[answer:\QuestionCounter]}
     {#1}}

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

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

\defineblock[answer]

%% Why this command generates an erreor?
\define\noanswer{%
	\beginanswer
		\incrementcounter[answer]
	\endanswer}

\define\PrintCounterValues{Here \type{\QuestionCounter = }\QuestionCounter.\par
Here \type{\AnswerCounter = }\AnswerCounter.\par
Here \type{\rawcountervalue[question] = }\rawcountervalue[question].\par
Here \type{\rawcountervalue[answer] = }\rawcountervalue[answer].
}


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

\PrintCounterValues
\stopquestion
    
\beginanswer
\startanswer
This is the answer to the first question (to \in{Question}[q:1]).

\PrintCounterValues
\stopanswer
\endanswer

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

\PrintCounterValues
\stopanswer
\endanswer

\startquestion
This is the third question, an easy one, without a given solution.

\PrintCounterValues
\stopquestion

%\noanswer\ % this generates an error…
\beginanswer
%\increment\AnswerCounter
%\incrementcounter[AnswerCounter]
%\increment\QuestionCounter
%\incrementcounter[QuestionCounter]
\incrementcounter[answer]
%\incrementcounter[question]
\endanswer

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

\PrintCounterValues
\stopquestion

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

\PrintCounterValues
\stopanswer
\endanswer
    
\startquestion 
This is the fifth question, without its own reference. (This Question is not linked to its Answer).

\PrintCounterValues
\stopquestion
    
\beginanswer
\startanswer
This is the answer to the fifth question. Read again \in{Answer}[a:2].

\PrintCounterValues
\stopanswer
\endanswer  
    
\page

\stopchapter

\startchapter[ownnumber=1,title=Answers and solutions]
\useblocks[answer]
\stopchapter

\stoptext
%%%% end example

> On 5 Nov 2016, at 19:14, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote:
> 
>> Otared Kavian <mailto:otared@gmail.com> 5. November 2016 um 18:34
>> Hi Wolfgang, Hi Mikael,
>> 
>> Thank you both for your great inputs!
>> 
>> I tried to modify the code each of you sent yesterday in order to « couple » automatically each question with its answer, using the keyword « coupling=… », but somehow I am unable to create automatic references by extracting the number of each question or that of each answer, for example by using \rawcountervalue,
>> or \currentconstructionnumber. Strangely the latter gives the name of the construction and not a number: is it on purpose?
> The \currentconstructionnumber command contains the name of the counter but getting the counter value won’t help because it comes too late.
>> The solution Wolfgang proposes today, using the new keyword referenceprefix, is almost perfect: however would it possible to create a version of
>> « currentconstructionreference » which creates a reference named for instance
>> answer:NumberOfCurentConstruction
> The referenceprefix key is only usefull when you set the reference by hand.
>> The reason for which I need such a construction is that in a document with hundreds or more exercise —> hint —> solution, it would be handy to avoid creating each time a manual reference, and then one would navigate from a Question to its Hint and then  back to the Question or go to its Answer.
> When you always all three elements (question, hint and answer) you can increment
> a counter at the begin of each element and create a page reference which contains
> the value of the counter.
> 
> Below is a modified version of my previous example for this method.
> 
> %%%% begin example
> \setupinteraction[state=start]
> 
> \define[1]\QuestionTextCommand
>   {\doglobal\increment\QuestionCounter
>    \pagereference[question:\QuestionCounter]%
>    \doifreferencefoundelse{answer:\QuestionCounter}
>      {\goto{#1}[answer:\QuestionCounter]}
>      {#1}}
> 
> \define[1]\AnswerTextCommand
>   {\doglobal\increment\AnswerCounter
>    \pagereference[answer:\AnswerCounter]%
>    \doifreferencefoundelse{question:\AnswerCounter}
>      {\goto{#1}[question:\AnswerCounter]}
>      {#1}}
> 
> \defineenumeration
>   [question]
>   [text=Question,
>    headcommand=\QuestionTextCommand]
> 
> \defineenumeration
>   [answer]
>   [text=Answer,
>    headcommand=\AnswerTextCommand]
> 
> \defineblock[answer]
> 
> \startbuffer
>     %
>     \startquestion
>     \input knuth
>     \stopquestion
>     %
>     \beginanswer
>         \startanswer
>         \input knuth
>         \stopanswer
>     \endanswer
>     %
> \stopbuffer
> 
> \starttext
> 
> \dorecurse{10}{\getbuffer}
> 
> \page
> 
> \useblocks[answer]
> 
> \stoptext
> %%%% end example
> 
> Wolfgang
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 15518 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
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 10:43 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 [this message]
2016-11-06 11:55                           ` Wolfgang Schuster
2016-11-06 14:46                             ` Otared Kavian
     [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=13F55FB9-088F-4245-8DD0-D493B636829D@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).