# I can't use & tabs in the align environment while using it inside a custom environment

I've got a custom environment made for problem sets, and it uses a tabular to align the question number and question body. When I try to use & to group parts of an equation together - like if I want to align the = sign across multiple equations, I get compile errors. Is there any way I can fix this behavior? I'm all for getting rid of the tabular in my environment if I can use some other method to align the question body in a way that's visually equivalent to what I have now...

MWE:

\documentclass{article}

\RequirePackage{amsmath, amsthm}
\RequirePackage{thmtools}

\newcounter{problem}
\setcounter{problem}{0}

\newlength{\mylen}
\settowidth{\mylen}{100}

\newenvironment{problem}[1][]{%
\refstepcounter{problem}
\par\noindent
\begin{tabular}{m{\mylen}p{\dimexpr\linewidth-\mylen-3\tabcolsep\relax}@{}}
\arabic{problem}. & \ifx\relax#1\relax \else\textbf{#1}\\[1mm]&\fi
}
{\end{tabular}\ignorespacesafterend\medskip}%

\begin{document}
\begin{problem}
\begin{align*}
=&\int_{1}^{3}\left(x+\frac{1}{x}\right)\ dx\\
=&\left.\frac{1}{2}x^{2}+ln\lvert x\rvert\;\right|_{1}^{3}
\end{align*}
\end{problem}
\end{document}

-
You can use tabular instead of align. & is used both by align and tabular. Similarly \\ . –  Harish Kumar Feb 8 '13 at 1:50
@HarishKumar Sadly, tabular doesn't work either. But I don't want to do that - I want bo be able to use the functionality of align or gather inside of my custom environment. –  agent154 Feb 9 '13 at 23:39
Hm, it seems to me, you just want a label on the start of the line of the length of 100, and that what follows in a minipage of the remaining width. Which in turn just is a simple always resuming enumerate which can be set up with enumitem to work kind of the same way. If not you need to explain better what you want, how it should look like etc. –  Qrrbrbirlbel Mar 12 '13 at 5:18
I get compile errors no matter what I put into your 'problem' environment. Is this really an align issue? (Ping, @Qrrbrbirlbel.) –  Hendrik Vogt Mar 12 '13 at 9:44
I had forgotten about this question, actually - I had the problem solved via another post in a semi-related issue. I'll post the solution below. –  agent154 Mar 12 '13 at 12:22

Here's the solution that I got from somebody else while trying to fix another issue related to this badly designed environment. It had the effect of also fixing this issue.

I've included two extra environment definitions just to allow my example to work, since it was pulled from a homework assignment.

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{enumitem}

\newcounter{problem}
\setcounter{problem}{0}

\newenvironment{problem}[1][]{
\refstepcounter{problem}%
\begin{list}{\arabic{problem}.}
{\settowidth{\labelwidth}{100}%
\setlength{\rightmargin}{0pt}%
}
\item \ifx\relax#1\relax \else\textbf{#1}\\[1mm]\fi
}
{\end{list}}

\newenvironment{parts}
{\begin{enumerate}[label=\alph*)]}
{\end{enumerate}}

\def\qed{\ensuremath{\square}} % <-QED Symbol
\newenvironment{solution}{%  <-Solution
\itshape{Solution:}\\}

\begin{document}
\begin{problem}[Evaluate the integrals:]
\begin{parts}
\item $\displaystyle\int\sec^4{x}\ dx$
\begin{solution}
\begin{align*}
\int\sec^2{x}\ dx&=\int\sec^2{x}\sec^2{x}\ dx\\
&=\int\left(\tan^2{x}+1\right)\sec^2{x}\ dx\\
&=\int\tan^2{x}\sec^2{x}\ dx+\int\sec^2{x}\ dx\\
&=\frac{1}{3}\left[\tan^3{x}+3\tan{x}\right]+C
\end{align*}
\end{solution}
\end{parts}
\end{problem}
\end{document}


-

As proposed in my comment, here a solution (?) with enumitem.

The list qrr@problem is created with the label format \arabic*. (where * stands for the counter internally used).

The problem environment then uses the qrr@problem environment to automatically add the first (and in your case only) \item, either without or with the text given in the mandatory argument. (The optional first argument is forwarded to the qrr@problem list.)

## Code

\documentclass{article}

\RequirePackage{amsmath, amsthm}
\RequirePackage{thmtools, enumitem}

\newlist{qrr@problem}{enumerate}{1}
\setlist[qrr@problem,1]{label=\arabic*.,resume}

\makeatletter
\newenvironment{problem}[2][]{%
\qrr@problem[#1]\item\if\relax\detokenize{#2}\relax\relax\else\textbf{#2}\fi
}{%
\endqrr@problem
}
\makeatother
\begin{document}
\begin{problem}{}
\begin{align*}
y & = \int_{1}^{3}\left(x+\frac{1}{x}\right)\ dx\\
& = \left.\frac{1}{2}x^{2}+ln\lvert x\rvert\;\right|_{1}^{3}
\end{align*}
\end{problem}

\begin{problem}{Next problem, same equation}
\begin{align*}
y & = \int_{1}^{3}\left(x+\frac{1}{x}\right)\ dx \\
& = \left.\frac{1}{2}x^{2}+ln\lvert x\rvert\;\right|_{1}^{3}
\end{align*}
\end{problem}
\end{document}


## Output

-
\ln should be used. –  kiss my armpit Mar 12 '13 at 7:01