5

I'm trying to create boxes in order to have exercises in my book. Nevertheless, I don't know what to do on certains points. I'm reading this paper :

https://mirror.hmc.edu/ctan/macros/latex/contrib/tcolorbox/tcolorbox.pdf

And I don't know what to do with some of those information. I found the interesting parts :

It would be great if I can use this :

/tcb/hypertarget=〈marker〉p100

/tcb/label=〈marker〉 p 98

If I understand well, the first one allows that by clicking on the box, I go somewhere else in the document (for example directly to the solution) and the second one allows me to write explicitly that an exercise is referring to a solution ? Or in another way, in the text, I can write a reference to an exercise ? Is it that?

In any case that's what I've written :

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{amsthm}
\usepackage{lmodern}    
\usepackage[most]{tcolorbox}




\newtcolorbox[auto counter, number within=section]{theoreme}[2][]{
    colback=red,
    colframe=red,
    colbacktitle=white!80!red,
    coltitle=black,
    fonttitle=\bfseries, 
    title=Théorème~\thetcbcounter.\ #2,
    before={}, 
    after={},
    attach boxed title to top left={yshift=-2mm, xshift=0.5cm},%
    #1
}





\newtcbtheorem[auto counter, number within=section]{exercice}{Exercice}{}{theo}
\newtcbtheorem[auto counter, number within=section]{solution}{Solution}{}{theo}


\begin{document}


hi


\end{document}

So, would it be possible to have that I can refer to the Exercise and to the Solution easily and how?

Thank you.

  • Welcome to TeX.SE. You're code does nothing yet, apart from defining some theorems and a theoreme box. Your question is unclear – user31729 Mar 20 '18 at 20:35
  • And it looks very familar to me, actually... Isn't this something similar like this: tex.stackexchange.com/questions/420571/referencing-and-labels ???? – user31729 Mar 20 '18 at 20:36
  • 1
    So you just have to write \begin{exercice e.g. } and then [label=other] and then the {} ... {} ? – Niels Mar 20 '18 at 20:50
  • because yes it is actually working ( I mean I succesfully got the number 0.0.1, but would it be possible to write \ref{other} and to have the possibility to click on the number to go back to the exercice? Like it is usually possible for ref ? because for now it is just the numbers that are written. And differently from Marine's question, I'm asking about something for the box itself. That's the /tcb/hypertarget=〈marker〉p100 – Niels Mar 20 '18 at 20:56
6

The hypertarget and hyperlink options are rather not meant for this, since they make the whole box a link or a target. It is better to place the targets explicitly in the title (or somewhere else) in the box.

I have removed the theorem definitions and replaced them by regular tcolorbox environments, inserting a hypertarget at the box title and a link to the solution or a link back to the exercise.

In order to make this work, both exercise and the solution must have the same label, e.g. foo or foobar etc.

The regular label must be inserted with label=mynicelabel etc. in the options of the tcolorbox instance.

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath,amsfonts,amssymb}%extensions de l'ams pour les mathématiques
\usepackage{amsthm} %pour les théoremes
\usepackage{lmodern}    %celui ci et le suivant pour les boites
\usepackage[most]{tcolorbox}

\usepackage{hyperref}



\newtcolorbox[auto counter, number within=section]{theoreme}[2][]{
    colback=red,
    colframe=red,
    colbacktitle=white!80!red,
    coltitle=black,
    fonttitle=\bfseries, 
    title=Théorème~\thetcbcounter.\ #2,
    before={}, 
    after={},
    attach boxed title to top left={yshift=-2mm, xshift=0.5cm},%
    #1
}



\newtcolorbox[auto counter, number within=section]{exercice}[3][]{title={\hypertarget{exer:#3}{Exercise \thetcbcounter.\ #2}\hfill\hyperlink{sol:#3}{Solution}},#1}
\newtcolorbox[auto counter, number within=section]{solution}[3][]{title={\hypertarget{sol:#3}{Solution \thetcbcounter.\ #2}\hfill\hyperlink{exer:#3}{Exercise}},#1}




\begin{document}


\begin{exercice}{Foo}{foo}

\end{exercice}

\begin{exercice}[colback=yellow!30!]{Foobar}{foobar}

\end{exercice}



\clearpage

\begin{solution}{Foo solution}{foo}

\end{solution}

\clearpage

\begin{solution}{Foobar solution}{foobar}

\end{solution}


\end{document}

Update without foo title

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath,amsfonts,amssymb}%extensions de l'ams pour les mathématiques
\usepackage{amsthm} %pour les théoremes
\usepackage{lmodern}    %celui ci et le suivant pour les boites
\usepackage[most]{tcolorbox}

\usepackage{hyperref}



\newtcolorbox[auto counter, number within=section]{exercice}[2][]{title={\hypertarget{exer:#2}{Exercise \thetcbcounter.\hfill\hyperlink{sol:#2}{Solution}}},#1}
\newtcolorbox[auto counter, number within=section]{solution}[2][]{title={\hypertarget{sol:#2}{Solution \thetcbcounter.\hfill\hyperlink{exer:#2}{Exercise}}},#1}




\begin{document}


\begin{exercice}{foo}

\end{exercice}

\begin{exercice}[colback=yellow!30!]{foobar}

\end{exercice}



\clearpage

\begin{solution}{foo}

\end{solution}

\clearpage

\begin{solution}{foobar}

\end{solution}
\end{document}

enter image description here

  • looks really nice. Thank you ! Would it be possible to not display the name Foo ? I think it will be nicer without it :) And are solutions and exercices going to have the same numbering? – Niels Mar 20 '18 at 21:12
  • @Niels: See the update at the end of my post. The exercise and solution will have the same numbering as long as there is no reset and the order of exercises is the same as the order of the solution. Any other approach requires much more work and I have no time for this right now – user31729 Mar 20 '18 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.