打印 mdframed 之外的唯一计数器的值

打印 mdframed 之外的唯一计数器的值

我已经为一个唯一的计数器定义了命令,如果计数器唯一名称未在 mdframed 内定义,我可以检索该值,这可能是什么问题,如果计数器名称在 mdframed 内定义,我似乎无法检索该值?

\documentclass{article}
\usepackage{uniquecounter}
\usepackage{mdframed}
\UniqueCounterNew{answers}
\pagestyle{plain}
\makeatletter

\newcommand*{\DefNewAnswersName}[2]{%
  % #1 is unique counter value
  % #2 is name of anchor
  \@namedef{answers@#2}{a#1}%
}
\newcommand*{\NewAnswersName}[1]{%
  \UniqueCounterCall{answers}\DefNewAnswersName{#1}%
}
\newcommand*{\PrintAnswersName}[1]{%
  \@nameuse{answers@#1}%
}
\makeatother


\begin{document}
 \NewAnswersName{first}%
 \NewAnswersName{second}%
\begin{mdframed}
%inside md framed environment it has problems
  \NewAnswersName{third}%
\end{mdframed}
 \noindent
  first: \PrintAnswersName{first}\\%
  second: \PrintAnswersName{second}\\
  third: \PrintAnswersName{third}
\end{document}

答案1

\@namedef只要它不在组内就可以工作,如果在环境\@namedef中应用,则后者为真mdframed(与任何环境一样),在这种情况下,它\@namedef仅在该环境内保存一个值,在外部它是空的,但仍然定义(在外部它会停止并显示错误消息)

\global一个解决方案是在 之前添加一个\@namedef

\documentclass{article}
\usepackage{uniquecounter}
\usepackage{mdframed}
\UniqueCounterNew{answers}
\pagestyle{plain}
\makeatletter

\newcommand*{\DefNewAnswersName}[2]{%
  % #1 is unique counter value
  % #2 is name of anchor
  \global\@namedef{answers@#2}{a#1}%
}

\newcommand*{\NewAnswersName}[1]{%
  \UniqueCounterCall{answers}\DefNewAnswersName{#1}%
}

\newcommand*{\PrintAnswersName}[1]{%
  \@nameuse{answers@#1}%
}

%%%% Explicitly 'wrong' commands, that do not what is expected, because
%%%% of the \begingroup ... \endgroup

\newcommand*{\GroupedDefNewAnswersName}[2]{%
  % #1 is unique counter value
  % #2 is name of anchor
\begingroup
 \@namedef{answers@#2}{a#1}%
\endgroup
}



\newcommand*{\GroupedNewAnswersName}[1]{%
  \UniqueCounterCall{answers}\GroupedDefNewAnswersName{#1}%
}


\makeatother


\begin{document}
 \NewAnswersName{first}%
 \NewAnswersName{second}%
\begin{mdframed}
%inside md framed environment it has problems
  \NewAnswersName{third}%
\end{mdframed}

 \GroupedNewAnswersName{fourth}%
 \GroupedNewAnswersName{fifth}%


 \noindent
  first: \PrintAnswersName{first}\\%
  second: \PrintAnswersName{second}\\
  third: \PrintAnswersName{third} \\
  fourth: \PrintAnswersName{fourth} \\
  fifth: \PrintAnswersName{fifth}
\end{document}

我介绍了两个应用分组的命令,它们显示分组提供空\@namedef值。不要使用它们,因为它们在逻辑上是错误的。

在此处输入图片描述

评论快照中的断帧mdframed只是 pdfviewer 的问题,而不是 LaTeX 的问题。

相关内容