# Custom environment problems using environ

As shown by my MWE, I have two small issues that I'd like to get assistance with in fixing.

First off: When I start two problem environments one after another without a blank space between the \end{problem} and \begin{problem}, my second problem goes in the right margin instead of beneath the previous one.

Secondly, I'd like it, if possible, to not have a blank space (as shown in the 4th problem) if I choose to omit a problem description. I'd like to do as in problem 5, but as it is now, the first letter shows up incorrectly as the problem title.

\documentclass{article}

\usepackage{environ}
\usepackage{array}

\newcounter{problem}
\setcounter{problem}{0}
\newlength{\mylen}
\settowidth{\mylen}{100}

\NewEnviron{problem}[1]{%
\stepcounter{problem}\noindent
\begin{tabular}{m{\mylen}p{\dimexpr\linewidth-\mylen-3\tabcolsep\relax}@{}}
\arabic{problem}. & \textbf{#1}\\[1mm]
& \BODY
\end{tabular}}%

\begin{document}
\begin{problem}{foobar}
test
\end{problem}
\begin{problem}
test
\end{problem}

\bigskip\bigskip\bigskip

\begin{problem}{foobar}
test
\end{problem}

\begin{problem}{}
test2
\end{problem}

\begin{problem}
test
\end{problem}
\end{document}

-

You need to add \par to the definition avoid the need to force a new paragraph in the document. Alsoe use \refstepcounter so that you can \label your problems.

There is no need here to grab the environment body as a macro, so I reverted to a standard environment definition, and as you want the title to be optional, I declared it so defaulting to empty, which means that it takes [] not {} syntax.

\documentclass{article}

%\usepackage{environ}
\usepackage{array}

\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
%  \arabic{problem}. & \ifx\relax#1\relax \\ \else\textbf{#1}\\[1mm]\fi  &
}
{\end{tabular}\ignorespacesafterend}%

\begin{document}
\begin{problem}[foobar]
test
\end{problem}
\begin{problem}
test
\end{problem}

\bigskip\bigskip\bigskip

\begin{problem}[foobar]
test
\end{problem}

\begin{problem}[]
test2
\end{problem}

\begin{problem}
test
\end{problem}
\end{document}

-
That does half to fix my issue... it removes the issue with the side by side environments... but it doesn't solve the issue with problem 5; I'd like the "test" to be in the line above where it is currently blank. That is, when I omit a problem name, I would like the description to begin on the same line, and only have the description move down a line if I include a problem name. Not sure if I explained myself properly or not. – agent154 Feb 7 '13 at 17:17
I would need some kind of conditional to tell whether #1 is defined in the environment, and if its not, to not include the \textbf{#1}\\[1mm] part... – agent154 Feb 7 '13 at 17:21
test added as requested – David Carlisle Feb 7 '13 at 17:28
It's still not working... the paragraph is starting on a new line even though there's no optional argument there. – agent154 Feb 7 '13 at 17:40
There we go... It's working great now, thanks. – agent154 Feb 7 '13 at 17:44