# [NTG-context] Cross-referencing Challenge

Aaron W. Hsu arcfide at sacrideo.us
Sun Apr 15 02:38:39 CEST 2012

Thank you for the response, my response is below...

On Sun, 15 Apr 2012 08:19:28 +0900, S Barmeier wrote:

> On 04/15/2012 07:44 AM, Aaron W. Hsu wrote:

>> Here is an example:
>>
>> \defchunk{This is a name}
>> Some text /BTEX\chunk{Other stuff}/ETEX goes here.
>> And here.
>> \stopchunk
>>
>> \defchunk{Other stuff}
>> Something else \stopchunk
>>
>> \defchunk{This is a name}
>> Some other stuff.
>> \stopchunk
>>
>> Now, at the end, let's say that each of these appears in sections 1, 2,
>> and 3, respectively.  The output I am looking for, in Unicode, might
>> look like this:
>>
>> ⟨This is a name 1⟩≡
>>   Some text ⟨Other stuff 2⟩ goes here.
>>   And here.
>> ──
>>
>> ⟨Other stuff 2⟩≡
>>   Something else
>> ──
>> This code is used in section 1.
>>
>> ⟨This is a name 1⟩≡
>>   Some other stuff.
>> ──
>>
>> So here, there are really three things that I want to achieve.  I need
>> to be able to get a list of all of the sections wherein a given chunk
>> is referenced using \chunk, a list of all the sections where a section
>> of the same name is defined, and finally, be able to tell in which
>> section the first occurrence of a chunk is defined.

> Have you had a look at the reference manual? Chapter 13 on descriptions
> will show you how to define something like
>
> \definedescription[chunk][location=serried,titlestyle=\bf]
>
> which you then should be able to use as
>
> \chunk[name1]{This is a name}
>
> For referencing (chapter 12), use \in for pointing to the relevant
> section, \at[name1] for a page number.
>
> \definedescription[chunk]
> \starttext \input tufte \section[s1]{One section}
> \chunk[c1]{A chunk}
> \input ward \par \input tufte \startchunk[c2]{A long chunk}
> \input ward \input tufte \stopchunk In section \in[s1] on page \at[c1],
> we defined the first chunk.
> \stoptext
>
> You can style this with \setupdesription[chunk][...=...] or directly in
> the definition.

So, basic cross-referencing with named labels is easy enough to figure
out, but I do not know how to go from this to the automatic referencing
that I want.  Specifically, in my above example, I do not want to
explicitly list any labels.  Moreover, that example contains two chunks
with the same name “This is a name.” I want to somehow be able to talk
about all of the chunks that have the same name.  So, if I have three
chunks of that name, I want to be able to say \refs{This is a name} or
something like that, and end up with the set of cross-references to each
of those chunks.

If I gave each specific instance a specific label that was unique and
then manually remembered which labels go together, then I could do the
cross-references.  I do not want to have to do this manually though, and
I want my macros to automatically remember and generate three things:

1) A mapping from a chunk name to the set of references where that chunk
is defined; a single chunk name may be defined in multiple places, and I
want to be able to talk about all of those references as a group.

2) A mapping from a chunk name to the first reference in the above set,
that is, the start of the chunk definitions.

3) A set of references pointing to where a given chunk name is used as a
reference. These are links pointing in the opposite direction.  A
\chunkref{This is a name} should create a reference to which I point in
the first definition of the chunk “This is a name.” The above example
shows this, where I use chunk 2 in chunk 1, so the reference to chunk 1
shows up in the cross-references under chunk 2.

The problem I am having is understanding how to map multiple references
onto a single label, for instance, or some other key, while still
retaining the ability to say what the first use of that label is.

Does this make sense? I hope this clarifies things.  I am trying to get
the cross-referencing behavior that one gets from systems like CWEB or
WEB.  In those systems, a preprocessor extracts the links and generates
them before outputting the TeX code.

--
Aaron W. Hsu | arcfide at sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.