# How to use \newenvironment; why doesn't this code work? How do I fix it?

I've been trying to find out but my latex skills are too weak to understand what seem like similar problems. When I try this:

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{exsheets}
\usepackage{tcolorbox}

%#####################
\begin{document}

\newenvironment{oppg}{\begin{tcolorbox}\begin{question}}{\end{question}\end{tcolorbox}}

\begin{oppg}
Exercise text in a box
\end{oppg}

\begin{oppg}
Exercise text in a box
\end{oppg}

\end{document}


I get a whole bunch of errors.

I don't understand why this doesn't work.

How can I wrap my exercises in colored boxes?

Thanks!

• The nesting is weird. I believe, you could get this with tcolorbox commands too – user31729 Jul 3 '15 at 14:39
• I've removed the memoir tag as it is not related to memoir – daleif Jul 3 '15 at 14:42

It is possible to wrap the question environment in a \tcolorboxenvironment redefinition.

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{exsheets}
\usepackage[most]{tcolorbox}

\begin{document}

\tcolorboxenvironment{question}{colback=yellow}  % 2nd arg is for tcolorbox options.

\begin{question}
Exercise text in a box
\end{question}

\end{document}


Edit A little bit more flexible, without question being forever now a tcolorbox, I made a copy via \LetLtxMacro, naming it \oppg.

This could be used with \tcolorboxenvironment instead of question which is still the same environment. However, both environments share the same counter (so far).

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{exsheets}
\usepackage[most]{tcolorbox}
\usepackage{letltxmacro}

\begin{document}

\LetLtxMacro\oppg\question

\tcolorboxenvironment{oppg}{colback=yellow,colframe=red}

\begin{question}%[print=false]
Exercise text in a box
\end{question}

\begin{oppg}[print=false]
Another box
\end{oppg}

\begin{oppg}
Another box
\end{oppg}

\end{document}


• I'll update with another possibility – user31729 Jul 3 '15 at 14:45
• This in a way disables \begin{question}[print=false] but that's probably not too big a deal in most use cases – cgnieder Jul 3 '15 at 14:54
• @clemens: I know, that's why I want to provide another solution – user31729 Jul 3 '15 at 14:55
• A solution with exsheets other than your current solution may not be easy without hacking. Using exsheets' hooks (which are intended for stuff like this) probably won't work with tcolorbox I suspect... – cgnieder Jul 3 '15 at 15:03
• @john.abraham You can't have floating objects inside a box. Note that you don't need \begin{figure} and \end{figure} around \includegraphics. – egreg Jul 3 '15 at 17:41

I've just (2015/07/04) sent an update of exsheets to CTAN which allows the following (recommended) solution:

\SetupExSheets{
question/pre-hook = \begin{tcolorbox} ,
question/post-hook = \end{tcolorbox}
}


The update should be available in a few days in TeX Live and MiKTeX. If you don't want to wait: the updated version (v0.19) is also available here: https://bitbucket.org/cgnieder/exsheets/commits/tag/v0.19

Full example:

\documentclass{article}
\usepackage{exsheets}[2015/07/04]
\usepackage{tcolorbox}
\usepackage{showframe} % just for showing page dimensions

\SetupExSheets{
question/pre-hook = \begin{tcolorbox} ,
question/post-hook = \end{tcolorbox} ,
solution/pre-hook = \begin{tcolorbox} ,
solution/post-hook = \end{tcolorbox} ,
no-skip-below ,
}

join  = { title[r,B]number[l,B](.333em,0pt) } ,
attach = {
main[l,vc]title[l,vc](0pt,0pt) ;
main[r,vc]points[l,vc](\marginparsep+5.5mm,0pt) % depending on the
% colorbox the 5.5mm may need adapting
}
}

\begin{document}

\begin{question}{2}
This is sample question 1.
\end{question}

\begin{solution}
This is sample solution 1.
\end{solution}

\begin{question}{1}
This is sample question 2.
\end{question}

\begin{solution}
This is sample solution 2.
\end{solution}

\begin{question}[print=false]
This is sample question 3 -- not printed.
\end{question}

\printsolutions

\end{document}


BTW: your usage of \newenvironment isn't really wrong. However, both exsheets' environments and tcolorbox' environments are no simple environments. exsheets uses environ internally and tcolorbox puts the contents in a box… This is the main reason exsheets offers hooks to place code before and after the questions/solutions.

• +1 for the update! I really struggled to find a way to merge both environments together, but I could not dig to deep into both package systems – user31729 Jul 4 '15 at 13:10
• Nice! Thank you for this, this is exactly what I was looking for! :-) – john.abraham Jul 4 '15 at 18:32