动态引用内容

动态引用内容

我正在尝试编辑一个包含许多共同部分的文档,这些共同部分仅取决于某些值。
这些值会针对每个组件进行覆盖,但在某个时候,我会引用一个子部分,其名称取决于计数器。
当我处于该循环中时,一切都很好,但如果我必须在循环后读取它,我总是会读取最后一个计数器值(您可能会说这是正常的),但我需要从添加它的那一刻起就获得该值。

为了给出一个想法,下面是我的文档的一小部分,其中包含我想要做的事情:

主文件
\documentclass[10pt,paper=a4,titlepage,twoside=true,openany,]{scrbook}
\usepackage{forloop}            
\usepackage{hyperref}               
\usepackage{lipsum}             
\usepackage{listofitems}        
\usepackage{ninecolors}         
\usepackage{scrhack}            
\usepackage{tabularray}

\UseTblrLibrary{diagbox, functional, varwidth}

\newcounter{CounterSeperator}
\newcounter{CounterCurrentDevElement}

\newcommand{\FileBuffer}{TabulArrayBuffer.tmp}

\setsepchar{\\/;}
\readlist*\DevList{
  Device full name A; DevShortNameA; Device description A\\
  Device full name B; DevShortNameB; Device description B\\
}

\newcommand{\BufferSectionDesc}{}
\newcommand{\CriticKA}{\colorbox{red4}{\textcolor{white}{\textbf{K1}}}}
\newcommand{\CriticKB}{\colorbox{brown4}{\textcolor{white}{\textbf{K2}}}}
\newcommand{\CriticKC}{\colorbox{violet4}{\textcolor{white}{\textbf{K3}}}}
\newcommand{\DefineSectionBuffer}[3] {
  \ifthenelse{#2 = 1}{\renewcommand{#3}{#1 - \CriticKA} }{\renewcommand{#3}{#1}}
  \ifthenelse{#2 = 2}{\renewcommand{#3}{#1 - \CriticKB} }{}
  \ifthenelse{#2 = 3}{\renewcommand{#3}{#1 - \CriticKC} }{}
}

\begin{document}

  \newwrite\FileInstance
  \immediate\openout\FileInstance=\FileBuffer

  \chapter{This is my chapter}
    \label{chap:MyNewChapter}
    \input{./tex/MyNewChapter}

  \immediate\closeout\FileInstance

  \chapter{Summary}
    \label{chap:Summary}
    \begin{longtblr}[evaluate=\fileInput,]{
        colspec = XXc,
      }
      \fileInput{\FileBuffer}
    \end{longtblr}

\end{document}
MyNewChapter.tex 文件
\section{One of my sections}
  \label{sec:MyNewChapter_MySection}

  \forloop[1]{CounterSeperator}{1}{\value{CounterSeperator} < \listlen\DevList[]\par} {
    \input{./tex/DevDescription}
  }
DevDescription.tex 文件
\stepcounter{CounterCurrentDevElement}
\DefineSectionBuffer{Component \# \arabic{CounterCurrentDevElement}}{\arabic{CounterCurrentDevElement}}{\BufferSectionDesc}
\subsection{\BufferSectionDesc}
  \label{sec:MyNewChapter_MySection_\DevList[\value{CounterSeperator}, 2]_Componenent\arabic{CounterCurrentDevElement}}
  \lipsum[1-2]
  \immediate\write\FileInstance{\arabic{CounterCurrentDevElement} \unexpanded{&} {\nameref{sec:MyNewChapter_MySection_\DevList[\value{CounterSeperator}, 2]_Componenent\arabic{CounterCurrentDevElement}}} \unexpanded{&} {\ref{sec:MyNewChapter_MySection_\DevList[\value{CounterSeperator}, 2]_Componenent\arabic{CounterCurrentDevElement}}} \unexpanded{\\}}

因此,在 DevDescription.tex 中,一切正常,问题出在添加表时。我有以下行为:

  • 参考链接正确(均为\参考\名称引用
  • \参考命令给了我正确的部分值
  • \名称引用命令总是给我以下文本“组件#3 - K3”

我希望我已经说得足够清楚了,并且有人能够帮助我。


编辑:
以下是我从这个例子中得到的表格的一个小截图 错误的参考描述

答案1

那里的问题之一是名称引用扩展的时间。默认情况下,由(在您的 MWE 中,由 加载)gettitlesstring使用的 不会在存储标签时扩展标题。因此,在您的 MWE 中,您会在文件中找到以下标签:namerefhyperref.aux

\newlabel{sec:MyNewChapter_MySection_DevShortNameA_Componenent1}{{1.1.1}{1}{\BufferSectionDesc }{subsection.1.1.1}{}}
\newlabel{sec:MyNewChapter_MySection_DevShortNameB_Componenent2}{{1.1.2}{1}{\BufferSectionDesc }{subsection.1.1.2}{}}

如您所见,\BufferSectionDesc未展开地存储在标签中。因此,当您调用\nameref以填充“摘要”时,它将检索当时的值\BufferSectionDesc,这是它在此之前获取的最后一个值。

但是你可以要求gettitlestring扩展名称引用的内容:

\GetTitleStringSetup{expand}

hyperref已加载后)。

这样,你会在.aux文件中找到:

\newlabel{sec:MyNewChapter_MySection_DevShortNameA_Componenent1}{{1.1.1}{1}{Component \# 1 - \colorbox {red4}{\textcolor {white}{\protect \textbf  {K1}}}}{subsection.1.1.1}{}}
\newlabel{sec:MyNewChapter_MySection_DevShortNameB_Componenent2}{{1.1.2}{1}{Component \# 2 - \colorbox {brown4}{\textcolor {white}{\protect \textbf  {K2}}}}{subsection.1.1.2}{}}

您的“摘要”如下:

在此处输入图片描述

相关内容