1

I have a rather large document with the following structure

\newenvironment{A}{sth.}{sth.}
\newenvironment{B}{sth.}{sth.}
\newenvironment{C}{sth.}{sth.}
\begin{A}
\begin{B}
\end{B}
\begin{C}
\end{C}
\begin{B}
\end{B}
\begin{C}
\end{C}
\end{A}
\begin{A}
\begin{B}
\end{B}
\begin{C}
\end{C}
\begin{B}
\end{B}
\begin{C}
\end{C}
\end{A}

Now I want to prevent the environments B and C to be separated, either on a column break or a page break. They always are paired such that after environment B always an environment C follows. One solution is to put both environments into one, i.e. instead of having \begin{B} \end{B} \begin{C} \end{C} I would have

\begin{D}
\end{D}

but I would prefer not to do this due to already having quite a lot of text written in that scheme. Another possibility would be to open a minipage in environment B, and close it in C, but that results in ! LaTeX Error: \begin{minipage} on input line 21 ended by \end{C}. Is there another solution for that? Or should I rather rewrite the whole document?

  • It seems like this shouldn't be too hard with a find-replace: Find \begin{B}, replace \begin{D}, find \end{C}, replace \end{D}, find \end{B}\n\begin{C}, replace '' – darthbith Apr 2 '16 at 16:19
2

Here is an idea.

First, the originale definition of A, B and C saved in A, BB and Cc

\newenvironment{A}{sth.}{sth.}
\newenvironment{BB}{sth.}{sth.}
\newenvironment{CC}{sth.}{sth.}

Then new environment B and C to keep BB and CC in same box.

\newsavebox\BC
\newenvironment{B}{%
\global\setbox\BC\vbox\bgroup\begin{BB}}{%
\par\xdef\mtprevdepth{\the\prevdepth}
\end{BB}\egroup}
\newenvironment{C}{%
\setbox\BC\vbox\bgroup\unvbox\BC\prevdepth\mtprevdepth\begin{CC}}{%
\end{CC}\egroup\box\BC}

Update: here is another idea using etoolbox package

\usepackage{etoolbox}

\newenvironment{A}{\textbf{A}sth.}{sth.}
\newenvironment{B}{\textbf{B}sth.}{sth.}
\newenvironment{C}{\textbf{C}sth.}{sth.}

\BeforeBeginEnvironment{B}{\begin{minipage}{\linewidth}}
\AfterEndEnvironment{C}{\end{minipage}}
  • Using the etoolbox-version was what I am looking for, due to already using other commands in this context (btw., I could have thought about that, too...) – arc_lupus Apr 2 '16 at 20:03

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.