3

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{asked}      { \textbf{Asked:}       \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. \\

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

        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}
  • 2
    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
  • 1
    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
  • 1
    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
  • 1
    @MichaelMolter: See Why is \[\] preferable to $$? – Werner Feb 5 '17 at 17:50
4

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{\asked}{\item[\textbf{Asked:}]}
\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}} \\
  \sum M_{o_I} = \sum M_{o_{II}} \\[2ex]
  M_o = F\times d_\perp
\end{gather*}

\solution
\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} =
    \SI{3500}{\lb} + \SI{5500}{\lb} + \SI{1750}{\lb} = \SI{10750}{\lb} \\[2ex]
  \sum M_B = M_1 + M_2 + M_3 \\
  \begin{alignedat}{2}
    M_1 &= F\times d_\perp = \SI{3500}{\lb}(\SI{3}{\ft})
      &&= \SI{10500}{\ft\lb} \\
    M_2 &= F\times d_\perp = \SI{5500}{\lb}(\SI{3}{\ft}+\SI{14}{\ft})
      &&= \SI{93500}{\ft\lb} \\
    M_3 &= F\times d_\perp = \SI{1750}{\lb}(\SI{3}{\ft}+\SI{14}{\ft}+\SI{6}{\ft}+\SI{2}{\ft})
      &&= \SI{43750}{\ft\lb} \\
    M_B &= \SI{10500}{\ft\lb} + \SI{93500}{\ft\lb} + \SI{43750}{\ft\lb}
      &&= \SI{147750}{\ft\lb}
  \end{alignedat}
\end{gather*}

\end{problem}

\end{document}

Note that the box is due to showframe; remove the package and it will go away.

Please, avoid $$ in LaTeX.

enter image description here

With a small modification, you can even give an optional font size argument to the environment, in order to accommodate a particularly long problem.

\documentclass[10pt]{article}
\usepackage{showframe}
\usepackage{enumitem,amsmath}
\usepackage{siunitx}

\newlength{\problemwd}

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

\newenvironment{problem}[1][\normalsize]
 {#1\settowidth{\problemwd}{\textbf{Equations:} }%
  \begin{itemize}[align=left,leftmargin=\problemwd,labelwidth=\problemwd,labelsep=0pt]}
 {\end{itemize}\clearpage}

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

\pagestyle{empty}

\begin{document}

\begin{problem}[\footnotesize]
\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{}
        \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}} \\
  \sum M_{o_I} = \sum M_{o_{II}} \\[2ex]
  M_o = F\times d_\perp
\end{gather*}

\solution
\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} =
    \SI{3500}{\lb} + \SI{5500}{\lb} + \SI{1750}{\lb} = \SI{10750}{\lb} \\[2ex]
  \sum M_B = M_1 + M_2 + M_3 \\
  \begin{alignedat}{2}
    M_1 &= F\times d_\perp = \SI{3500}{\lb}(\SI{3}{\ft})
      &&= \SI{10500}{\ft\lb} \\
    M_2 &= F\times d_\perp = \SI{5500}{\lb}(\SI{3}{\ft}+\SI{14}{\ft})
      &&= \SI{93500}{\ft\lb} \\
    M_3 &= F\times d_\perp = \SI{1750}{\lb}(\SI{3}{\ft}+\SI{14}{\ft}+\SI{6}{\ft}+\SI{2}{\ft})
      &&= \SI{43750}{\ft\lb} \\
    M_B &= \SI{10500}{\ft\lb} + \SI{93500}{\ft\lb} + \SI{43750}{\ft\lb}
      &&= \SI{147750}{\ft\lb}
  \end{alignedat}
\end{gather*}

\end{problem}

\end{document}

enter image description here

| improve this answer | |
  • This looks like a very clean implementation. – Michael Molter Feb 5 '17 at 17:14
  • @MichaelMolter The idea is that you only need to mark up the keys. Using \begin and \end would be possible, but surely more cumbersome. – egreg Feb 5 '17 at 17:23
2

I would give up on table and replace it by something else, for example minipage. Here is an example:

\documentclass[10pt]{article}

\usepackage{units}
\usepackage{amsmath}

\newenvironment{problem}{}{\pagebreak}

\newenvironment{field}[1]{\par\vspace{0.125in}\noindent\rlap{\textbf{#1:}}\hspace{1in}\begin{minipage}[t]{5in}}{\end{minipage}}
\newenvironment{given}{\begin{field}{Given}}{\end{field}}
\newenvironment{asked}{\begin{field}{Asked}}{\end{field}}
\newenvironment{sketch}{\begin{field}{Sketch}}{\end{field}}
\newenvironment{equations}{\begin{field}{Equation}}{\end{field}}
\newenvironment{solution}{\begin{field}{Solution}}{\end{field}}

\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}
| improve this answer | |
  • This approach seems to do the trick. Are there any consequence or benefits to using minipage in this context, other than that it just works of course! Thank you. – Michael Molter Feb 5 '17 at 16:58
  • One difference with respect to tabular is that now the parts of the problem can move to the second page. You can't break a minipage in the middle though (and if you want to you'll have to find another minipage like command like tcolorbox for that). – Sergei Golovan Feb 5 '17 at 17:02
  • 1
    Or itemize as it was suggested by @egreg. Note though that if you start an item by a display formula \item introduces an additional vertical gap. – Sergei Golovan Feb 5 '17 at 17:06
2

The outer tabular isn't really helping, but if you want a table, you could do something like this, using an environment syntax for the parts is particularly tricky if you are embedding in a table as each cell is then a separate group and you are starting an environment in one cell and ending in another.

\documentclass[10pt]{article}
\usepackage{units}
\usepackage{amsmath,array}

\newenvironment{problem}{%%
    \noindent
    \hspace{-1in}%
    \begin{tabular}{>{\bfseries}l p{5.5in}}%%
}{%%
    \end{tabular}%%
    \par
    \pagebreak
}

\begin{document}
    \pagestyle{empty}
\setlength\extrarowheight{.125in}

    \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 equialent resultant force
and specify its location measured from $B$.
\\

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

Equations&
{\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}}
\end{align*}}

            \vspace{.0625 in}

            \[M_o = F\times d_\perp\]
\\

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}
| improve this answer | |
2

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\Asked{\item[\eqparbox{Pb}{Asked: }]\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} 

enter image description here

| improve this answer | |

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.