1

I have created a new environment for definitions and theorems (with a leftbar style) and a specific counter (\thesection.\thecounter), to have (for instance) the following numbering: Definition 1.2.1, Theorem 1.2.2, Theorem 1.2.3... When I refer to some definition or theorem, I would like to have the precise number (\thesection.\thecounter) as it appears on the PDF. The problem is that (as you could see the screenshot), when I use \label and \ref, I only have the number of the section. I have tried several things (including using \refstepcounter) without any success...

Here is the code:

\documentclass[11pt,a4paper]{book}
\usepackage[left=1.5cm,right=1.5cm,
top=2.5cm,bottom=1.2cm]{geometry}

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage[french]{babel}
\RequirePackage{amsfonts,amsmath,amssymb,amsthm,lmodern}
\usepackage[normalem]{ulem}

\usepackage{bclogo,graphicx, fancybox, enumitem, moreverb, textpos, xcolor, colortbl}

\RequirePackage{framed}
\newlength{\leftbarwidth}
\newlength{\leftbarsep}
\newcommand*{\leftbarcolorcmd}{\color{leftbarcolor}}
\renewenvironment{leftbar}{
\def\FrameCommand{{\leftbarcolorcmd{
\vrule width \leftbarwidth \hspace{\leftbarsep}}}
\fboxsep=\FrameSep\colorbox{colorboxcolor}
}
\MakeFramed {\advance \hsize-\width \FrameRestore}
}{
\endMakeFramed}

\newcounter{compteur}

