# Manual Labelling and Cross Referencing in mdframed

Question is asked after the MWE.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\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}


Without the command \manuallabel command after ProblemSpecBox environment, everything works fine. However, if I include \manuallabel which is later reference it with \ref{ProblemO} as answered in this question Manually set reference values I get the following errors:

! Argument of \manuallabel has an extra }.

! Paragraph ended before \manuallabel was complete.

I would appreciate it if someone can help me solve this problem.

• Note that the title and the label should not be the same in general as given in the first answer below by @jpellis. – saintdoe Feb 19 '16 at 2:06

Instead of defining a new \label command, you can just amend the label name within the environment declaration.

Also, you're not using the \newenvironment command properly, nor as you using this new environment properly because you placed \manuallabel between the start of the environment and its compulsory argument.

Here's the code with these few issues fixed:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\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}


As requested by @evansdoe, the title and reference name need to be different which the above answer does not allow. The following code functions in the same way as the above but it also allows optional argument in the environment declaration which will customize the reference label. If no custom reference label is specified, the title is used.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\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}


• Why did you make the title and the label to have the same name? Because in my question the title read as Problem O for $\mathbb{C}$ and not Problem O. However when referencing this I will only need Problem O and not Problem O for $\mathbb{C}$ which is what your answer does. (Please see drive.google.com/open?id=0B_EVcKlnBFPcY1NWSnVaMEp3S0k) But this is not what I specifically want. – saintdoe Feb 19 '16 at 1:56
• I just amended the answer to allow an optional argument in the environment declaration which can customize the reference label. If the optional argument is omitted, the reference label is the same as the title. – JP-Ellis Feb 19 '16 at 2:24

With a little modification of @jpellis answer this solves the problem.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,amsfonts}
\usepackage[framemethod=tikz]{mdframed}
\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}


Note that in the answer of @jpellis the title and the reference are the same as in

However, the correct answer produced by the above LaTeX code gives