2

I'm trying to create an environment that will close a multicols group, do a thing, then start a new multicols.

Something like

\newcount{\colcount}
\newenvironment{balancedbreak}{%
\let\colcount\col@number
\end{multicols}\newpage\begin{multicols}{\colcount}
}{
}

This does not work, I get the error message

! LaTeX Error: \begin{balancedbreak} on input line 38 ended by \end{multicols}

MWE

This builds.

\documentclass{article}
\usepackage{multicol}
\usepackage{lipsum}

\newcount{\colcount}
\newenvironment{balancedbreak}{%
\let\colcount\col@number%
%\end{multicols}\newpage\begin{multicols}{\colcount}%
\newpage
}{%
}

\begin{document}
\begin{multicols}{2}
\lipsum[1]
\lipsum[2]
\begin{balancedbreak}
\textbf{Testing here}
\end{balancedbreak}
\lipsum[3]
\lipsum[4]
\end{multicols}
\end{document}

I get one long unbalanced column, then a new page and balanced columns. If I uncomment the line starting \end{multicols} in the new environment and comment out the next one I get the error message above.

I'm curious about the general case: "how do I end an environment in the 'before' section of a \newenvironment?

(My immediate need is "how can I better achieve the goal of ending a multicols page so it balances and move to the next page?". I could simply not do it at all the way I am now, but I'd like to use an environment for other reasons.)

3

It would be a whole lot easier to do this with a macro. With an environment you have to keep track of \@currenvir and to be in the right group. The \endgroup and \begingroup are to terminate and restart the blanacedbreak environment.

\documentclass{article}
\usepackage{multicol}
\usepackage{lipsum}
%\tracingmacros=1

\makeatletter
\newcount{\colcount}
\newenvironment{balancedbreak}{%
\global\colcount=\col@number% current value will otherwise be lost
\endgroup\endmulticols\newpage
\multicols\colcount\begingroup
\def\@currenvir{balancedbreak}%
}{}%
\makeatother

\begin{document}
\begin{multicols}{2}
\lipsum[1]
\lipsum[2]
\begin{balancedbreak}
\textbf{Testing here}
\end{balancedbreak}
\lipsum[3]
\lipsum[4]
\end{multicols}
\end{document}
| improve this answer | |
  • I considered a macro (and found tex.stackexchange.com/questions/59166/…), but I became interested in this as a general case problem. I have reasons (that I won't bore you with) for wanting to use an environment here... but I suppose if I'm messing with the native grouping aspects I'm not really using an environment, am I? – Keith Davies Jan 23 '17 at 4:48
  • This does what I'd asked, so I'm selecting it as the answer. Turns I asked how to do a foolish thing and can't use it for my intended purpose, but this does do what I'd asked. – Keith Davies Jan 29 '17 at 19:23
1

Further research and experimentation show that the new environment is not just a 'wrapper for stuff', but actually has structural meaning (creates a implicit group). That is,

\newcount\colcount
\newenvironment{balancedbreak}{%
\let\colcount\col@number%
\end{multicols}\newpage\begin{multicols}{\colcount}%
}{%
}

acts something like

  • \begin{balancedbreak}
    • \begingroup
      • \let\colcount\col@number
      • \end{multicols}\newpage\begin{multicols}{\colcount}

I'd thought -- incorrectly, it seems -- that the 'before' was something like a text substitution. I'd forgotten the implicit begingroup.

It turns out that using the definition above,

\balancedbreak
Some content goes here
\endbalancedbreak

Does appear to work.

Current working solution:

\newcount\colcount
\newenvironment{balancedbreak}{%
\ifnum\doublecol@number>\z@ % if multicol
  \let\colcount\col@number%
  \end{multicols}\newpage\begin{multicols}{\colcount}%
\else%
  \newpage%
\fi
}{%
}

This works if and only if there is no other \begin{someenv} between the \begin{multicols} and the \balancedbreak. There can be a \begin{someenv} some stuff goes here \end{someenv} without a problem, but this cannot happen 'inside' another environment or group.

This is way more brittle than really makes me happy, and I don't know that I'd recommend it as a general solution. That said, understanding how it works and why, I think I can live with it. I am, of course, open to other solutions.

| improve this answer | |
  • I believe if you set the number of columns to 3 it will fail. \begin{multicols}{0} defaults to 2 columns. You also need \makeatletter ... \makeatother to use any macro name that includes @. – John Kormylo Jan 23 '17 at 15:11
  • heh, I'd noticed that myself when I saw the error message. \global\colcount=\col@number solved that. And I'm doing this in a package, so the @ is just fine (in preamble it wouldn't be). – Keith Davies Jan 23 '17 at 15:26

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.