# Defining tabular \newenvironment with internal \newenvironments?

Motivation: I have developed a template to do engineering homework problems (See this question), and am now trying to encapsulate the table formating code for problems and their subsections into a \newenvironment.

The Error: When I run the appended M(N)WE, I get an error message:

! Extra }, or forgotten \endgroup.
<template> \unskip \hfil }
\hskip \tabcolsep \endtemplate
l.24        \begin{given}
The weights of the various components of the truck are ...


Question:

1. Am I using this \newenvironment command the wrong way?
2. Is it not possible to include the & and \\ table formating commands in a \newenvironment?
3. What is the right way to accomplish the essence of what I am trying to do?

Attempts: So far, I have tried the following:

• Placing the formating commands outside the \newenvironment, where I would want the macro to place them.
• Using the environ package.
• Using tabu instead of tabular
• Using \tabular and \endtabular instead of \begin{tabular} and \end{tabular}

All to no avail.

Appended M(N)WE: This is the style I am trying to get to; however, it produces the error I referenced above.

\documentclass[10pt]{article}

\usepackage{units}

\newenvironment{problem}{
\begin{tabular}{l p{5.5in}}
}{
\end{tabular}
\pagebreak
}

\newenvironment{given}       { \textbf{Given:}       \vspace{0.125in}   & }{\\}
\newenvironment{sketch}     { \textbf{Sketch:}      \vspace{0.125in}   &} {\\}
\newenvironment{equations}  { \textbf{Equations:}   \vspace{0.125in}   &} {\\}
\newenvironment{solution}   { \textbf{Solution:}     \vspace{0.125in}  &} {\\}

\begin{document}
\pagestyle{empty}

\begin{problem}
\begin{given} The weights of the various components of the truck are show in the sketch below. \end{given}
\begin{asked} Replace this system of forces by an equialent resultant force and specify its location measured from $B$. \end{asked}
\begin{sketch} \begin{center} PUT PICTURE HERE \end{center} \end{sketch}
\begin{equations}
$$\sum F_{x_I} = \sum F_{x_{II}}$$
$$\sum F_{y_I} = \sum F_{y_{II}}$$
$$\sum M_{o_I} = \sum M_{o_{II}}$$

\vspace{.0625 in}

$$M_o = F\times d_\perp$$
\end{equations}
\begin{solution}
$$\sum F_x = F_{x_1} + F_{x_2} + F_{x_3} = 0$$
$$\sum F_y = F_{y_1} + F_{y_2} + F_{y_3} = \unit[3500]{lb} + \unit[5500] {lb} + \unit[1750]{lb} = \unit[10\;750]{lb}$$

\vspace{.0625 in}

$$\sum M_B = M_1 + M_2 + M_3$$
\begin{aligned} M_1 &= F\times d_\perp = \unit[3500]{lb}(\unit[3]{ft}) &= \unit[10\;500]{ft\cdot lb} \\ M_2 &= F\times d_\perp = \unit[5500]{lb}(\unit[3]{ft}+\unit[14]{ft}) &= \unit[93\;500]{ft\cdot lb} \\ M_3 &= F\times d_\perp = \unit[1750]{lb}(\unit[3]{ft}+\unit[14]{ft}+\unit[6]{ft}+\unit[2]{ft}) &= \unit[43\;750]{ft\cdot lb} \vspace{.0625in} \\ M_B &= \unit[10\;500]{ft\cdot lb} + \unit[93\;500]{ft\cdot lb} + \unit[43\;750]{ft\cdot lb} &= \unit[147\;750]{ft\cdot lb} \end{aligned}
\end{solution}
\end{problem}
\end{document}


MWE: Here is a version that does not use \newenvironment, but works, and represents the document I am trying to produce.

\documentclass[10pt]{article}

\usepackage{units}

\begin{document}
\begin{tabular}{l p{5.5in}}

\textbf{Given:} \vspace{.125in} &

The weights of the various components of the truck are show in the sketch below. \\

Replace this system of forces by an equialent resultant force and specify its location measured from $B$. \\

\textbf{Sketch:} \vspace{.125in} &

\begin{center}PUT PICTURE HERE\end{center} \\

\textbf{Equations:} \vspace{.125in} &