\newenvironment{theo}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{red}
\colorlet{colorboxcolor}{red!10}
\begin{leftbar}
{\red\textsc{Théorème \addtocounter{compteur}{1}
\thesection.\thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newenvironment{defi}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{blue}
\colorlet{colorboxcolor}{blue!10}
\begin{leftbar}
{\blue\textsc{Définition \addtocounter{compteur}{1}\thesection.\thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newtheorem{theorem}{Théorème}[section]        
\theoremstyle{definition}        
\newtheorem{definition}{Définition}[section]

\begin{document}

\chapter{First chapter}

\section{First section}
\section{Second section}
 
\begin{definition}\label{def1}A definition
\end{definition}

\begin{theorem}\label{th1}A theorem
\end{theorem}

Definition \ref{def1} and Theorem \ref{th1}

\begin{defi}\label{def2}A beautiful definition
\end{defi}

\begin{theo}\label{th2}A beautiful theorem
\end{theo}

Beautiful Definition \ref{def2} and beautiful Theorem \ref{th2}

\end{document}

enter image description here

1 Answer 1

1

There are several issues with your code, e.g., adding extra spaces by using something like:

Théorème \addtocounter{compteur}{1}
\thesection.

which adds an extra space because of the not commented end-of-line.

The issues with the labels are:

  • To get a reference to a counter, you need to increase the counter with \refstepcounter{…} instead of \addtocounter{…}{1}.
  • The scope of \refstepcounter ends on the end of the current group.

So instead of a construct like:

{\red\textsc{Théorème \addtocounter{compteur}{1}
\thesection.\thecompteur.}

you need to move the \refstepcounter outside the group with {…}, doing something like:

\refstepcounter{comteur}%
{\red\textsc{Théorème \thesection.\thecompteur.}

Now, you can use \label{…} inside the theo environment, because the \refstepcounter and the \label would now be on the same group level. You have to do the same for all your environments.

\documentclass[11pt,a4paper]{book}
\usepackage[left=1.5cm,right=1.5cm,
top=2.5cm,bottom=1.2cm]{geometry}

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage[french]{babel}
\RequirePackage{amsfonts,amsmath,amssymb,amsthm,lmodern}
\usepackage[normalem]{ulem}

\usepackage{bclogo,graphicx, fancybox, enumitem, moreverb, textpos, xcolor, colortbl}

\RequirePackage{framed}
\newlength{\leftbarwidth}
\newlength{\leftbarsep}
\newcommand*{\leftbarcolorcmd}{\color{leftbarcolor}}
\renewenvironment{leftbar}{
\def\FrameCommand{{\leftbarcolorcmd{
\vrule width \leftbarwidth \hspace{\leftbarsep}}}
\fboxsep=\FrameSep\colorbox{colorboxcolor}
}
\MakeFramed {\advance \hsize-\width \FrameRestore}
}{
\endMakeFramed}

\newcounter{compteur}

\newenvironment{theo}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{red}
\colorlet{colorboxcolor}{red!10}
\begin{leftbar}
\refstepcounter{compteur}{\red\textsc{Théorème 
\thesection.\thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newenvironment{defi}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{blue}
\colorlet{colorboxcolor}{blue!10}
\begin{leftbar}
  \refstepcounter{compteur}%
{\blue\textsc{Définition \thesection.\thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newtheorem{theorem}{Théorème}[section]        
\theoremstyle{definition}        
\newtheorem{definition}{Définition}[section]

\begin{document}

\chapter{First chapter}

\section{First section}
\section{Second section}
 
\begin{definition}\label{def1}A definition
\end{definition}

\begin{theorem}\label{th1}A theorem
\end{theorem}

Definition \ref{def1} and Theorem \ref{th1}

\begin{defi}\label{def2}A beautiful definition
\end{defi}

\begin{theo}\label{th2}A beautiful theorem
\end{theo}

Beautiful Definition \ref{def2} and beautiful Theorem \ref{th2}

\end{document}

This already results in:

Beautiful Definition 1 and beautiful Theorem 2

If the reference should not only be the compteur counter but the compteur counter prefixed by the section counter, you could redefine \thecomteur and indeed also reset it with each section using

\counterwidthin{compteur}{section}

after the definition of compteur and also change the output in your environments:

\documentclass[11pt,a4paper]{book}
\usepackage[left=1.5cm,right=1.5cm,
top=2.5cm,bottom=1.2cm]{geometry}

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage[french]{babel}
\RequirePackage{amsfonts,amsmath,amssymb,amsthm,lmodern}
\usepackage[normalem]{ulem}

\usepackage{bclogo,graphicx, fancybox, enumitem, moreverb, textpos, xcolor, colortbl}

\RequirePackage{framed}
\newlength{\leftbarwidth}
\newlength{\leftbarsep}
\newcommand*{\leftbarcolorcmd}{\color{leftbarcolor}}
\renewenvironment{leftbar}{
\def\FrameCommand{{\leftbarcolorcmd{
\vrule width \leftbarwidth \hspace{\leftbarsep}}}
\fboxsep=\FrameSep\colorbox{colorboxcolor}
}
\MakeFramed {\advance \hsize-\width \FrameRestore}
}{
\endMakeFramed}

\newcounter{compteur}
\counterwithin{compteur}{section}

\newenvironment{theo}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{red}
\colorlet{colorboxcolor}{red!10}
\begin{leftbar}
\refstepcounter{compteur}{\red\textsc{Théorème 
\thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newenvironment{defi}[1][]%
{\setlength{\leftbarwidth}{1pt}
\setlength{\leftbarsep}{2pt}
\colorlet{leftbarcolor}{blue}
\colorlet{colorboxcolor}{blue!10}
\begin{leftbar}
  \refstepcounter{compteur}%
{\blue\textsc{Définition \thecompteur.}} \textbf{#1}}%
{
\end{leftbar}
}

\newtheorem{theorem}{Théorème}[section]        
\theoremstyle{definition}        
\newtheorem{definition}{Définition}[section]

\begin{document}

\chapter{First chapter}

\section{First section}
\section{Second section}
 
\begin{definition}\label{def1}A definition
\end{definition}

\begin{theorem}\label{th1}A theorem
\end{theorem}

Definition \ref{def1} and Theorem \ref{th1}

\begin{defi}\label{def2}A beautiful definition
\end{defi}

\begin{theo}\label{th2}A beautiful theorem
\end{theo}

Beautiful Definition \ref{def2} and beautiful Theorem \ref{th2}

\end{document}

Beautiful Definition 1.2.1 and beautiful Theorem 1.2.2

Another alternative would be to change the reference format of the counter, but this is only needed if \the<COUNTER> should be different from the \ref result, which does not seem to be the case here.

BTW: I would recommend to have a look into package tcolorbox. This package does also support definition of new theorem environments with handling of the counters and labels/references to that counters.

1
  • Thanks a lot for your quick, complete and very clear answer! :-)
    – J-P
    Commented Jul 19 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .