[Tex/LaTex] returning maths values in a new command

amsmathmacrosmath-mode

So I'm trying to construct a command which calls another command to make typing easier, but for some reason I'm not able to get the commands to return things properly. Here is what I have

\documentclass[12pt]{article}


\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}

\newcommand{\mfunction}[4] {
    \def \lnum{#4}

    \ifnum\lnum=0
        {M_{{#1}_{{#2},{#3}}}}
    \else
        {M_{{#1}_{{#2},{#3}}^{k}}}
    \fi
}

\newcommand{\man}[3][0] {\mfunction{A}{#2}{#3}{#1}}
\newcommand{\mbn}[3][0] {\mfunction{B}{#2}{#3}{#1}}
\newcommand{\mdn}[3][0] {\mfunction{D}{#2}{#3}{#1}}



\begin{document}
    \begin{align*}
        \man[1]{n}{l} \coloneqq \{a\}\\
        \mbn{n}{k} \coloneqq \{b\} \\
        \mdn{n-1}{k} \coloneqq \{d\}
    \end{align*}
\end{document}

This is coming back with the errors that I'm missing '$' and '}' signs where I have '\end{align*}'. Oddly when I place the code directly into my 'quick' command I get the proper output. So the code that works is:

\documentclass[12pt]{article}


\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}



\newcommand{\man}[3][0] {{M_{{A}_{{#2},{#3}}^{#1}}}}



\begin{document}
    \begin{align*}
        \man[1]{n}{l} \coloneqq \{a\}
    \end{align*}
\end{document}

Why would the first version not work and the second one work? Is something happening with my definition/if statement that is causing the new command to return data that is not intended?

Thanks.

Best Answer

Delete the blank line after \def \lnum{#4}.