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.

Here is what I am trying to achieve:

Chapter 1.
...
Theorem 1.1.
...
Section 1.1.
...
Theorem 1.1.1.
...
Subsection 1.1.1.
...
Theorem 1.1.1.1.

Is this even possible? If so, how? ...

share|improve this question
    
Just to clarify, you want one environment that will automatically detect the sectioning depth? –  cmhughes Oct 9 '11 at 16:31
    
That is indeed what I want. (I have several other theorem environments that will be synchronized with the theorem environment's counter.) –  Lucas Oct 9 '11 at 16:45

2 Answers 2

Using the etoolbox package you can patch the sectional commands (\chapter, \section, \subsection, ...) to redefine the way the theorem counter works. Resetting the theorem counter at every sectional unit is obtained via \@addtoreset, although this is also possible using the chngcntr package). Here's a working minimal example that does exactly that:

\documentclass{book}
\usepackage{etoolbox}% http://ctann.org/pkg/etoolbox
\newtheorem{theorem}{Theorem}

% Modify theorem counter to match that of the section unit
\preto{\chapter}{\renewcommand{\thetheorem}{\thechapter.\arabic{theorem}}}%
\preto{\section}{\renewcommand{\thetheorem}{\thesection.\arabic{theorem}}}%
\preto{\subsection}{\renewcommand{\thetheorem}{\thesubsection.\arabic{theorem}}}%

% Reset the counter at every sectional unit
\makeatletter
\@addtoreset{theorem}{chapter}
\@addtoreset{theorem}{section}
\@addtoreset{theorem}{subsection}
\makeatother

\begin{document}

\chapter{First chapter}
\begin{theorem} This is a theorem \end{theorem}
\section{First section}
\begin{theorem} This is a theorem \end{theorem}
\subsection{First subsection}
\begin{theorem} This is a theorem \end{theorem}
\section{Second section}
\begin{theorem} This is a theorem \end{theorem}
\subsection{First subsection}
\begin{theorem} This is a theorem \end{theorem}
\begin{theorem} This is a theorem \end{theorem}
\subsection{Second subsection}
\begin{theorem} This is a theorem \end{theorem}

\end{document}

Section-synchronized theorem

share|improve this answer
    
Trying this gives me a LaTeX error about \thetheorem being undefined when I hit my first \chapter command. –  Lucas Oct 9 '11 at 17:14
    
Did you copy/paste the entire minimal example, or just use part of it in your document? If \thetheorem is undefined, it could be that you need \newcounter{theorem} before \newtheorem{..}{..}. Try that as a quick-fix attempt. Otherwise, report back with more information. –  Werner Oct 9 '11 at 17:19
    
Well, \newcounter{theorem} resolved the error, but I got "Theorem 1" "Theorem 2" "Theorem 3" in my output. –  Lucas Oct 9 '11 at 17:24
    
I am unable to duplicate your problem, even when I use ntheorem or amsthm. Did you copy/paste my minimal example as-is and it gave you these errors? If so, are you able to include an MWE in your original question that shows your code? Otherwise it would be very difficult to assess the problems without proper reproduction. –  Werner Oct 9 '11 at 17:39

Maybe this can help you …

\documentclass{scrbook}

\usepackage{etoolbox}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}

\makeatletter
% save current section numbering
\AtBeginEnvironment{theorem}{\edef\tmpsect{\@currentlabel}}
% reset theorem counter with all headings
\@addtoreset{theorem}{chapter}
\@addtoreset{theorem}{section}
\@addtoreset{theorem}{subsection}
% add current section numbering to theorem number
\renewcommand{\thetheorem}{\tmpsect-\arabic{theorem}}
\makeatother

\begin{document}
\chapter{Chapter}
\begin{theorem}
Text
\end{theorem}
\section{Section}
\begin{theorem}
Text
\end{theorem}
\section{Section}
\begin{theorem}
Text
\end{theorem}
\subsection{Subsection}
\begin{theorem}
Text
\end{theorem}
\section{Section}
\begin{theorem}
Text
\end{theorem}
\subsection{Subsection}
\begin{theorem}
Text
\end{theorem}
\section{Section}
\begin{theorem}
Text
\end{theorem}
\subsubsection{Subsubsection}
\begin{theorem}
Text
\end{theorem}
\section{Section}
\begin{theorem}
Text
\end{theorem}
\subsection{Subsection}
\begin{theorem}
Text
\end{theorem}
\end{document}

result

If not try to provide a minimal working example and clarify your question, please.

Note
that \@currentlabel represents the current label (wow …) which can be another then the current section. But as egreg said if you don’t use labels outside of groups (like {figure}) there should be no problems …

share|improve this answer
2  
\@currentlabel may not reflect the current sectioning level: no \label outside a group should occur to make this work. –  egreg Oct 9 '11 at 19:13
    
@egreg: Thanks! I added this as note :-) –  Tobi Oct 10 '11 at 9:41

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.