参考计数器

参考计数器

这当然是一个愚蠢的问题。我有一个 newcounter 定义,即

\newcounter{sharc}

如何使用 label/ref 之类的命令调用该计数器的特定值?

示例

blabla  `\thesharc`  blala
blabla  `\thesharc`  blabla
blabla  `\thesharc`  blabla <- I want to refer to this value
blabla  `\thesharc`  blabla

答案1

“引用一个值”是什么意思?

您是否希望仅获取阿拉伯数字的简单计数器值,即使所讨论的计数器从属于其他计数器,因此\the<counter>定义为在打印值时添加任何前缀并应用任何格式?

如果使用 hyperref 包,如何自动创建超链接?

是因为它可能。

为了仅获取最后一个计数器的阿拉伯数字纯值,该计数器通过\refstepcounterHeiko Oberdiek 的参考值包可能会引起人们的兴趣。
(在 LaTeX 的几乎所有文档类的分段命令中,\refstepcounter都用于步进计数器。这是因为\refstepcounter宏使计数器的值可用于\label..\ref机制。)

\the<counter>为了在没有超链接的情况下获得扩展,超链接捆绑/包正在使用中,Heiko Oberdiek 的引用计数包可能会引起你的兴趣。

(下面的例子需要至少编译两次/需要至少两次 LaTeX 运行,其间不删除辅助文件。)

\documentclass[a4paper]{article}

% --- Layout of MWE.Has nothing to do with
%     referencing-techniques.
\setlength\textwidth{\paperwidth}
\setlength\oddsidemargin{1.5cm}
\setlength\marginparwidth{\oddsidemargin}
\addtolength\marginparwidth{-2\marginparsep}%
\setlength\topmargin{\oddsidemargin}
\addtolength\textwidth{-1.5\oddsidemargin}
\addtolength\oddsidemargin{-1in}
\addtolength\oddsidemargin{-\hoffset}
\setlength\evensidemargin{\oddsidemargin}
\setlength\textheight\paperheight
\setlength\footnotesep{.333\topmargin}
\addtolength\textheight{-2\topmargin}
\addtolength\topmargin{-1in}
\addtolength\topmargin{-\voffset}
\setlength\headheight{0ex}
\setlength\headsep{0ex}
\pagestyle{empty}
\parindent=0ex
\parskip=.85\ht\strutbox
\topskip=0ex
% --- Layout done. ---

