Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Perhaps the calculator package is of use to you? –  Christian Hupfer Aug 13 at 10:01

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
#facepalm That did the trick. Does the new line get returned in the command function as well then? I assumed it wouldn't be. –  Aram Papazian Aug 13 at 10:04
1  
@AramPapazian TeX (usually) turns a blank line into a \par token: that's not allowed inside an align, so you get an error. Remember that a command simply inserts the material you've defined it with into where you use it: if you try writing your example with a blank line directly in the align you'll see the same error. –  Joseph Wright Aug 13 at 10:09
    
I just didn't realise TeX was converting the blank line into a \par token. I made the (bad) assumption that in \newcommand all blank lines would be 'filtered' out, but it also makes sense that it wouldn't. Is there a way to alter it so it isn't converted if I do want blank lines to make it more human readable? –  Aram Papazian Aug 13 at 10:14
    
You can add a line with just a %, then it will almost look empty. Note that TeX also ignores the newline after a comment. –  CompuChip Aug 13 at 14:24

A blank line is converted to \par, which is illegal in align and all math display environment.

There are also some other glitches in your code:

\documentclass[12pt]{article}

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

\newcommand{\mfunction}[4]{%
  \ifnum#4=0\relax
    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}

There is no need of defining \lnum, as you can use #4 directly in \ifnum. I also added % to protect the end-of-lines which would produce a space. It isn't strictly necessary here because the macro will be used in math mode, where spaces are ignored, but it's a good habit because for macros to be used in text mode spaces are significant.

You can choose between

\ifnum#4=0\relax

and

\ifnum#4=0

(with no %); \relax more clearly marks the end of the number, but in other circumstances, when full expandability is needed, it shouldn't be used (not the case here).

I also removed unnecessary braces.

share|improve this answer
    
Your solution is super elegant! This turned out to be the one I ended up using as it gave the most description. I had already accepted the other response so didn't want to take the reps away from Ian, but wanted to mention that this was a great explanation for future users. Thanks =) –  Aram Papazian Aug 14 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.