2

Hello (and sorry for the englishmarmelade),

I try to define a environment exercise. I would like to define the label automatically.

Why ? First and unique goal is to see in my structure faster where is my exercice and how many exercice of this type i have.

My code :

\documentclass[11pt,a4paper]{article}

\usepackage[utf8]{inputenc} \usepackage[french]{babel} \usepackage[T1]{fontenc}

%--hyperref \usepackage[pdfborder={0 0 0}]{hyperref} %pour que les références soient des liens hypertextes %\usepackage{charter} %Modif des polices : charter, newcent, mathptmx, mathpple... \hypersetup{ colorlinks=true, linkcolor=blue, filecolor=magenta,
urlcolor=cyan, }

%------Operation math et latex \usepackage{etoolbox}

\newcounter{exo} \makeatletter \newenvironment{exo}[1]% {\refstepcounter{exo}% \protected@edef@currentlabelname{#1}% addition here \vspace{0.5cm}\noindent

\fbox{ {\large\bfseries{Exercice \theexo~: #1} \par}\label{#1} {\par\vspace{0.5cm}}}} \makeatother

\begin{document} \begin{exo}{type} Derivate this function $f(x)=x$ \end{exo}

\end{document}

First problem : It works (i can use \ref) but i don't see it on my structure view (i use texmaker) and that was my goal.

Comment : The editor can't see the label inside an environment So that's not the way.

Second problem : If type is empty i have an error because \label wait an information Idea : How to solve this with an ifempty
''' \ifx&#1&% % #1 is empty \label{theexo} \else % #1 is nonempty \label{#1} \fi '''

Another try : Changing \subsection to use it as exercise but not sur it's a good solution.

Thank you for all the help.

Ange

  • Welcome. // Can you please add the beginning (\documentclass ... etc.? Thank you – MS-SPO Jan 17 '24 at 16:33
  • 5
    it is normally not a good idea to create the labels automatically like this. If you change the exercises all references will be wrong. And no, an editor can not detect a label that you hide in a command. – Ulrike Fischer Jan 17 '24 at 16:33
  • 3
    Welcome to TeX.se, and thanks for providing an (almost complete) minimal example document. I suspect that almost everyone's advice will be "Don't do this". :) – Alan Munn Jan 17 '24 at 16:36
  • 2
    FYI, you may want to look at and use the various packages already available for exercises or exams at ctan: https://ctan.org/topic/exam . – MS-SPO Jan 17 '24 at 16:38
  • 1
    I'm not seeing why the second problem is surprising. If the type is empty, what are you wanting to happen? What should the \label do? – Teepeemm Jan 17 '24 at 17:16
  • Note that hyperref should be loaded last unless you know a package must be loaded later (and there are very few such packages). – cfr Jan 17 '24 at 18:35
  • Do you need to print the information in the PDF, or is it enough to write messages to the log file? If messages are enough: you can place \typeout{Exercise on page \thepage.} at the start of each environment. You can also define a counter, and add to the counter each time the environment is used, then typeout the value at end document. – rallg Jan 17 '24 at 19:03
  • For one problem with such automatically generated labels see, e.g., "Automatic Labelling" of (sub)(sub)sections does not work with math – cabohah Jan 18 '24 at 07:41

1 Answers1

1

I've streamlined your code, as it seems to be needlessly complicated. E.g., why have both \label{#1} and \protected@edef\@currentlabelname{#1}?

I would further strongly recommend that you specify that the exo environment take 2 arguments: The first to specify the label to be used in cross-references, and the second to specify the title string. If you want, you can use the same string (say, "type") for both arguments; however, I recommend you preserve the flexibility to use two different arguments. That way, if you choose to change the title string at some future point in time, you don't also have to change the argument of various \ref commands.

enter image description here

\documentclass[11pt,a4paper]{article}

%%\usepackage[utf8]{inputenc} % that's the default nowadays \usepackage[T1]{fontenc} \usepackage[french]{babel} \usepackage[margin=2.5cm]{geometry} % choose suitable page parameters

%\usepackage{etoolbox} % doesn't seem to be needed

\newcounter{exo} %\makeatletter % not needed anymore \newenvironment{exo}[2]% {\refstepcounter{exo}\label{#1}% \vspace{0.5cm} \noindent \fbox{\large\bfseries Exercice \theexo,: #2}\space} {\par\vspace{0.5cm}} %\makeatother % also not needed anymore

\usepackage{hyperref} % load this package almost last \hypersetup{colorlinks=true, allcolors=blue}

\usepackage[nameinlink,french]{cleveref} % load this package last \crefname{exo}{exercice}{exercices}

\begin{document}

\begin{exo}{ex_type}{type} Créer la fonction $f(x)=x$. \end{exo}

\noindent Une référence croisée à l'\cref{ex_type}. \end{document}

Mico
  • 506,678