\usepackage{hyperref}
\usepackage[counter,user]{zref}
\usepackage{refcount}
\makeatletter
%% This will establish a new zref-property holding just the
%% plain value of the counter that was \refstep'ped as the
%% last one in arabic numerals:
%% (\refstepcounter is used internally by all the 
%%   sectioning-commands of LaTeX-documentclasses.)
\zref@newprop{MyPlainCntValue}{\the\value{\zref@getcurrent{counter}}}%
%
%% This will place both a normal label and a zref-label.
%% The zref-label will store the value of the new property.
%% Both kinds of labels are needed as zref does not yet
%% have hyperref-features.
\newcommand\Mylabel[1]{%
  \zref@labelbyprops{#1}{MyPlainCntValue}%
  \label{#1}%
}%
%
%% This is used for referencing saved MyPlainCntValue-property-values 
%% of zref-labels:
\newcommand\MyPlainCntValueRef[1]{%
   \zref[MyPlainCntValue]{#1}%
}%
\makeatother

\newcounter{sharc}[section]
\renewcommand\thesharc{\thesection.\arabic{sharc}}

\begin{document}

\section{Within the preamble define the label-placement- and
         referencing-macros for referencing the plain values
         of counters in arabic numerals.}
\begin{verbatim}
\usepackage{hyperref}
\usepackage[counter,user]{zref}
\usepackage{refcount}
\makeatletter
%% This will establish a new zref-property holding just the
%% plain value of the counter that was \refstep'ped as the
%% last one in arabic numerals:
%% (\refstepcounter is used internally by all the 
%%   sectioning-commands of LaTeX-documentclasses.)
\zref@newprop{MyPlainCntValue}{\the\value{\zref@getcurrent{counter}}}%
%
%% This will place both a normal label and a zref-label.
%% The zref-label will store the value of the new property.
%% Both kinds of labels are needed as zref does not yet
%% have hyperref-features.
\newcommand\Mylabel[1]{%
  \zref@labelbyprops{#1}{MyPlainCntValue}%
  \label{#1}%
}%
%
%% This is used for referencing saved MyPlainCntValue-property-values 
%% of zref-labels:
\newcommand\MyPlainCntValueRef[1]{%
   \zref[MyPlainCntValue]{#1}%
}%
\makeatother
\end{verbatim}

\section{Define the \texorpdfstring\texttt\empty{sharc}-counter 
         and just for fun bind it to the 
         \texorpdfstring\texttt\empty{section}-counter}
\begin{verbatim}
\newcounter{sharc}[section]
\renewcommand\thesharc{\thesection.\arabic{sharc}}
\end{verbatim}

\section{Within the document-environment step the
         \texorpdfstring\texttt\empty{sharc}-counter via
         \texorpdfstring{\protect\newline}\empty
         \texorpdfstring{\texttt{\string\refstepcounter}}%
         {\textbackslash refstepcounter}, 
         and via 
         \texorpdfstring{\texttt{\string\Mylabel}}%
         {\textbackslash Mylabel}
         place the referencing-labels.}%
(\verb|\refstepcounter| is used internally by all the 
sectioning-commands of \LaTeX-documentclasses.)

\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla

\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla

\verb|blabla \refstepcounter{sharc}\thesharc\Mylabel{INeedReferenceToThis} blabla|:\\
blabla \refstepcounter{sharc}\thesharc\Mylabel{INeedReferenceToThis} blabla 
\\\emph{(This is to be referenced/linked. Therefore the}
\verb|\Mylabel|\emph{-command was used for placing both a
normal label and a} \textsf{\textbf{zref}}\emph{-label.
For testing the links view the resulting pdf at a
magnification/zoom-factor where scrolling within the window
where the pdf is displayed is needed.)}

\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla

\newpage
\section{Referencing the label}%

\verb|\ref{INeedReferenceToThis}| 

yields:

\ref{INeedReferenceToThis}

{(\verb|\ref| comes from the \LaTeXe-kernel but may be 
redefined by some package. This produces the number with all 
prefixes from other counters. When using the 
\textsf{\textbf{hyperref}}-bundle, this also does
produce a hyperlink to the anchor automatically produced by 
\verb|\refstepcounter|.)}

\hrulefill

\verb|\makeatletter|\\
\verb|\getrefbykeydefault{INeedReferenceToThis}%|\\
\verb|                   {}%|\\
\verb|                   {\refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%|\\
\verb|\makeatother|

yields:

\makeatletter
\getrefbykeydefault{INeedReferenceToThis}%
                   {}%
                   {\refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%
\makeatother

{(\verb|\getrefbykeydefault| comes from the 
\textsf{\textbf{refcount}}-package. This produces the number 
with all prefixes from other counters. Even when using the
\textsf{\textbf{hyperref}}-bundle, this does not produce a
hyperlink to the anchor automatically produced by 
\verb|\refstepcounter|.)}

\hrulefill

\verb|\MyPlainCntValueRef{INeedReferenceToThis}|

yields:

\MyPlainCntValueRef{INeedReferenceToThis}

{(\verb|\MyPlainCntValueRef| is defined by
means of macros from the \textsf{\textbf{zref}}-package.
This produces just the plain number in arabic numerals without
any prefixes from other counters. Even when using the 
\textsf{\textbf{hyperref}}-bundle, this does not produce
a hyperlink to the anchor automatically produced by 
\verb|\refstepcounter|.)}

\hrulefill

\verb|\hyperref[INeedReferenceToThis]{\MyPlainCntValueRef{INeedReferenceToThis}}| 

yields:

\hyperref[INeedReferenceToThis]{\MyPlainCntValueRef{INeedReferenceToThis}}

{\sloppy(\verb|\hyperref| comes from the
\textsf{\textbf{hyperref}}-bundle. Therefore this only works
when the \textsf{\textbf{hyperref}}-bundle is loaded.
\verb|\MyPlainCntValueRef| is defined by means of macros 
from the \textsf{\textbf{zref}}-package. This produces just
the plain number in arabic numerals without any prefixes from
other counters. This does also produce a hyperlink to the
anchor automatically produced by 
\verb|\refstepcounter|.)

}
\hrulefill

In expansion contexts you can use \textsf{\textbf{zref}}'s \verb|\zref@extractdefault|
for obtaining just the plain number - here the plain number delivered by 
\verb|\zref@extractdefault| will be used by \verb|\romannumeral| for delivering
roman numerals instead of arabic numerals:

\verb|\makeatletter|\\
\verb|\expandafter\@firstofone|\\
\verb|\expandafter{%|\\
\verb|  \romannumeral|\\
\verb|  \zref@extractdefault{INeedReferenceToThis}%|\\
\verb|                      {MyPlainCntValue}%|\\
\verb|                      {0 \zref@refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%|\\
\verb|}%|\\
\verb|\makeatother|

yields:

\makeatletter
\expandafter\@firstofone
\expandafter{%
  \romannumeral
  \zref@extractdefault{INeedReferenceToThis}%
                      {MyPlainCntValue}%
                      {0 \zref@refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%
}%
\makeatother

\end{document}

结果输出的第 1 页 结果输出的第 2 页

答案2

你走在正确的道路上,

\newcounter{sharc}

是正确的开始。然后你可以用以下命令将其设置为 val(数字,而不是计数器):

\setcounter{sharc}{val}

如果要将其设置为计数器cnt,可以使用:

\setcounter{sharc}{\value{cnt}}

为了增加它,请使用:

\stepcounter{sharc}

为了引用它,请使用,正如 NBur 和 Andrew 在一些评论中提到的,

\refstepcounter{sharc}
\label{labelname}

但是,这不会显示计数器。因此,您需要使用以下方法之一来显示它:

\arabic{sharc}
\alph{sharc}
\Alph{sharc}
\roman{sharc}
\Roman{sharc}

所有这些信息你都可以在维基百科中找到这里

综合起来:

结果

\documentclass{article}
\newcounter{sharc}
\setcounter{sharc}{2} % set initial value
\renewcommand{\thesharc}{\roman{sharc}} % set references to \roman
\newcommand{\sharcref}[1]{[\ref{#1}]} % reference in brackets

\begin{document}
    initial value: \arabic{sharc} (\Roman{sharc})

    \stepcounter{sharc} % add one
    next value: \arabic{sharc} (\alph{sharc})

    \refstepcounter{sharc} % add one and reference this one
    labeled value: \arabic{sharc} [\roman{sharc}]
    \label{l:sharc}

    this is the label: \ref{l:sharc}\par
    or with the defined command: \sharcref{l:sharc}
\end{document}

相关内容