9

I would like to define an environment having two parts, i.e.

 \begin{myenv}
     some text
 \nextpart{myenv}
     some more
 \end{myenv}

I think I could do this by exploring the definition of \begin, \end, and \newenvironment, but I wonder if somebody has already done so.

4
  • We will need to know a bit more about what you want to achieve: I don't really see what the 'two parts' mean here.
    – Joseph Wright
    Feb 27, 2013 at 9:03
  • Maybe you could define you environment, and inside the definition, add a switch (\nextpart) that will change stuff in your environment (IMO, you do not need to repeat "myenv"). I think that the command \appendix work like that in the environment "document"... Feb 27, 2013 at 9:07
  • what type of features you would like the "\nextpart{} some more" have.eg bold,italic etc.. What is the differentiating feature between the two parts ? Feb 27, 2013 at 9:13
  • Thank you all for your comments and suggestions. I was asking the question in general, but it arose when trying to do a "side-to-side text" environment. Concretely, I am typesetting a draft revision of the articles of association of some organisation, and in some parts, I want to present two versions side-to-side (people in the organisation have submitted contradictory amendments and we need to vote on this). So the two parts are the two conflicting versions, and they contain complex things (\par, \label, \ref, etc.)
    – YS1
    Mar 2, 2013 at 19:18

1 Answer 1

12

I have done something similar; in my experience, it was an environment that has

  1. a question
  2. a proof / answer

Here's an example:

\documentclass{article}
\usepackage{amsthm}

\newenvironment{questionanswer}{
  \newcommand{\nextpart}{\end{quote}\begin{proof}}
  \begin{quote}%
  }{%
  \end{proof}%
}

\begin{document}
\begin{questionanswer}
  Something to be quoted.
  \nextpart
  Something to be proven.
\end{questionanswer}
\end{document}

To my understanding, this is expanded to exactly

\begin{document}
\begin{quote}
  Something to be quoted.
  \end{quote}\begin{proof}
  Something to be proven.
\end{proof}
\end{document}

Note that by using \renewcommand, this can be arbitrarily complex:

\documentclass{article}
\usepackage{amsthm}
\usepackage{amsmath}

\newenvironment{questionanswer}{%
  \newcommand{\nextpart}{\end{quote}\begin{proof}%
    \renewcommand{\nextpart}{\end{proof}\begin{itemize}%
    \renewcommand{\nextpart}{\end{itemize}\begin{enumerate}%
    \renewcommand{\nextpart}{\end{enumerate}\begin{equation}%
  }}}}% As you can see, this can be arbitrarily complex
  % (just be careful of your braces!)
  \begin{quote}%
}{%
  \end{equation}%
}

\begin{document}
Some normal text.
\begin{questionanswer}
  Something to be quoted.
  \nextpart
  Something to be proven.
  \nextpart
  \item one
  \item two
  \nextpart
  \item enum a
  \item enum b
  \nextpart
  \sum_0^\infty a_n := \lim_{n\to\infty}\sum_0^n a_n
\end{questionanswer}
\end{document}

Complex output

This works by defining \nextpart to both

  1. change the environment
  2. change the definition of \nextpart

at each invocation. If \nextpart were defined outside of the questionsanswer environment, there'd be no way to reset \nextpart to the value it should have at the beginning of each cycle.

4
  • Thanks for your suggestion ; this is what I am doing currently, except the two sub-environments are of the same kind right now. What I am wondering is: does this nest nicely ?
    – YS1
    Mar 2, 2013 at 19:23
  • @YS1 It's certainly extensible (see edit), but I'm not sure what you mean by 'nesting' -- could you edit your question with an ideal use-case and what it should do? It's probably possible, I just don't really know what you'd use it for (and it might be quite awkward because of recursion, if I understand you correctly). Mar 2, 2013 at 21:41
  • @Sean Allred Why is the macro \nextpart defined inside the difination of the environment questionanswer, not outside?
    – lyl
    Sep 2, 2022 at 14:51
  • @lyl It's been many years since my macro programming days -- you might consider asking this as a fresh follow-up question :-) you can just link back to this answer to ask the more general question of 'why would you define commands in other commands'. For this specific case, I'll add some explanation to my post above. Sep 3, 2022 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .