Referencing a theorem-like environment by its [name]
I am using ntheorem to typeset a set of conditions. In my preamble I have:
\theoremstyle{empty}
\newtheorem{Condtion}{Condtion}
When I want to typeset a condition, I write:
\begin{Condtion}[name]
\label{cnd:nm}
foo foo foo
\end{Condition}
The name appears boldface on the same line as the start of the text of the condition, with no number or anything. Perfect.
What I want to do now is refer to the condition by some variant of the \ref
command,
\ref calls the number [which is not displayed anywhere else]
\thref writes "Condition n" for the nth condition
\nameref writes the name of the SECTION of the label.
a zref solution was suggested here, but seems unsatisfactory and unwieldly.
Any suggestions on a simple way to do this? (Even a simpler zref solution would be nice) At the moment I've just def开发者_JS百科ined a \newcommand
for each condition and use that rather than citing the condition itself. This is semantically opaque and just unsatisfying...
(edit: I emailed one author of ntheorem, Wolfgang May, and he explained that there isn't really a way to do this within ntheorem, since the [name] option isn't recorded.)
(edit: This isn't a dupe as suggested in the comment, since I'm interested in referencing an environment by its optional name command, not referencing the section/chapter it sits in.)
I think the following may do what you want.
\makeatletter
\def\namedlabel#1#2{\begingroup
\def\@currentlabel{#2}%
\label{#1}\endgroup
}
\makeatother
Then you use it as
\begin{theorem}
\namedlabel{thm:seamus}{Seamus' Theorem}
Here is Seamus' Theorem.
\end{theorem}
Here I reference~\ref{thm:seamus}.
Unfortunately, it can then only be referenced by name, though I suppose you could use a normal \label
as well (with a different key of course).
For the amsthm
environments you can use
\makeatletter
\let\@old@begintheorem=\@begintheorem
\def\@begintheorem#1#2[#3]{%
\gdef\@thm@name{#3}%
\@old@begintheorem{#1}{#2}[#3]%
}
\def\namedthmlabel#1{\begingroup
\edef\@currentlabel{\@thm@name}%
\label{#1}\endgroup
}
\makeatother
nameref doesn't work: it references the title of the SECTION the theorem-like environment appears in.
This is a nameref bug that was already fixed a while ago: http://web.archiveorange.com/archive/v/9uUx5EuqoCGynIvx3om7#lY2MJxvge2oMgOi
Unfortunately some Linux distros like Debian/Ubuntu ship with horribly old versions of the packages (Ubuntu 11.04 still ships TeX Live 2009 although the latest version is TeX Live 2011). If you're using such a Linux distro stop using the Tex Live package in the distro, and install TeX Live directly from here: http://www.tug.org/texlive/ You can then update packages using tlmgr (a really cool tool that doesn't ship with Debian/Ubuntu).
you may want to check out the nameref
package, which is distributed with hyperref
. There is a section in the nameref
documentation about referencing "stuff".
More on referencing can be found in the TeX FAQ item Referring to things by their name.
I thought others might find this helpful. Even though I had an updated hyperref
package installed, I had to explicitly call \usepackage{nameref}
after \usepackage{hyperref}
in order to get the correct behavior from \nameref
. Without the explicit call to \usepackage{nameref}
, \nameref
worked, but exhibited the bug discussed in this thread.
Update: this workaround isn't requred for a minimal example (which I checked). I don't know if there's something more complicated going on in my style files, but I'll update this if I find something. This may still help someone running into the same issue I was.
精彩评论