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