我有一个蕴涵环境impl
,其计数器按预期工作(1、2……)。在后续章节中,我为每个蕴涵制定了一系列假设,并且我希望假设环境hypo
能够识别它所属蕴涵的计数器。到目前为止,我拥有的是:
\documentclass{article}
\usepackage{amsmath}
%% Implications
\newcounter{implcounter}
\newenvironment{impl}{
\refstepcounter{implcounter}
\textbf{Implication \theimplcounter:}
}{\par}
%% Hypotheses
\newcounter{hypcounter}
\newenvironment{hypo}{
\refstepcounter{hypcounter}
\textbf{Hypothesis \thehypcounter:}
}{\par}
\numberwithin{hypcounter}{implcounter}
\begin{document}
This is one implication:
\begin{impl} \label{impl:a}
Hello
\end{impl}
This is another implication:
\begin{impl}
Bye bye
\end{impl}
But then:
\begin{hypo}
Not the numberwithin I would like!
% I would like for this to be implication 1.1
\end{hypo}
\end{document}
我想要一个hypo
环境,其中的数字是 所引用的数字\label{impl:a}
,在本例中为 1。
答案1
尝试这个:
\documentclass{article}
\usepackage{amsmath}
%% Implications
\newcounter{implcounter}
\newenvironment{impl}{
\refstepcounter{implcounter}
\textbf{Implication \theimplcounter:}
}{\par}
%% Hypotheses
\newcounter{hypcounter}
\newenvironment{hypo}[1][\theimplcounter]{
\refstepcounter{hypcounter}
\textbf{Hypothesis #1.\thehypcounter:}
}{\par}
\begin{document}
This is one implication:
\begin{impl} \label{impl:a}
Hello
\end{impl}
This is another implication:
\begin{impl}
Bye bye
\end{impl}
But then:
\begin{hypo}[\ref{impl:a}]
Not the numberwithin I would like!
% I would like for this to be implication 1.1
\end{hypo}
\end{document}
你只需要在你的环境中添加第二个参数(可选)来引用你选择引用的含义...如果你不提供参数,它将引用最后一个 impl
编辑:
你想要的可以这样做:
\documentclass{article}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{refcount}
\usepackage{stringstrings}
\usepackage{xstring}
%% Implications
\newcounter{implcounter}
\newenvironment{impl}{
\refstepcounter{implcounter}
\textbf{Implication \theimplcounter:}
}{\par}
%% Hypotheses
\makeatletter
\newcommand\andrea@test@count[1]{%
\@ifundefined{c@#1}
{% the counter doesn't exist so I define it
\expandafter\newcounter{#1}
}
{% the counter exists and I do nothing
\relax
}%
}
\newenvironment{hypo}[1][\theimplcounter]{
\ifnum #1=\theimplcounter
{ %this is the default case for not given argument
\andrea@test@count{counter\theimplcounter}
\refstepcounter{counter\theimplcounter}
\textbf{Hypothesis \theimplcounter.\csname thecounter\theimplcounter\endcsname:}
}
\else
{ %this is tha case with given argument
\andrea@test@count{counter#1}
\refstepcounter{counter#1}
\textbf{Hypothesis #1.\csname thecounter#1\endcsname:}
}
\fi
}{\par}
\makeatother
\begin{document}
This is one implication:
\begin{impl} \label{impl:a}
Hello this Implication has label \getrefnumber{impl:a}
\end{impl}\quad
This is another implication:
\begin{impl} \label{impl:b}
Hello this Implication has label \getrefnumber{impl:b}
\end{impl}\quad
This is a third implication without label:
\begin{impl}
Bye bye
\end{impl}\quad
And then:\par
\begin{hypo}[\getrefnumber{impl:b}]
This hypothesis is refered to label \getrefnumber{impl:b}.
So I want 2.1
\end{hypo}\quad
\begin{hypo}
This Hypothesis is refered to no label.
So I want it 3.1
\end{hypo}\quad
\begin{hypo}[\getrefnumber{impl:a}]
This hypothesis is refered to label \getrefnumber{impl:a}.
I want it 1.1
\end{hypo}\quad
\begin{hypo}[\getrefnumber{impl:a}]
This hypothesis is refered to label \getrefnumber{impl:a}.
I want 1.2
\end{hypo}\quad
\begin{hypo}
This last hypothesis has no label. So I want it 3.2
\end{hypo}
\end{document}
答案2
这是基于Christian Hupfer 的回答但会尝试遵守使用expl3
代码的准则,并在未定义隐含上下文的情况下发出警告。
它将两个环境重新实现为函数的xparse
包装器expl3
,这些函数遵循标准语法。它还\cs_new_protected_nopar
优先使用\cs_new
,并尽可能尝试使用更高效的选项来处理属性列表。
该实现不再依赖于LaTeX 2e 提供的\label
/\ref
系统。相反,标签可以作为impl
环境的可选参数给出,并且必须作为环境的强制参数给出hypo
。
使用两个属性列表:每种环境类型一个。如果未定义隐含上下文,则输出中将使用零,并将警告和详细信息写入控制台。
据我了解,一个标准是,用户级命令和环境应该使用\NewDocumentEnvironment
etc. 而不是\newenvironment
etc. 来定义,并且这应该是简单解析用户输入并将其传递给低级expl3
函数的最小包装器。
然而,我不确定我是否理解得正确,尤其是因为人们似乎通常不这样做。所以这可能完全是错的。
买者自负
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
% based on ateb Christian Hupfer: https://tex.stackexchange.com/a/375209/
\ExplSyntaxOn
\prop_new:N \g_milo_hypcounter_prop
\prop_new:N \g_milo_implcounter_prop
% these are the counters
\int_new:N \g_milo_hypo_int
\int_new:N \g_milo_impl_int
\msg_new:nnn { milo } { undef }
{
milo ~::~reference~#1~is~unknown.~0~will~be~used~instead.~\msg_line_context:.
}
\cs_new_protected_nopar:Nn \milo_storehypcounter:n {
\prop_gput:Nnx \g_milo_implcounter_prop { #1 } { \int_to_arabic:n { \g_milo_impl_int } }
\prop_gput:Nnn \g_milo_hypcounter_prop { #1 } { 0 }
}
\cs_new_protected_nopar:Nn \milo_extracthypcounter:n {
\prop_get:NnNTF \g_milo_implcounter_prop { #1 } \l_tmpa_tl
{
\l_tmpa_tl
}
{
0
\msg_warning:nnn { milo } { undef } { #1 }
}
.
\prop_get:NnNTF \g_milo_hypcounter_prop { #1 } \l_tmpa_tl
{
\prop_gput:Nnx \g_milo_hypcounter_prop { #1 } { \int_eval:n { \l_tmpa_tl + 1 } }
\prop_item:Nn \g_milo_hypcounter_prop { #1 }
}
{
0
\msg_warning:nnn { milo } { undef } { #1 }
}
}
\NewDocumentEnvironment { impl } { o }
{
\int_gincr:N \g_milo_impl_int
\IfValueT { #1 }
{
\milo_storehypcounter:n { #1 }
}
\textbf{Implication ~ \int_to_arabic:n {\g_milo_impl_int} : }
}{
\par
}
\NewDocumentEnvironment { hypo } { m }
{
\textbf{ Hypothesis ~ \milo_extracthypcounter:n { #1 } }
}{
\par
}
\ExplSyntaxOff
\begin{document}
This is one implication:
\begin{impl}[impl:a]
Hello
\end{impl}
This is another implication:
\begin{impl}[impl:b]
Bye bye
\end{impl}
But then:
\begin{hypo}{impl:a}
Foo a
\end{hypo}
\begin{hypo}{impl:a}
Impl a cont.
\end{hypo}
\begin{hypo}{impl:b}
Foo b
\end{hypo}
\begin{hypo}{impl:a}
Foo a cont.
\end{hypo}
\begin{hypo}{impl:u}
Undefined case.
\end{hypo}
\end{document}
请注意,最后一种情况未定义,因此会产生警告和零:
答案3
以下是使用类似于属性列表的一种方法:
\documentclass{article}
\usepackage{xfp}
%% Implications
\newcounter{implcounter}
\newenvironment{impl}{%
\par\noindent
\refstepcounter{implcounter}%
\textbf{Implication \theimplcounter:}%
}{\par}
%% Hypotheses
\makeatletter
\newenvironment{hypo}[1]{%
\par\noindent
\ifcsname c@impl@#1\endcsname\else
\@namedef{c@impl@#1}{0}% First hypothesis for implication #1
\fi
\begingroup\edef\x{\endgroup
\noexpand\global\noexpand\@namedef{c@impl@#1}{\noexpand\fpeval{\csname c@impl@#1\endcsname+1}}}\x% Increment hypothesis
\textbf{Hypothesis \ref{#1}.\csname c@impl@#1\endcsname:}%
}{\par}
\makeatother
\begin{document}
This is one implication:
\begin{impl} \label{impl:a}
Hello
\end{impl}
This is another implication:
\begin{impl} \label{impl:b}
Bye bye
\end{impl}
But then:
\begin{hypo}{impl:a}
Foo a
\end{hypo}
\begin{hypo}{impl:a}
Impl a cont.
\end{hypo}
\begin{hypo}{impl:b}
Foo b
\end{hypo}
\begin{hypo}{impl:a}
Foo a cont.
\end{hypo}
\end{document}
答案4
出于某种原因,这个问题参考计数器被标记为该帖的重复帖。
您可以在那里阅读:
这当然是一个愚蠢的问题。我有一个 newcounter 定义,即
\newcounter{sharc}
如何使用 label/ref 之类的命令调用该计数器的特定值?
示例
blabla `\thesharc` blala
blabla `\thesharc` blabla
blabla `\thesharc` blabla <- I want to refer to this value
blabla `\thesharc` blabla
在 StackExchange 中查找标记为重复的问题及其答案似乎并不容易。因此,我也在这里发布了我对这个问题的回答:
“引用一个值”是什么意思?
您是否希望仅获取阿拉伯数字的简单计数器值,即使所讨论的计数器从属于其他计数器,因此\the<counter>
定义为在打印值时添加任何前缀并应用任何格式?
如果使用超链接-包裹?
是因为它可能。
为了仅获取最后一个计数器的阿拉伯数字纯值,该计数器通过\refstepcounter
Heiko 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}