I'm trying to create a new numbered environment called 'example'. I would like to be able to reference the examples I create from other places in the document. I'm not sure how to do it...
then '\ref{MyExample}' would be referencing the current section rather than example. So basically, I think that what I need is to somehow 'tell' Latex that '\label' should be linked to the '\theexample' counter ?
Does anyone know how that can be done (or something cleverer, that does what I'm interested in)?
There are several packages that allow the declaration of own floats. First I would suggest the caption package. Refer to Section 4 (Own enhancements -\DeclareCaptionType, p. 27f) to learn more. Since the caption package allows customizing captions of floats in many ways, I would consider this the preferable way.
Other packages like float, floatrow or trivfloat also let you define new float environments. Refer to the documentations of the packages to see how to get things work.
You expect that the \label commands catches the last value of the counter that numbers the example. If so, replace \stepcounter by \refstepcounter. Note that there are three different commands to increase counters: \addtocounter, \stepcounter and \refstepcounter. Each one has a different secondary effect. The \label command picks only the value of the last counter whose value has been increased by one with \refstepcounter.
Please, let me give you a couple of additional advices:
The \newenvironment command requires two mandatory arguments: the first, for things to be done when the environment starts and the second, for things to be processed when the environment ends. Your definition of "example" only contains one mandatory argument. The second one is taken as null. Just to play with this, add "{Hello}" and the end of the definition of example:
... \rm}{\medskip}}{Hello}
Then compile. As you can easily see, the word "Hello" is typeset at the end of every example, since it is taken as the second argument of the example environment.
The counter of the example environment can also be called example. It is the more natural choice from the user's viewpoint. LaTeX makes this possible. In the low internal level, the environment relies on the commands \example and \endexample, whereas the counter is managed by \c@example. As you see, they are all different commands. No problem then.
The \setcounter command is superfluous if, in the environment, you first increase the counter and then print it.
Don't you use \rm as well as other obsolete commands like \tt, \bf, \sf, \it or \sc. Replace then by \rmfamily, \ttfamily, \bfseries, \sffamily, \itshape and \scshape.
\noindent has no arguments. So the braces right after it are superfluous.
An alternative code to yours could have the following appearance:
A final comment. Instead of defining numbered environments by yourself, you can also use the facilities provided by the \newtheorem command. Take a look to the packages amsthm and ntheorem.
I have one similar problem.
I would like to use something like the figure environment but instead of Figure 1: , Figure 2:, ... I would like to have something like Diagram 1:, Diagram 2:, ...
I am also using the figure environment for normal pictures and have a list of figures. I would also like to reference the diagrams and have an extra list of diagrams.
If I use this, the title is not below the picture. I am also not sure how to create a list of diagrams in this case.
Feel free to open a new topic for any new question you have. Your follow-up question to this 2008 topic might be a bit hard to see for some fellow forum users. We got enough space for millions of topics, so don't hesitate. We can even sort the archive of solutions better then.
If you still need help after Johannes advice, let us know, perhaps with more details.