為什麼我的 expl3 \prop_item 會增加我的 \stepcounter?

為什麼我的 expl3 \prop_item 會增加我的 \stepcounter?

我正在嘗試做什麼

我想建立一個自訂交叉引用標籤,讀者可以點擊該標籤並查看我正在引用的文字行。但是,我希望標籤的編號能夠自動進行,這樣我就可以從文字中新增和減去項目,而無需手動重新編號。

為了實現這一目標,我目前正在拼湊一個expl3 \prop_行為類似於 Pythondict或類似 C 的語言map,這很常見。我在命令中設定並調用自訂標籤並進行測試。但是,似乎我\prop_item以一種意想不到的方式增加了計數器。

微量元素

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

在此輸入影像描述

使輸入變數不可變

看來我的格式化行沒有像我期望的那樣\dictappend{#1}{A*\thearticle***}傳遞不可變的值。\the難道不是這樣的嗎?

我不想要或不起作用的東西

  • 我嘗試過使用來自這個帖子,但我嘗試過的配置沒有幫助。這很奇怪,因為 @egreg 的答案似乎在我將其應用於 時應該有效\dictappend{#1}{\edef\newstring{AJ\thearticlejournal}},但這只會使我的字串消失。
  • 使 2 參數標籤定義為 la這個問題這裡是不可接受的。我希望能夠將新條目插入大列表中,而無需手動重新編號。我的用例需要自我參考。
  • 由於我有多個計數器,我想引用\myref,使用\the來存取計數沒有達到我需要的效果正如這裡引用的。單獨使用時\the,我能做到的最好的方法就是僅注入計數器值。
  • 同樣,與\refstepcounter類似的工作這個例子只傳回值而不是文字。

詢問

為什麼我的變數在此操作期間會增加?我怎麼能解決這個問題,使它們在定義每個字典條目後不會改變?

答案1

當您將計數器的值儲存在屬性清單中:

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

然後使用\dictappend\countAJ

\dictappend{#1}{AJ\thearticlejournal}

考慮到中的簽名,您正在AJ\thearticlejournal以“無操作”的方式儲存。n\prop_gput:Nnn

\CGet當您在then中使用此值時AJ\thearticlejournal,換句話說,您將在呼叫( 和)\thearticlejournal的位置獲得當前值。\CGet\myref

如果您希望該值是您儲存它時的值,那麼您需要在該點擴展它。您可以透過使用\prop_gput:Nnx而不是\prop_gput:Nnnin來做到這一點\dictappend

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

相關內容