GeneralHow to create a numbered (& 'referenceable') newenvironment?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Pawel
Posts: 9
Joined: Fri Jul 25, 2008 9:51 am

How to create a numbered (& 'referenceable') newenvironment?

Post by Pawel »

All,

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...

What I have at the moment is

Code: Select all

\newcounter{theexample} \setcounter{theexample}{1}
\newenvironment{example}[1][]{\medskip\noindent {\textbf{Example
\arabic{theexample}. #1} \stepcounter{theexample} \rm}{\medskip}}
However, when I do

Code: Select all

\begin{example}[My Example]\label{MyExample}
yada yada yada
\end{example}
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)?

Thanks in advance for any help.
Pawel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to create a numbered (& 'referenceable') newenvironment?

Post by localghost »

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.


Best regards and welcome to the board
Thorsten¹
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to create a numbered (& 'referenceable') newenvironment?

Post by Juanjo »

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:

Code: Select all

\newcounter{example}
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip\noindent%
   \textbf{Example~\theexample. #1} \rmfamily}{\medskip}
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.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Pawel
Posts: 9
Joined: Fri Jul 25, 2008 9:51 am

Re: How to create a numbered (& 'referenceable') newenvironment?

Post by Pawel »

Guys,

I am at a loss for words (and slightly embarassed for having posted such a crap bit of LaTeX code).

Thank you VERY much.
Pawel
Stefanie
Posts: 25
Joined: Fri Apr 04, 2008 2:39 pm

Re: How to create a numbered (& 'referenceable') newenvironment?

Post by Stefanie »

a bit late, maybe, but for numbered examples i'd recommend the gb4e package. It's very good, also have a look at cgloss4e.sty for interlinear glosses.
Pawel
Posts: 9
Joined: Fri Jul 25, 2008 9:51 am

Re: How to create a numbered (& 'referenceable') newenvironment?

Post by Pawel »

It's never too late ;). Thanks. I'll have a look.
p
vewi
Posts: 1
Joined: Wed Apr 08, 2015 3:42 pm

How to create a numbered (& 'referenceable') newenvironment?

Post by vewi »

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.

Code: Select all

 \newcounter{diagram}
  \newenvironment{diagram}[1][]{\refstepcounter{diagram}\par\medskip\noindent
       \center{Diagram~\thediagram: #1}}
Thank you for your help.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to create a numbered (& 'referenceable') newenvironment?

Post by Johannes_B »

Hi and welcome,

this can be easily done with package tocbasic. Please have a look at chapter 15.5 of the documentation. It introduces the command \DeclareNewTOC.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Re: How to create a numbered (& 'referenceable') newenvironm

Post by Stefan Kottwitz »

Hi vewi,

welcome to the forum!

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.

Stefan
LaTeX.org admin
Hoed
Posts: 28
Joined: Sat Jan 07, 2012 12:46 pm

Re: How to create a numbered (& 'referenceable') newenvironm

Post by Hoed »

@ Juanjo

Thank you, even years after you made it, for this suggestion. Saved a lot of trouble-shooting for me.

Regards.
Post Reply