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?



{\begin{env1} \begin{env2}}%

{\end{env3} \end{env1}}


        This is test1

        This is test2



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*.




{\medskip\smallskip\par\noindent\textbf{Exercise (#1):}}


%***Patrick's solution:*** (doesn't seem to work, see below)
    {\csname block\endcsname \begin{exercise*}{#1}}%

{\end{solution*} \csname endblock\endcsname}

%This works. (using the 3 environments)
            First exercise

            First Solution

    %This doesn't work (Patrick's definitions with csname):



Edit 2

In case the block consists of more than one exercise, I want them to be separated by a thin line, defined by


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:


Exercise (First exercise):

This is exercise 1


Solution 1


Exercise (Second exercise):

This is exercise 2


Solution 2


  • For starters you have some typos: 'evn1' instead of 'env1'. – Count Zero Oct 6 '11 at 10:18
  • Was that the critical part? – Andro Oct 6 '11 at 10:34
  • 1
    A remark: the \csname .. \endcsname is only necessary because of the number in the command name. With the environment block you can write just block and \endblock. It has no influence on nesting / your problem. – topskip Oct 6 '11 at 13:12
  • @Patrick: Oh, ok. I didn't know that – Andro Oct 6 '11 at 13:52

Your test1 and test2 environments must be used one after the other, so probably another strategy is better:



Contents for env2
Contents for env3

Possible text to be set between env2 and env3 can be specified as argument to \testbreak, with a suitable definition.


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.


|improve this answer|||||
  • Thanks, but I'd like to keep the 3 entities as environments. See the edit in my message – Andro Oct 6 '11 at 13:03
  • @Andro See edited answer – egreg Oct 6 '11 at 13:18
  • Thanks. That looks good! I also want to know if it can automatically implement a functionality I wanted to use (I planned to do it manually, but if automatism is possible I would prefer it). See my edited message – Andro Oct 6 '11 at 13:59
  • @Andro Define a \newexercise macro, along the lines of \solution; you'll need a new conditional. – egreg Oct 6 '11 at 21:42

This is not recommended, I assume, as it does no proper environment initialization/checking. Some LaTeX experts could explain what is missing.


\newenvironment{env1}{(begin env1)}{(end env1)}
\newenvironment{env2}{(begin env2)}{(end env2)}
\newenvironment{env3}{(begin env3)}{(end env3)}

{\csname env1\endcsname \begin{env2}}%

{\end{env3} \csname endenv1\endcsname}


        This is test1

        This is test2


The \csname env1\endcsname construct builds a control sequence \env1 where 1 is part of the macro name. When you've got numbers in a macro name, you have to make special arrangements to create a macro name.

|improve this answer|||||
  • Nice touch, but I still don't see how env1 and endenv1 relate to each other. What I see here is still nesting in the most traditional sense. – Count Zero Oct 6 '11 at 11:23
  • @CountZero I just want to solve the problem. If Andro had provided more information I could have written something about whether this approach solves his problem or not. There is nothing special I want to show. (Or perhaps I don't get your comment right.) – topskip Oct 6 '11 at 11:32
  • @Patrick thanks, I tried to apply it but it doesn't seem to work. See the edit in my message, with details of my actual code. – Andro Oct 6 '11 at 13:03

You can't nest environments that way. An environment has to have a \begin{env-name} and an \end{env-name} statement. You broke that rule when you separated the beginning and end of env1 across test1 and test2. This is not permitted, because by defining test1 and test2 you define two new scopes. Once you use an environment inside them, you also have to finish them within the same scope. This is a basic programming rule; unfortunately for you it's impossible to break an environment up like that.

|improve this answer|||||
  • 3
    Basic programming rules applying to TeX? I wish this could be true. – topskip Oct 6 '11 at 10:24
  • @Patrick: And why wouldn't that be true? Where do you start a procedure/function/environment/etc. without closing it? – Count Zero Oct 6 '11 at 10:49
  • Is there a way around this, maybe with an appropriate package, or organizing it differently? My goal was to create a macro that automatically "appends" env1 as the container without writing it explicitly every time. – Andro Oct 6 '11 at 10:51
  • @CountZero you can use e.g. \begingroup in one definition and \endgroup in another definition. I don't think TeX's programming language can be compared to any other well known language. – topskip Oct 6 '11 at 11:04
  • How could one nest align environment within tabular environment? – Royi Mar 31 '18 at 14:58

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.