# How to use \newenvironment in style file?


Sorry this is my first time posting a question.

MWE

I would love to have the \newenvironment{problem} along with \newcounter{problem} in the style file named stylesheet.sty.

\documentclass{article}
\usepackage{stylesheet}
\newcounter{problem}[section] % new counter for problems
\newenvironment{problem}{%
% new environment for problems
\refstepcounter{problem}\par\medskip
\textbf{(\arabic{section}\alph{problem})}%
}

\begin{document}

\section{Problem Title}
This is a dummy text for the first problem. % example text

\begin{problem}
This is a dummy text for the first part of problem 1 with a dummy equation.
\end{problem}

\end{document}

• The environment is not existing. See MWE – Keto Dec 29 '17 at 23:50
• You're missing the {} after the {} part of the environment. – user31729 Dec 29 '17 at 23:51
• Yes, thank you. That was so obvious. I usually forget the post environment brackets. I'm surprise that doesn't fail in TeX file but fails in style file. – Keto Dec 29 '17 at 23:54
• It won't work within the usual .tex file as well if there is \begin{document} right after the missing {} pair, i.e. without white space – user31729 Dec 29 '17 at 23:58

The

\newenvironment{problem}{%
% new environment for problems
\refstepcounter{problem}\par\medskip
\textbf{(\arabic{section}\alph{problem})}%
}


is missing the environment's end code section, i.e. the proper syntax is

\newenvironment{foo}[number of arguments]{start code}{end code}


(Of course, there may be no arguments at all)

In a document, it is working by change if there is white space after start code, but in a package with no white space or other content directly after it, it will prove fatal.

\ProvidesPackage{stylesheet}

\newcounter{problem}[section] % new counter for problems
\newenvironment{problem}{%
% new environment for problems
\refstepcounter{problem}\par\medskip
\textbf{(\arabic{section}\alph{problem})}%
}{%
%End code
}

\endinput


Driver file:

\documentclass{article}

\usepackage{stylesheet}
\begin{document}

\section{Problem Title}
This is a dummy text for the first problem. % example text

\begin{problem}
This is a dummy text for the first part of problem 1 with a dummy equation.
\end{problem}

\end{document}