$$\sum F_{x_I} = \sum F_{x_{II}}$$
$$\sum F_{y_I} = \sum F_{y_{II}}$$
$$\sum M_{o_I} = \sum M_{o_{II}}$$
\vspace{.0625 in}
$$M_o = F\times d_\perp$$ \\

\textbf{Solution:} \vspace{.125in} &

$$\sum F_x = F_{x_1} + F_{x_2} + F_{x_3} = 0$$
$$\sum F_y = F_{y_1} + F_{y_2} + F_{y_3} = \unit[3500]{lb} + \unit[5500] {lb} + \unit[1750]{lb} = \unit[10\;750]{lb}$$
\vspace{.0625 in}
$$\sum M_B = M_1 + M_2 + M_3$$
\begin{aligned} M_1 &= F\times d_\perp = \unit[3500]{lb}(\unit[3]{ft}) &= \unit[10\;500]{ft\cdot lb} \\ M_2 &= F\times d_\perp = \unit[5500]{lb}(\unit[3]{ft}+\unit[14]{ft}) &= \unit[93\;500]{ft\cdot lb} \\ M_3 &= F\times d_\perp = \unit[1750]{lb}(\unit[3]{ft}+\unit[14]{ft}+\unit[6]{ft}+\unit[2]{ft}) &= \unit[43\;750]{ft\cdot lb} \vspace{.0625in} \\ M_B &= \unit[10\;500]{ft\cdot lb} + \unit[93\;500]{ft\cdot lb} + \unit[43\;750]{ft\cdot lb} &= \unit[147\;750]{ft\cdot lb} \end{aligned}

\end{tabular}
\end{document}

• you can not do \newenvironment{given} { \textbf{Given:} \vspace{0.125in} & }{\\} as it is like doing {\textbf{Given:}&\\} as environments form a group, you would get similar errors. (If you really want that syntax, anything is possible but it's perhaps confusing to use environment syntax for something that is not a group. – David Carlisle Feb 5 '17 at 16:05
• Well, that unfortunately seems to answer questions 1 and 2. Any direction for 3? – Michael Molter Feb 5 '17 at 16:20
• I'll do something but not sure about the intention of \vspace{0.125in}  vspace does nothing very useful in an l cell which is horizontal mode – David Carlisle Feb 5 '17 at 16:24
• So I guessed but the fact that adding vertical space in the first cell, in horizontal mode causes space to be added at the end of the row is a more or less accidental and definitely undocumented artefact of the implementation:-) – David Carlisle Feb 5 '17 at 16:55

It can't work like that: environments form groups.

On the other hand, you're using the wrong tools, in my opinion. Rather than a tabular you should use an itemized list.

I changed unit into the more powerful siunitx package.

\documentclass[10pt]{article}
\usepackage{showframe} % just for the example
\usepackage{enumitem,amsmath}
\usepackage{siunitx}

\newlength{\problemwd}
\AtBeginDocument{\settowidth{\problemwd}{\textbf{Equations:} }}

\sisetup{inter-unit-product=\ensuremath{{}\cdot{}}}
\DeclareSIUnit{\lb}{lb}
\DeclareSIUnit{\ft}{ft}

\newenvironment{problem}
{\begin{itemize}[align=left,leftmargin=\problemwd,labelwidth=\problemwd,labelsep=0pt]}
{\end{itemize}\clearpage}

\newcommand{\given}{\item[\textbf{Given:}]}
\newcommand{\sketch}{\item[\textbf{Sketch:}]}
\newcommand{\equations}{\item[\textbf{Equations:}]}
\newcommand{\solution}{\item[\textbf{Solution:}]}

\pagestyle{empty}

\begin{document}

\begin{problem}
\given The weights of the various components of the truck are show in the sketch below.

\asked Replace this system of forces by an equivalent resultant force and specify
its location measured from $B$.
\sketch \mbox{} % for avoiding centering
\begin{center} PUT PICTURE HERE \end{center}

\equations
\begin{gather*}
\sum F_{x_I} = \sum F_{x_{II}} \\
\sum F_{y_I} = \sum F_{y_{II}} \\
\\

