# Macros that contain catcode 1 and 2

I would like to create a macro for keeping lines together. I have read unbreakable block and other posts like it. What I have found to work best is the simple use of \vbox; A MWE is below.

\documentclass[12pt]{book}
\usepackage{pgffor}

\begin{document}

\foreach \n in {0,...,34}{% also works for 33
\par text line
}

\vspace{15pt}
\vbox{
Keep these lines together\\
Keep these lines together\\
Keep these lines together
}

\foreach \n in {0,...,33}{
\par text line
}

\end{document}


The problem is my latex file is automatically generated by a separate process that I have no access to. However, there are hooks in the code like in the following example, which does not run.

\documentclass[12pt]{book}
\usepackage{pgffor}

\newcommand{\blockStart}{
\vspace{15pt}
\vbox{
}

\newcommand{\blockStop}{
}
}

\begin{document}

\foreach \n in {0,...,34}{
\par text line
}

\blockStart
Keep these lines together\\
Keep these lines together\\
Keep these lines together
\blockStop

\foreach \n in {0,...,33}{
\par text line
}

\end{document}


My task is to define macros for \blockStart and \blockStop that make this code work like the first example. The problem is the hanging braces in the \newcommand lines. I am not looking for an alternative to \vbox; it does exactly what I want with no extra spacing issues. I am looking for a way to define these macros.

Some TeX primitives (e.g., \vbox, \hbox) can be used with implicit braces: \bgroup and \egroup:

\documentclass[12pt]{book}
\usepackage{pgffor}

\newcommand{\blockStart}{%
\vspace{15pt}%
\vbox\bgroup
}

\newcommand{\blockStop}{%
\egroup
}

\begin{document}

\foreach \n in {0,...,34}{
\par text line
}

\blockStart
Keep these lines together\\
Keep these lines together\\
Keep these lines together
\blockStop

\foreach \n in {0,...,33}{
\par text line
}

\end{document}


This is also, how environment lrbox works (\hbox + \bgroup and \egroup). However, \bgroup and \egroup cannot replace curly braces (category code tokens 1 and 2) for macro arguments.

This works:

\documentclass[12pt,letterpaper]{book}
\usepackage{pgffor}

\newcommand{\blockStart}{\begin{samepage}}
\newcommand{\blockStop}{\end{samepage}}

\begin{document}

\foreach \n in {0,...,35}{
\par text line
}

\blockStart
Keep these lines together\\
Keep these lines together\\
Keep these lines together
\blockStop

\foreach \n in {0,...,33}{
\par text line
}

\end{document}

• samepage has different spacing than vbox. Thanks, though. – louis Aug 21 '16 at 20:49