# Cross-referencing a custom environment with two arguments

Sorry if my question title is ambiguous.

I want to write an environment called "problem" that works as follows. I want to be able to write

    \begin{problem}{1}{2}
this is a problem statement \label{myprob}
\end{problem}

Look at problem number \ref{myprob}


and get the following output:

    Problem 1.2: This is a problem statement.

Look at problem number 1.2.


Any ideas as to how I would do this?

-
What does the first number represent? – egreg Feb 7 '13 at 0:13
If it represents the current chapter or section it is easier to do this. – Sigur Feb 7 '13 at 0:26
The title and body of your question appear to have conflicting goals: The title refers to an environment with two counters; in the TeX/LaTeX sphere a counter is usually understood to be a variable (implemented as a TeX macro) whose values can be nonnegative integers). In contrast, the body of the question seems to refer to an environment that takes two arguments, both of which can be arbitrary integers but don't seem to be variables -- and thus don't seem to be counters in the TeX-specific sense of the word. Please consider revising your question to clarify your objective. – Mico Feb 7 '13 at 0:32
@egreg: the first number refers the problem set and the second refers to the problem number on that problem set. – zdorovo Feb 7 '13 at 5:13
@zdorovo: If you're interested in a more comprehensive (and automated) solution - creation of a problem set and number - consider asking a new, follow-up question. – Werner Feb 7 '13 at 6:23

If that's all, then this will do:

\documentclass{article}
\makeatletter
\newenvironment{problem}[2]
{\def\@currentlabel{#1.#2}%
\noindent\textbf{Problem~\@currentlabel}:\space\ignorespaces}
{\par}
\makeatother
\begin{document}
\begin{problem}{1}{2}
This is a problem statement. \label{myprob}
\end{problem}
\end{document}


This relies on the fact that referencing is done using \@currentlabel. Therefore, setting this appropriately. Passing macros as arguments to problem might require some expansion.

-

Here is another solution using theorems environment from amsthm package.

\documentclass{report}
\usepackage{amsthm}

\newcounter{problemH}
\setcounter{problemH}{1} % set up the first number
\newtheorem{problem}{Problem}[problemH]

\begin{document}
\begin{problem}
this is a problem statement \label{myprob}
\end{problem}

Look at problem number \ref{myprob}

\begin{problem}
this is a problem statement
\end{problem}
\end{document}


To replace the period . by colon : use

%\newtheoremstyle{stylename}{spaceabove}{spacebelow}{bodyfont}{indentamt}{headfont}{headpunct}{headspace}{headspec}
\newtheoremstyle{problemH}{3pt}{3pt}{}{}{\bfseries}{:}{.5em}{}
\theoremstyle{problemH}
\newtheorem{problem}{Problem}[problemH]


to obtain

-
I was wondering if there was a nice way to use the theorem environment for this. The only issue with this solution is that I want the number of the Problem to refer to its position in my homework assignment and not in the notes I'm typing. – zdorovo Feb 7 '13 at 5:28
@zdorovo, so you want to type the position (counter) every time? – Sigur Feb 7 '13 at 11:51
Yeah, since the order I'll put homework problems in my class notes will not be the order that the problems appear in my homework. Sorry for not giving more context. I suppose a more elegant and perhaps intuitive solution would be to somehow grab problem statements from my homework solutions (which I also typeset with LaTeX) but that seemed too complicated. – zdorovo Feb 7 '13 at 16:23