2

I would like to use \newenvironment in my style file. It compiles perfectly in the TeX file but does not work in the style file (aka package). I suspect it's similar to the inability of using \newcommand in place of \renewcommand in a 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}
  • Welcome to TeX.SE. What do you mean by inability of \newcommand in place of \renewcommand? .sty are full of \newcommand statements in many cases. And yes, a MWE would be useful. Is your environment existing at all, then you have to use \renewenvironment, for example. (Or use the environ or xparse packages and their alternatives to \renewenvironment) – user31729 Dec 29 '17 at 23:41
  • 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
2

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}

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.