GeneralCounter for defined environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Emonk
Posts: 5
Joined: Sat Apr 09, 2011 9:58 am

Counter for defined environment

Post by Emonk »

Hi all,

I have an environment in my LaTeX code:

Code: Select all

\newenvironment{exercise}[1]
{\par \noindent \newline \newline \line(1,0){360} \\ \textbf{Exercise #1} \begin{itshape}\\}
{\\ \end{itshape} \line(1,0){360}}
I can call the environment by:

Code: Select all

\begin{exercise}{exercise_number}Contents\end{exercise}
But: I now have 18 exercises, and what if I want to paste in an exercise after exercise 7? Than I'll have to renumber al exercises after number 7! Is there an easy way to insert a counter, that I don't have to send the number of the exercise? Just like this:

Code: Select all

\begin{exercise}Contents\end{exercise}
Note: my English is very bad, please use easy words :)

Thanks!
Last edited by Emonk on Sat Apr 09, 2011 11:25 am, edited 1 time in total.

Recommended reading 2024:

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

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

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

Counter for defined environment

Post by localghost »

You do not need to reinvent the wheel. It's so much easier (and smarter) to use the ntheorem package. Study its manual to learn more.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{ntheorem}

\theoremstyle{break}
\theoremprework{\hrule}
\theorempostwork{\hrule}
\newtheorem{exercise}{Exercise}

\begin{document}
  \begin{exercise}
    Text
  \end{exercise}
\end{document}
And by the way, please always provide a minimal example that is compilable as provided for others.


Best regards and welcome to the board
Thorsten
Emonk
Posts: 5
Joined: Sat Apr 09, 2011 9:58 am

Counter for defined environment

Post by Emonk »

Very much thanks!
I now have added this code:

Code: Select all

\usepackage{ntheorem}
\theoremstyle{break}
\theoremprework{\hrule}
\theorempostwork{\hrule}
\newtheorem{exercise2}{Exercise}
But I get the error:
! LaTeX Error: Command \theoremstyle already defined.
Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...

l.76 }

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

Counter for defined environment

Post by localghost »

As already said, reduce the problem to a complete but minimal example. It has to contain only necessary code and must be compilable as provided. Could be that the digit in the name of the new theorem causes trouble. Have you tested my example?
Emonk
Posts: 5
Joined: Sat Apr 09, 2011 9:58 am

Re: Counter for defined environment

Post by Emonk »

Yes, and now it's working. THANKS!
Post Reply