问题是在 MWE 之后提出的。
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[colorlinks=true, linktocpage=false]{hyperref}
\hypersetup{
citecolor = {blue}
}
\makeatletter
\newcommand{\manuallabel}[2]{\def\@currentlabel{#2}\label{#1}}
\makeatother
\newenvironment{ProblemSpecBox}[2]
{%
\begin{mdframed}[innerlinewidth=0.5pt,%
innerleftmargin=10pt, innerrightmargin=10pt,%
innertopmargin = 10pt, innerbottommargin=10pt,%
skipabove=\dimexpr\topsep+\ht\strutbox\relax,%
roundcorner=5pt, frametitle={#1},%
frametitlerule=true, frametitlerulewidth=1pt]
{#2}
\end{mdframed}
}
\begin{document}
This is a test example
\begin{ProblemSpecBox} \manuallabel{prob:ProblemO}{Problem O} % I think this is where the problem is.
{
Problem O for $\mathbb{C}$
}
{
\emph{Given} $a, b \in \mathbb{C}$; \emph{Find} $n \in \mathbb{Z}$ such that
\[
|a|^{n} = |b|
\]
}
\end{ProblemSpecBox}
\ref{prob:ProblemO} % This must display Problem O i.e., the second argument of \manuallabel which will be linked to it's source by hyperref.
\end{document}
如果没有命令\manuallabel
命令ProblemSpecBox
环境,一切都会正常工作。但是,如果我在\manuallabel
后面引用它,则可以使用\ref{ProblemO}
此问题中的答案手动设置参考值我收到以下错误:
! \manuallabel 的参数有一个额外的 }。
! 段落在 \manuallabel 完成之前结束。
如果有人能帮助我解决这个问题我将不胜感激。
答案1
您无需定义新\label
命令,只需修改环境声明中的标签名称即可。
此外,您没有\newenvironment
正确使用命令,也没有正确使用这个新环境,因为您将其放置\manuallabel
在环境的开始和其强制参数之间。
以下是修复了以下几个问题的代码:
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[colorlinks=true, linktocpage=false]{hyperref}
\hypersetup{
citecolor = {blue}
}
\makeatletter
\newenvironment{ProblemSpecBox}[1]{
\protected@edef\@currentlabelname{#1}
\protected@edef\@currentlabel{#1}
\begin{mdframed}[
innerlinewidth=0.5pt,
innerleftmargin=10pt,
innerrightmargin=10pt,
innertopmargin = 10pt,
innerbottommargin=10pt,
skipabove=\dimexpr\topsep+\ht\strutbox\relax,
roundcorner=5pt,
frametitle={#1},
frametitlerule=true,
frametitlerulewidth=1pt]
}{
\end{mdframed}
}
\makeatother
\begin{document}
This is a test example:
\begin{ProblemSpecBox}{Problem O}
\label{prob:ProblemO}
\emph{Given} $a, b \in \mathbb{C}$; \emph{Find} $n \in \mathbb{Z}$ such that
\[
|a|^{n} = |b|
\]
\end{ProblemSpecBox}
Reference: \ref{prob:ProblemO}
\end{document}
附录
根据@evansdoe 的要求,标题和参考名称必须不同,而上面的答案不允许。以下代码的功能与上面的相同,但它还允许在环境声明中使用可选参数来自定义参考标签。如果未指定自定义参考标签,则使用标题。
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[colorlinks=true, linktocpage=false]{hyperref}
\hypersetup{
citecolor = {blue}
}
\makeatletter
\def\ProblemSpecBox{
\@ifnextchar[\ProblemSpecBox@opt{\ProblemSpecBox@noopt}}
\def\ProblemSpecBox@opt[#1]#2{
\protected@edef\@currentlabelname{#1}
\protected@edef\@currentlabel{#1}
\begin{mdframed}[
innerlinewidth=0.5pt,
innerleftmargin=10pt,
innerrightmargin=10pt,
innertopmargin = 10pt,
innerbottommargin=10pt,
skipabove=\dimexpr\topsep+\ht\strutbox\relax,
roundcorner=5pt,
frametitle={#2},
frametitlerule=true,
frametitlerulewidth=1pt]
}
\def\ProblemSpecBox@noopt#1{
\ProblemSpecBox@opt[#1]{#1}
}
\def\endProblemSpecBox{
\end{mdframed}
}
\makeatother
\begin{document}
This is a test example:
\begin{ProblemSpecBox}[Problem 0]{Problem 0 in \(\mathbb{C}\)}
\label{prob:ProblemO}
\emph{Given} $a, b \in \mathbb{C}$; \emph{Find} $n \in \mathbb{Z}$ such that
\[
|a|^{n} = |b|
\]
\end{ProblemSpecBox}
Reference: \ref{prob:ProblemO}
\end{document}
答案2
对@jpellis 的答案稍加修改就可以解决问题。
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[colorlinks=true, linktocpage=false]{hyperref}
\hypersetup{
citecolor = {blue}
}
\makeatletter
\newenvironment{ProblemSpecBox}[2]{ % Takes two arguments and not one
\protected@edef\@currentlabelname{#2}
\protected@edef\@currentlabel{#2}
\begin{mdframed}[
innerlinewidth=0.5pt,
innerleftmargin=10pt,
innerrightmargin=10pt,
innertopmargin = 10pt,
innerbottommargin=10pt,
skipabove=\dimexpr\topsep+\ht\strutbox\relax,
roundcorner=5pt,
frametitle={#1},
frametitlerule=true,
frametitlerulewidth=1pt]
}{
\end{mdframed}
}
\makeatother
\begin{document}
This is a test example:
\begin{ProblemSpecBox}{Problem O for \mathbb{C}}{Problem O}
\label{prob:ProblemO}
\emph{Given} $a, b \in \mathbb{C}$; \emph{Find} $n \in \mathbb{Z}$ such that
\[
|a|^{n} = |b|
\]
\end{ProblemSpecBox}
Reference: \ref{prob:ProblemO}
\end{document}
请注意,@jpellis 的答案中的标题和参考与
然而,上述 LaTeX 代码产生的正确答案是
这回答了这个问题。