Solutions&
{\begin{align*}
\sum F_x &= F_{x_1} + F_{x_2} + F_{x_3} = 0\\
\sum F_y &= F_{y_1} + F_{y_2} + F_{y_3} =
\unit[3500]{lb} + \unit[5500] {lb} + \unit[1750]{lb} = \unit[10\;750]{lb}
\end{align*}}

\vspace{.0625 in}

{\begin{align*}
\sum M_B = M_1 + M_2 + M_3\\
M_1 &= F\times d_\perp = \unit[3500]{lb}(\unit[3]{ft}) &= \unit[10\;500]{ft\cdot lb} \\
M_2 &= F\times d_\perp = \unit[5500]{lb}(\unit[3]{ft}+\unit[14]{ft}) &= \unit[93\;500]{ft\cdot lb} \\
M_3 &= F\times d_\perp = \unit[1750]{lb}(\unit[3]{ft}+\unit[14]{ft}+\unit[6]{ft}+\unit[2]{ft}) &= \unit[43\;750]{ft\cdot lb} \vspace{.0625in} \\
M_B &= \unit[10\;500]{ft\cdot lb} + \unit[93\;500]{ft\cdot lb} +  \unit[43\;750]{ft\cdot lb} &=  \unit[147\;750]{ft\cdot lb}
\end{align*}}

\end{problem}
\end{document}


I would do it as a description environment with specialised items (\Given, \Asked,&c.), put in an \eqparbox so they all use the largest width of them. In addition, I replaced the many $$…$$, which you should not use with latex, with the relevant amsmath environments. Note this environment can break across pages.

\documentclass[10pt]{article}

\usepackage{amsmath, units}
\usepackage{enumitem, eqparbox}%%%
\newlength\itemwd
\settowidth{\itemwd}{\textbf{Equations}}
\newcommand\Given{\item[\eqparbox{Pb}{Given: }]\leavevmode}
\newcommand\Sketch{\item[\eqparbox{Pb}{Sketch: }]\leavevmode}
\newcommand\Eqns{\item[\eqparbox{Pb}{Equations: }]\leavevmode}
\newcommand\Sol{\item[\eqparbox{Pb}{Solution: }]\leavevmode}

\begin{document}

\begin{description}[leftmargin =\eqboxwidth{Pb},labelsep = 0pt]

\Given The weights of the various components of the truck are show in the sketch below. \\

\Asked Replace this system of forces by an equialent resultant force and specify its location measured from $B$. \\

\Sketch
\begin{center}PUT PICTURE HERE\end{center}

\Eqns \vspace{-\dimexpr\abovedisplayskip+\baselineskip\relax}
\begin{align*}\sum F_{x_I} & = \sum F_{x_{II}}\\%
\sum F_{y_I} & = \sum F_{y_{II}}\\%
\sum M_{o_I} & = \sum M_{o_{II}}
\\[2ex]
M_o & = F\times d_\perp
\end{align*}
\Sol \vspace{-\dimexpr\abovedisplayskip+\baselineskip\relax}%
\begin{gather*}\sum F_x = F_{x_1} + F_{x_2} + F_{x_3} = 0\\
\sum F_y = F_{y_1} + F_{y_2} + F_{y_3} = \unit[3500]{lb} + \unit[5500] {lb} + \unit[1750]{lb} = \unit[10\;750]{lb}\\[2ex]
\sum M_B = M_1 + M_2 + M_3\\[2ex]
\begin{alignedat}{2}
M_1 &= F\times d_\perp = \unit[3500]{lb}(\unit[3]{ft}) & &= \unit[10\;500]{ft\cdot lb} \\
M_2 &= F\times d_\perp = \unit[5500]{lb}(\unit[3]{ft}+\unit[14]{ft}) & &= \unit[93\;500]{ft\cdot lb} \\
M_3 &= F\times d_\perp = \unit[1750]{lb}(\unit[3]{ft}+\unit[14]{ft}+\unit[6]{ft}+\unit[2]{ft}) & &= \unit[43\;750]{ft\cdot lb} \\[2ex]
M_B &= \unit[10\;500]{ft\cdot lb} + \unit[93\;500]{ft\cdot lb} + \unit[43\;750]{ft\cdot lb} & & = \unit[147\;750]{ft\cdot lb}
\end{alignedat}
\end{gather*}%

\end{description}

\end{document}