2

What I'm trying to do

I want to create a custom cross-referencing tag which a reader can click on and view the line of text I am referencing. However, I want the numbering of the tags to be automated so I can add and subtract items from my text without manually renumbering something.

To achieve this, I am currently kludging together an expl3 \prop_ which behaves similarly to the Python dict or C-like map which are quite common. I set and call my custom tags inside commands and spit out a test. BUT, it seems like my \prop_item increments my counter in a manner I don't expect.

MWE

\documentclass{article}

\usepackage{hyperref} \usepackage{xparse}

% Define a dict-like object where I can store the label and my associated text. \ExplSyntaxOn \prop_new:N \g_prop_dict \NewDocumentCommand{\dictappend}{mm}{% \prop_gput:Nnn\g_prop_dict{#1}{#2} } \NewDocumentCommand{\CGet}{m}{% \prop_item:Nn\g_prop_dict{#1} } \ExplSyntaxOff

% First counter \newcounter{articlejournal} \setcounter{articlejournal}{1} \newcommand{\countAJ}[1]{% \dictappend{#1}{AJ\thearticlejournal} \phantomsection\label{#1}{\textbf{AJ\thearticlejournal}} \stepcounter{articlejournal} }%

% Secound counter \newcounter{articleconference} \setcounter{articleconference}{1} \newcommand{\countAC}[1]{% \dictappend{#1}{AC\thearticleconference} \phantomsection\label{#1}{\textbf{AC\thearticleconference}} \stepcounter{articleconference} }%

% Command to retrieve the value and format it correctly. \newcommand{\myref}[1]{\hyperref[#1]{\textbf{\CGet{#1}}}}

\begin{document} \countAJ{foo} - is test 1

\countAJ{bar} - is test 2

\countAC{baz} - this is a different one

Here I reference \myref{bar} and \myref{foo}, and here I want \myref{baz}.

I expect these to look like AJ2, AJ1, and AC1, respectively. \end{document}

enter image description here

Making Input Variables Immutable

It seems like my \dictappend{#1}{A*\thearticle***} formatted lines are not passing immutable values with \the like I would expect. Is that not how it works?

What I Don't Want or Does not Work

  • I have tried using parts from this post, but no configuration helped that I tried. This is odd, because @egreg 's answer seems like it should work when I apply it to \dictappend{#1}{\edef\newstring{AJ\thearticlejournal}}, yet this just makes my string disappear.
  • Making a 2-argument label definition a la this question is not acceptable here. I want to be able to insert a new entry into a big list without re-numbering things manually. My use-case requires self-reference.
  • Since I have multiple counters I want to reference with \myref, using \the to access the count does not have the effect I need as cited here. Using \the alone, the best I could manage was injection of the counter value only.
  • Similarly, working with \refstepcounter similar to this example only returns the value instead of the text.

The Ask

Why are my variables incrementing during this operation? How can I fix this such that they do not change after each dict entry is defined?

WesH
  • 427
  • 1
    I did not examine this thoroughly, but a hunch is that you actually want to use \prop_gput:Nnx in \dictappend. – gusbrs Mar 14 '23 at 20:56
  • 2
    \dictappend{#1}{A*\thearticle***} just stores A*\thearticle*** you want an x or e variant to expand the argument – David Carlisle Mar 14 '23 at 20:59
  • @gusbrs Your hunch seems correct. Write that up for your magic internet points. – WesH Mar 14 '23 at 21:01
  • @DavidCarlisle I can get the x to work like gusbrs mentioned, but using the nested e in the arg-spec :Nne in the _gput appears to fail. – WesH Mar 14 '23 at 21:09
  • e variant of that one isn't predefined but you can always define such variants, just a representative sample are predefined, more x than e as the e type is newer – David Carlisle Mar 14 '23 at 21:12

1 Answers1

2

When you store the value of your counter in the property list with:

\NewDocumentCommand{\dictappend}{mm}{%
  \prop_gput:Nnn\g_prop_dict{#1}{#2}
}

Then use \dictappend in \countAJ as:

\dictappend{#1}{AJ\thearticlejournal}

You are storing AJ\thearticlejournal with "no manipulation", given the n signature in \prop_gput:Nnn.

When you use this value in \CGet then AJ\thearticlejournal is expanded, in other words, you get the current value for \thearticlejournal in the place you called \CGet (and \myref).

If you want this value to be the one at the point you have stored it, then you need to expand it at that point. Which you can do by using \prop_gput:Nnx instead of \prop_gput:Nnn in \dictappend:

\NewDocumentCommand{\dictappend}{mm}{%
  \prop_gput:Nnx\g_prop_dict{#1}{#2}
}
gusbrs
  • 13,740