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}

\refstepcounterinstead of\stepcounterto let TeX know that the counter should be referable. Is there a reason for not sticking to the regular\label-\ref-mechanism? – Tobi Sep 03 '18 at 13:10%at the ends of lines in your macros, but I do not see why you can't use\refhere,\myrefis just the same except that it adds space before and after the returned number. – David Carlisle Sep 03 '18 at 13:18\defto define commands, LaTeX aficionados recommend using\newcommandas this checks to make sure you are not overwriting an existing command. It also has some other nice features; see, for example, https://tex.stackexchange.com/questions/191266/defining-macros-with-arguments/191271#191271. You can also use\NewDocumentCommandand friends from the xparse package. – Sep 03 '18 at 13:34