I have a problem with nesting environments. I have created a MWE for this.
I have environment {env1} which I want to contain environments {env2} and {env3} in the following way. What's wrong with the following code?
\documentclass{article}
\newenvironment{env1}{}{}
\newenvironment{env2}{}{}
\newenvironment{env3}{}{}
\newenvironment{test1}%
{\begin{env1} \begin{env2}}%
{\end{env2}}
\newenvironment{test2}%
{\begin{env3}}%
{\end{env3} \end{env1}}
\begin{document}
\begin{test1}
This is test1
\end{test1}
\begin{test2}
This is test2
\end{test2}
\end{document}
Edit:
It seems that in order to solve this problem I need to give the details of my lengthy code… (wanted to avoid it because of the length) So here it is:
I tried to apply Patrick's patch, as it looked closest to what I wanted. Unfortunately it doesn't work.
In my code,
env1=block, env2=exercise*, env3=solution*, test1=exercise, test2=solution. The "exercise" and "solution" macros are designed to ease writing when the block of exercises consists of only 1 exercise and 1 solution, and it automatically build the block around the exercise* and solution*.
\documentclass{article}
\usepackage{color}
\newcommand{\thickline}{\noindent\textcolor{blue}{\rule{\textwidth}{5pt}}}
\newenvironment{block}
{\bigskip\par\thickline\nopagebreak\par%
\begin{minipage}{\dimexpr\textwidth-\parindent\relax}\small}
{\end{minipage}\par\smallskip\nopagebreak\thickline\bigskip}
\newenvironment{exercise*}[1]
{\medskip\smallskip\par\noindent\textbf{Exercise (#1):}}
{\bigskip}
\newenvironment{solution*}
{\par\noindent\textbf{Solution:}}
{\medskip\smallskip}
%***Patrick's solution:*** (doesn't seem to work, see below)
\newenvironment{exercise}[1]%
{\csname block\endcsname \begin{exercise*}{#1}}%
{\end{exercise*}}
\newenvironment{solution}%
{\begin{solution*}}%
{\end{solution*} \csname endblock\endcsname}
%**************
\begin{document}
%This works. (using the 3 environments)
\begin{block}
\begin{exercise*}{hmmm}
First exercise
\end{exercise*}
\begin{solution*}
First Solution
\end{solution*}
\end{block}
%This doesn't work (Patrick's definitions with csname):
\begin{exercise}{hmmm}
exercise
\end{exercise}
\begin{solution}
solution
\end{solution}
%************
\end{document}
Edit 2
In case the block consists of more than one exercise, I want them to be separated by a thin line, defined by
\newcommand{\thinline}{\noindent\textcolor{red}{\rule{\textwidth}{2pt}}}
and the thick lines would come at the beginning and end of the block.
So if there are 2 exercises in the block, for example, it would look like:
\thickline
Exercise (First exercise):
This is exercise 1
Solution:
Solution 1
\thinline
Exercise (Second exercise):
This is exercise 2
Solution:
Solution 2
\thickline
Best Answer
Your
test1
andtest2
environments must be used one after the other, so probably another strategy is better:Possible text to be set between
env2
andenv3
can be specified as argument to\testbreak
, with a suitable definition.EDIT:
With your example at hand, the proposal is
Exercises can then be typeset in the
exercise
environment, where\solution
(which may not be present) will start the solution.