6

I want to know the possibilities and the limitation of LaTeX's \newcommand regarding the definition and the use/call of new macros. I know some of these examples are primitive but they can help to understand the limitations. I am only interested in basic \newcommands with "\"char+ as name.

Invalid examples:

\newcommand{\newcenter}{center} 
\begin{\newcenter}
\end{center}

\newcommand{\tabularthree}{{tabular}}
\begin \tabularthree {ccc}
cell& cell
\end \tabularthree

\newcommand{\tion}{tion}
\sec\tion{mysec}

Valid examples:

\newcommand{\optarg}{[myoptarg]} 
\section\optarg{mysec}

\newcommand{\cc}{cc}
\newcommand{\mybegin}{\begin}
\mybegin{tabular}{c\cc}
cell& cell
\end{tabular}

\newcommand{\tabulartwo}{tabular}
\begin{\tabulartwo} {ccc}
cell& cell
\end \tabulartwo

\newcommand{\tabularfour}{tabular}
\begin \tabularfour{ccc}
cell& cell
\end \tabularfour
  • 1
    I don't really see what the use-case would be other than unreadable and unmaintainable document source code. – Henri Menke Sep 26 '18 at 20:51
  • These don't so much look like limitations of \newcommand but an issue of understanding LaTeX (and TeX)'s command expansion rules. – A.Ellett Sep 26 '18 at 20:54
  • these are not really related to \newcommand but to the use of the macros, you would get the same errors if the macros were defined by the primitive \def – David Carlisle Sep 26 '18 at 21:06
13

Henri Menke told you how to work around the rules. This answer will show you why the syntax you used (doesn't) work.

The ones that don't work:

\newcommand{\newcenter}{center}
\begin{\newcenter}
\end{center}

This one fails because \@currenvir expands to \newcenter and when LaTeX calls the \@checkend command, it compares the meaning of \@currenvir and the argument to \end, which is center. The comparison fails because it compares \newcenter with center without further expasion, so they differ and an error is raised. The error message, on the other hand, fully expands \@currenvir and the error message is misleading:

! LaTeX Error: \begin{center} on input line 29 ended by \end{center}.

\newcommand{\tabularthree}{{tabular}}
\begin \tabularthree {ccc}
cell& cell
\end \tabularthree

This one fails because LaTeX checks if the enfironment exists with \ifcsname which will build a command with the argument to \begin which, in this case, is \tabularthree, which expands to {tabular}. The \ifcsname then checks for a command named {tabular}, which doesn't exist and LaTeX tells you:

! LaTeX Error: Environment {tabular} undefined.

\newcommand{\tion}{tion}
\sec\tion{mysec}

This one fails because it's invalid. The command \sec is expanded, and it is the secant function, which is only allowed in math-mode, so TeX thinks you forgot a $:

! Missing $ inserted.
<inserted text> 
                $
l.46 \sec

If \sec wasn't a math-mode command, TeX would try to execute it. If it had an argument, the argument would be the command \tion, which then expands to tion and then the \sec command would do its thing (whatever it happened to be) with tion. If it didn't exist, TeX would complain that \sec doesn't exist.


The one that works:

\newcommand{\optarg}{[myoptarg]}
\section\optarg{mysec}

This one compiles, but it doesn't produce what you'd expect. The test for the optional argument fails, because the code for \section doesn't see a [ character (and it doesn't expand \optarg looking for it), so it takes \optarg as the mandatory argument and the title of the section becomes [myoptarg] and mysec is written below.


The ones that do work:

\newcommand{\cc}{cc}
\newcommand{\mybegin}{\begin}
\mybegin{tabular}{c\cc}
cell& cell
\end{tabular}

This one works because when TeX expands \mybegin it just finds \begin. TeX then proceeds expanding \begin and doing things normally. The \cc thing works because tabular expands the tokens of the alignment preamble.


\newcommand{\tabulartwo}{tabular}
\begin{\tabulartwo} {ccc}
cell& cell
\end \tabulartwo

This one works by the same reason that the first case didn't. The check for the environment name is performed for \tabulartwo and \tabulartwo, which are the same thing. And the braces there don't make a difference because by TeX's argument grabbing rules, a normal argument is either a single token (\tabulartwo is a single token) or, if a open brace ({) is found, then the argument is the <balanced text> within that pair of braces (\tabulartwo and tabular are <balanced text> within a pair of braces).


\newcommand{\tabularfour}{tabular}
\begin \tabularfour{ccc}
cell& cell
\end \tabularfour

Same as above.


But you really shouldn't be inventing syntax for commands, because even if it works, you will obfuscate your code and not even you will understand it in a few weeks.

  • 2
    On the other hand, \begin{tabular}{c\cc} will not work if the array package is loaded. – egreg Sep 26 '18 at 21:34
10

\expandafter solves almost all your problems. You seem to be a LaTeX beginner, so these are not things you should usually have to do. Please also have a look at When to use \edef, \noexpand, and \expandafter?

\documentclass{article}
\begin{document}

\newcommand{\newcenter}{center} 
\expandafter\begin\expandafter{\newcenter}
\end{center}

\newcommand{\tabularthree}{{tabular}}
\expandafter\begin\tabularthree{ccc}
cell& cell
\expandafter\end\tabularthree

\newcommand{\tion}{tion}
\csname sec\tion\endcsname{mysec}

\end{document}
  • You beat me to the punch. Your answer is almost verbatim my answer. lol. – A.Ellett Sep 26 '18 at 21:01

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.