General\newcommand. Cannot create macros called \x1 and \x2

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
gustaf4711
Posts: 6
Joined: Fri Feb 04, 2022 12:17 pm

\newcommand. Cannot create macros called \x1 and \x2

Post by gustaf4711 »

I try to create two simple macros named \x1 and \x2 to save typing time. One try uses the standard \newcommand and the other the TeX \def but both fails. Using may questions, I am unable to get some help from Google. Unfortunately The TeXBook's chapter 20---with all those bends and double-bends---is too difficult for me to follow.

Below please find the two cases which both fail. The error message is included at the beginning as a LaTeX comment.

Hopefully someone could help me out of this trouble.

Regards,
ChG

First try:

Code: Select all

%
% ! LaTeX Error: \mathbf allowed only in math mode.
% ...
% l.6  \newcommand{\x1}{\mathbf{x}_1}
%
\documentclass[12pt,a4paper]{article}
\begin{document}\pagestyle{empty}
 \newcommand{\x1}{\mathbf{x}_1}
 \newcommand{\x2}{\mathbf{x}_2}
\begin{displaymath}
  \x1+\x2
\end{displaymath}
\end{document}
Second try:

Code: Select all

% ! Use of \x doesn't match its definition.
% l.9   \x1
%         +\x2
\documentclass[12pt,a4paper]{article}
 \def\x1{\mathbf{x}_1}
 \def\x2{\mathbf{x}_2}
\begin{document}\pagestyle{empty}
\begin{displaymath}
  \x1+\x2
\end{displaymath}
\end{document}
Last edited by cgnieder on Fri Feb 04, 2022 8:15 pm, edited 1 time in total.
Reason: added code markup

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newcommand. Cannot create macros called \x1 and \x2

Post by cgnieder »

command names cannot have digits in them. Command names may only consist of what (La)TeX regards as “letters”, see the TeX FAQ: https://texfaq.org/FAQ-linmacnames
site moderator & package author
gustaf4711
Posts: 6
Joined: Fri Feb 04, 2022 12:17 pm

\newcommand. Cannot create macros called \x1 and \x2

Post by gustaf4711 »

Thank you very much to point out that a newcommand's comm_name is made from letters only.

A bit ashamed I of course found it clearly said in my book by Kopka and Daly. TeX's error-messages did not give me any clear indication of what was the nature of my error, so I did not consult the book.

The \x1 and the \x2 make the manuscript much easier to read and therefore I keep them. An editor macro then easily turns the readable tex-file into a copy-file with, acceptable \xa and \xb .

ChG
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

\newcommand. Cannot create macros called \x1 and \x2

Post by Ijon Tichy »

In the shown case, you can define a command with one argument:

Code: Select all

\documentclass[12pt,a4paper]{article}
\newcommand*{\x}[1]{\mathbf{x}_{#1}}
\begin{document}\pagestyle{empty}
\begin{displaymath}
  \x1+\x2+\x{12}
\end{displaymath}
\end{document}
And please note: Several one letter macros are already defines. To avoid broken code, you should always use \newcommand or \NewDocumentCommand. The second one needs a new LaTeX kernel or package xparse. See ltnews for more information.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply