# My own ref command

I would like to make my own \ref system.

I wrote \Problem command to denote the serial number and title. I want a new \myref command which yields the number and title by referencing the label. Could you help me?

Here is the MWE.

\documentclass{article}
\newcounter{cnt}
\setcounter{cnt}{1}

\def\Problem#1{
\noindent\textsf{\thecnt. #1}
\stepcounter{cnt}
}
\def\Answer#1{
\noindent\textsf{Ans. \myref{#1}}
}
\def\myref#1{
\ref{#1}
% I don't know.
}

\begin{document}

\section{Problems}

\Problem{Foo}\label{problem1}

\Problem{Bar}\label{problem2}

\section{Answers}

\Answer{problem1}

I want to display \textsf{Ans. 1. Foo}.

\Answer{problem2}

I want to display \textsf{Ans. 2. Bar}.

\end{document}

• Use \refstepcounter instead of \stepcounter to let TeX know that the counter should be referable. Is there a reason for not sticking to the regular \label-\ref-mechanism? – Tobi Sep 3 '18 at 13:10
• also you are missing % at the ends of lines in your macros, but I do not see why you can't use \ref here, \myref is just the same except that it adds space before and after the returned number. – David Carlisle Sep 3 '18 at 13:18
• @Tobi Thank you for teaching me \refstepcounter. I don't know how to use \label - \ref to display two entries. Could you let me know how to print the title by referencing? – P.-S. Park Sep 3 '18 at 13:27
• @DavidCarlisle Sorry for my humble source. :-( – P.-S. Park Sep 3 '18 at 13:27
• Btw, instead of \def to define commands, LaTeX aficionados recommend using \newcommand as this checks to make sure you are not overwriting an existing command. It also has some other nice features; see, for example, tex.stackexchange.com/questions/191266/…. You can also use \NewDocumentCommand and friends from the xparse package. – Andrew Sep 3 '18 at 13:34

## 2 Answers

I suggest using theorems, so \label and \ref work out of the box and don't add unwanted spaces.

\documentclass{article}
\usepackage{amsthm}

\newtheoremstyle{problem}
{0pt}       % ABOVESPACE
{0pt}       % BELOWSPACE
{\upshape}  % BODYFONT
{0pt}       % INDENT (empty value is the same as 0pt)
{\sffamily} % HEADFONT
{.}         % HEADPUNCT
{ }         % HEADSPACE
% CUSTOM-HEAD-SPEC follows
{\thmnumber{#2}. \protect\problemname}
\providecommand{\problemname}{}

\newtheoremstyle{answer}
{0pt}       % ABOVESPACE
{0pt}       % BELOWSPACE
{\upshape}  % BODYFONT
{0pt}       % INDENT (empty value is the same as 0pt)
{\sffamily} % HEADFONT
{.}         % HEADPUNCT
{ }         % HEADSPACE
% CUSTOM-HEAD-SPEC follows
{Ans.\ \protect\answername}
\providecommand{\answername}{}

\theoremstyle{problem}
\newtheorem{innerproblem}{}
\makeatletter
\newenvironment{Problem}[1]
{%
\renewcommand{\problemname}{#1}%
\innerproblem
\edef\@currentlabel{\theinnerproblem.\ \unexpanded{#1}}%
}
{\endinnerproblem}
\makeatother

\theoremstyle{answer}
\newtheorem*{inneranswer}{}
\newenvironment{Answer}[1]
{\renewcommand{\answername}{\ref{#1}}\inneranswer}
{\endinneranswer}

\begin{document}

\section{Problems}

\begin{Problem}{Foo}\label{problem1}
Compute $1+1$.
\end{Problem}

\begin{Problem}{Bar}\label{problem2}
Compute $0+0$.
\end{Problem}

\section{Answers}

\begin{Answer}{problem1}
$1+1=2$.
\end{Answer}

\begin{Answer}{problem2}
$0+0=0$.
\end{Answer}

\end{document}


I found an easier(?) way through an answer for labeling a word.

\makeatletter
\def\Problem#1#2{
\stepcounter{cnt}
\noindent\textbf{\thecnt.} \textsf{#1}%
\def\@currentlabel{\thecnt. \unexpanded{#1}}\label{#2}}%
}
\makeatother

\def\Answer#1{
\noindent\textsf{Ans. \ref{#1}}
}

\Problem{FOOBAR}{foobar}

\Answer{foobar}


Then, \ref{foobar} in \Answer command returns a concatenation of the counter and the title of label foobar.

It suffices for my work. But, this method does not seem to work well for hyperlink. As I click the title in Answer, it goes to the first page.