# Environment definition gives an error

\documentclass{scrreprt}

\usepackage{fontspec}
\setmainfont{Linux Libertine O}

\usepackage{graphicx}

\usepackage{polyglossia}
\setdefaultlanguage[babelshorthands=true]{german}

% align in enumerate
\newenvironment{enumalign}[1][\textwidth]%
{\item \begin{minipage}[t]{#1} \vspace{-5.375ex} \begin{align*}}
{\end{align*} \end{minipage}}

\renewcommand*\thechapter{\arabic{chapter}}

\usepackage{color}

\newcommand*\defin[1]{\textcolor{red}{#1}}

\begin{document}

\begin{enumerate}
\begin{enumalign}
test
\end{enumalign}
\end{enumerate}

\end{document}


LaTeX is telling me \begin{align*} on input line 511 ended by \end{enumalign}. It seems to have something to do with how LaTeX interprets my code and replaces macros - but I could not get it right.

-

This is the correct code:

\documentclass{scrreprt}

\usepackage{amsmath}

\usepackage{fontspec}
\setmainfont{Linux Libertine O}

\usepackage{graphicx}

\usepackage{polyglossia}
\setdefaultlanguage[babelshorthands=true]{german}

% align in enumerate
\makeatletter
\newenvironment{enumalign}[1][\linewidth]%
{\item\minipage[t]{#1}\vspace{-5.375ex}\start@align\@ne\st@rredtrue\m@ne}
{\endalign\endminipage}
\makeatother

\renewcommand*\thechapter{\arabic{chapter}}

\usepackage{color}

\newcommand*\defin[1]{\textcolor{red}{#1}}

\begin{document}

\begin{enumerate}
\begin{enumalign}
test
\end{enumalign}
\end{enumerate}

\end{document}


Output:

First of all, you have to load amsmath if you want to use align* in your document.

Moreover, the available room for the minipage is \linewidth and not \textwidth, so I changed it.

And now, try to see why you've got that error. Nested environments often cause errors like the one you've got. To circumvent this, you have to use command-form of the environment (begin and end).

For example, the command form of the environment myenv are

\myenv     % same as \begin{myenv}
\endmyenv  % same as \end{myenv}


In your case, I've substituted \begin{minipage} with \minipage and \end{minipage} with \endminipage.

About align* (being it the starred form), if you take a look at the definition of the align* environment in amsmath.sty, you'll see

\newenvironment{align*}{%
\start@align\@ne\st@rredtrue\m@ne
}{%
\endalign
}


so, I've substituted \begin{align*} with \start@align\@ne\st@rredtrue\m@ne and \end{align*} with \endalign.

Note that, as egreg points out in his comment, you can replace \start@align\@ne\st@rredtrue\m@ne with \@nameuse{align*}, which is a wrapper for \csname align*\endcsname.

-
Or just \@nameuse{align*} instead of \start@align\@ne\st@rredtrue\m@ne –  egreg Dec 28 '13 at 14:08
@egreg Right. I've just taken the definition from amsmath.sty. I'll add it in the answer. –  karlkoeller Dec 28 '13 at 14:13
It works and I do see roughly where this is going. An explanation would still be very nice. –  chaosflaws Dec 28 '13 at 14:17