1

With the following codes

\documentclass[12pt,a4paper]{article}
\usepackage{amsfonts,amsmath,amssymb,graphicx}
\usepackage{tikz,tkz-tab,moreverb} % tableau de signe moreverb not needed
\usetikzlibrary{arrows}
\usepackage[francais]{babel}
\usepackage{pifont} %bouni
\usepackage{fancybox} %pour faire l'encadrement
\usepackage[latin1]{inputenc}
\usepackage{verbatim}
\usepackage{color}
\usepackage[final]{pdfpages} %pour inserer une page pdf
\usepackage{fancyhdr} % pagestyle
%---- Dimensions des marges ---
\usepackage{geometry}
\geometry{left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm}
%\usepackage{setspace}
%\onehalfspacing
%---- Structure Exercice -----
\newtheorem{Exc}{Exercice}
\def\exo#1{\futurelet\testchar\MaybeOptArgmyexoo}
\def\MaybeOptArgmyexoo{\ifx[\testchar \let\next\OptArgmyexoo
                        \else \let\next\NoOptArgmyexoo \fi \next}
\def\OptArgmyexoo[#1]{\begin{Exc}[#1]\normalfont}
\def\NoOptArgmyexoo{\begin{Exc}\normalfont}
\newcommand{\finexo}{\end{Exc}}
\newcommand{\flag}[1]{}
%%%%%%%%%%%%%%%% debut exercice  %%%%%%%%%%%%%%%%%%%
%\exo{{}}:\\

%\finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \definecolor{sfaxlogo}{RGB}{127,176,206} \colorlet{sfaxblue}{sfaxlogo!70!black!60!blue} \begin{document} %%%%%%%%%%%%%%%% debut exercice %%%%%%%%%%%%%%%%%%% \exo{{}}:

\finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% debut exercice %%%%%%%%%%%%%%%%%%% \exo{{}}:

\finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% \end{document}

I get

enter image description here

How can get the exercises with the following form

enter image description here

Schüler
  • 886

1 Answers1

2

This is a good use case for amsthm, which lets you define a custom theorem style. Also, \newcommand is generally favored over \def, it also allows a much cleaner definition of the behavior you want:

\documentclass[12pt,a4paper]{article}
\usepackage{fancybox}
\usepackage{amsthm}
%any other packages

%---- Structure Exercice ----- \newtheoremstyle{sboxexc}% name of the style to be used {\topsep}% measure of space to leave above the theorem. E.g.: 3pt {\topsep}% measure of space to leave below the theorem. E.g.: 3pt {\itshape}% name of font to use in the body of the theorem {0pt}% of space to indent {\bfseries}% name of head font {}% punctuation between head and body { }% space after theorem head; " " = normal interword space {\thmname{#1}}% Manually specify head \newcounter{counter} \setcounter{counter}{1} \theoremstyle{sboxexc} \newcommand{\optionaltext}{} \newtheorem*{Exc}{\shadowbox{Exercice \arabic{counter}\optionaltext}} \newcommand{\exo}[2][]{\renewcommand{\optionaltext}{#2}\begin{Exc}\normalfont $:$} \newcommand{\finexo}{\renewcommand{\optionaltext}{}\end{Exc}\addtocounter{counter}{1}}

\begin{document} %%%%%%%%%%%%%%%% debut exercice %%%%%%%%%%%%%%%%%%% \exo

Lorem \finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% debut exercice %%%%%%%%%%%%%%%%%%% \exo{: optional extra text}

Lorem \finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% debut exercice %%%%%%%%%%%%%%%%%%% \exo

Lorem \finexo %%%%%%%%%%%%%%%% fin exercice %%%%%%%%%%%%%%%%%%% \end{document}

Gives

enter image description here

See:

Plant
  • 48
  • Thank you. But the problem is that I cannot make Lorem in a new line – Schüler Oct 05 '20 at 18:41
  • You can simply add a new line at the end of the \newtheorem: \newtheorem*{Exc}{\shadowbox{Exercice \arabic{counter}\optionaltext}\\} – Plant Oct 05 '20 at 19:51
  • Please if I wrote \exo{} \begin{itemize} \item [(1)] \item [(2)] \item [(3)] \end{itemize} \finexo, I will not obtain a new line. I hope to help me. Thanks – Schüler Oct 06 '20 at 07:28
  • For environments to register as a line you need to manually have some content in the line defined by \exo, a hacky fix is \newcommand{\exo}[2][]{\renewcommand{\optionaltext}{#2}\begin{Exc}\normalfont $\:$}, (and no new line at the end of \newtheorem). There may be a more "correct" way but that should work. – Plant Oct 06 '20 at 17:17