动机:我已经开发了一个模板来解决工程作业问题(参见这个问题),现在正尝试将问题及其子部分的表格格式化代码封装到一个\newenvironment
.
错误:当我运行附加的 M(N)WE 时,收到一条错误消息:
! Extra }, or forgotten \endgroup.
<template> \unskip \hfil }
\hskip \tabcolsep \endtemplate
l.24 \begin{given}
The weights of the various components of the truck are ...
问题:
- 我是否以
\newenvironment
错误的方式使用此命令? - 是否不能在中包含
&
和表格格式化命令?\\
\newenvironment
- 完成我正在尝试做的事情的本质的正确方法是什么?
尝试:到目前为止,我已经尝试了以下方法:
- 将格式化命令放在外面
\newenvironment
,我希望宏将它们放置在那里。 - 使用该
environ
包。 - 使用
tabu
而不是tabular
- 使用
\tabular
and\endtabular
代替\begin{tabular}
and\end{tabular}
但一切都无济于事。
附加 M(N)WE:这是我想要的风格;但是,它会产生我上面提到的错误。
\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}
平均能量损失:这是一个不使用\newenvironment
但可以工作的版本,它代表了我想要制作的文档。
\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}
答案1
它不能这样工作:环境形成群体。
另一方面,我认为你使用的工具不对。tabular
你应该使用逐项列表,而不是列表。
我换成unit
了更强大的siunitx
套装。
\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}
请注意,盒子是由于showframe
;移除包装,它就会消失。
请避免$$
使用 LaTeX。
通过小小的修改,您甚至可以为环境提供可选的字体大小参数,以适应特别长的问题。
\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}
答案2
我会放弃 table 并用其他东西代替它,例如 minipage。以下是一个例子:
\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}
答案3
外部tabular
并没有真正的帮助,但如果你想要一个表格,你可以做这样的事情,如果你嵌入表格中,使用环境语法对部分进行处理会特别棘手,因为每个单元格都是一个单独的组,并且你在其中一个单元格中开始一个环境并在另一个单元格中结束。
\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}
答案4
我会将其作为一个description environment
带有专门项目(\Given
、\Asked
等)的 ,放入一个 ,\eqparbox
这样它们都会使用其中的最大宽度。此外,我用$$ … $$
相关环境替换了许多 ,您不应该在 latex 中使用它们amsmath
。请注意,此环境可能会跨页面中断。
\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}