Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to have \newtheorem{teo}{Teorema}[chapter] if the book class is loaded but \newtheorem{teo}{Teorema}[section] if the article class is loaded. Can I do this with some sort of conditional?

\documentclass{article}
\usepackage{theorem}
\newtheorem{teo}{Teorema}[chapter] %if book
\newtheorem{teo}{Teorema}[section] %if article

\begin{document}

\end{document}
share|improve this question
3  
I think this question Which document class is being used? should answer your question. –  Alan Munn Aug 10 '11 at 3:49
    
For others that can't work out solutions here: I had a similar need but the place where knowledge of class was needed was inside a style file (.sty). Solutions here had LaTeX fail. It worked using \@ifclassloaded along with bits taken from: conditionals - Test if a package (or package option) is loaded - TeX - LaTeX Stack Exchange –  Stéphane Gourichon Mar 5 at 8:57

2 Answers 2

up vote 9 down vote accepted

Altough, the link Alan points to has the required info, that page assumes that you already know about \makeatletter/\makeatother macros, which depending on the experience level one may not have encountered.

The following is intended to exit if a class other than book or article is used. You can remove the \QUITHERE macro if that is not desired.

\documentclass{book}
\usepackage{theorem}

\makeatletter%
\@ifclassloaded{book}{%
    \newtheorem{teo}{Teorema}[chapter]%if book
    \typeout{Using book class.}%
}{%
    \@ifclassloaded{article}{%
        \newtheorem{teo}{Teorema}[section]%if article
        \typeout{Using article class.}%
    }{%
        \typeout{Error: Unsupported class: only 'article' and 'book' are suported.}\QUITHERE
    }%
}%
\makeatother%

\begin{document}
\end{document}
share|improve this answer
    
Thank you !!!!! –  Regis da Silva Aug 10 '11 at 4:14
    
Also see similar question on how to configure fancyhdr depending on the document class –  Peter Grill Aug 12 '11 at 4:39

It seems your aim is to make theorem numbers subordinate to the highest (non-\part) sectioning level defined in the respective document class -- \chapter for the book class, and \section for article. Therefore, I suggest to test not for the name of the loaded class, but for the availability of the \chapter command. If this command is undefined (or its meaning is \relax), number theorems per section, else, per chapter. This method will also work for, e.g., the report class and the KOMA-Script classes.

\documentclass{book}

\usepackage{etoolbox}

\ifundef{\chapter}{%
  \newtheorem{teo}{Teorema}[section]% if \chapter not defined
  \typeout{Numbering theorems per section.}%
}{%
  \newtheorem{teo}{Teorema}[chapter]% if \chapter defined
  \typeout{Numbering theorems per chapter.}%
}

\begin{document}

\begin{teo}
A theorem.
\end{teo}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.