使用以下代码,
\documentclass{standalone}
\newcommand{\@s}[1]{\hspace{#1pt}}
\begin{document}
text
\end{document}
我收到以下错误:
! LaTeX Error: Command \@ already defined.
Or name \end... illegal, see p.192 of the manual.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.2 \newcommand{\@s}[1]{\hspace{#1pt}}
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.2 \newcommand{\@s}[1]{\hspace{#1pt}}
You're in trouble here. Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
! Missing number, treated as zero.
<to be read again>
##
l.2 \newcommand{\@s}[1]{\hspace{#1pt}}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Illegal unit of measure (pt inserted).
<to be read again>
##
l.2 \newcommand{\@s}[1]{\hspace{#1pt}}
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)
! You can't use `macro parameter character #' in horizontal mode.
<recently read> ##
l.2 \newcommand{\@s}[1]{\hspace{#1pt}}
Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.
这里出了什么问题?我该如何修复它?
答案1
有几处地方是错误的。 @
是 LaTeX 中的特殊字符。在软件包中,它的 catcode 为 11,这意味着它被视为字母,但在文档中,它的 catcode 为 12,即“其他”。这意味着,在文档上下文中,它不能是组成 名称的多字符字符串的一部分\newcommand
,除非您重新启用 用作@
字母,您将在代码中使用 看到这种情况\makeatletter
,然后使用 停止使用\makeatother
。(注意:catcode“其他”符号能作为单字符宏名,例如\%
、\$
,甚至是\@
,当前问题的根源)
因此\newcommand{\@s}[1]{\hspace{#1pt}}
,由于 的状态为@
“其他”,因此尝试定义宏\@
,而不是\@s
。 并且\@
是已定义的宏,因此会产生错误。您可以在声明\makeatletter
之前放置\newcommand
,但是如果您恢复到正常设置并在\makeatother
声明之后使用 ,则无法使用 进行调用\@s
(它将被标记为\@
和s
),而只能使用\csname @s\endcsname
,这很拗口。
要在正常文档设置中使用它,需要像这样
\documentclass{standalone}
\makeatletter
\newcommand{\@s}[1]{\hspace{#1pt}}
\makeatother
\begin{document}
text\csname @s\endcsname{100}text
\end{document}
在命名宏时避免使用@
totally 会更容易,因为目的是允许包开发人员仅使用它,从而避免与用户定义的宏名称发生冲突的可能性。
\documentclass{standalone}
\newcommand{\ats}[1]{\hspace{#1pt}}
\begin{document}
text\ats{100}text
\end{document}
看到了吗?是不是感觉好多了?
答案2
您不能创建名称由字母字符和特殊字符混合组成的命令。命令名称要么只有一个特殊字符,要么有任意数量的字母字符。\makeatletter
在 .cls 或 .sty 文件之外或之外,@
将被视为特殊字符,因此不能用作命令名称的字母字符。
以下方法可能有效:
\documentclass{standalone}
\makeatletter
\newcommand*{\@s}{foo}
\makeatother