我只是想在某些情况下打印出一些警告和错误消息。我只找到使用 TeX 代码 ( makeatletter
...) 或包内代码 ( \PackageError
) 的解决方案。对于后者,我找不到文档。
还有其他解决方案吗?
\documentclass{article}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage[spelling=new]{german}
\usepackage{ifthen}
\newcommand{\mymacro}[1]{%
\ifthenelse{ \equal{#1}{A} }
{A case}
{%
\ifthenelse{ \equal{#1}{B} }
{B case}
{%
\ifthenelse{ \equal{#1}{C} }
{C case}
{%
% print out error message that
% only A, B or C are allowed
% values
}
}
}
}
\begin{document}
\mymacro{X} % error
\end{document}
答案1
\documentclass{article}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage[spelling=new]{german}
\usepackage{ifthen}
\newcommand{\mymacro}[1]{%
\ifthenelse{ \equal{#1}{A} }%
{A case}%
{%
\ifthenelse{ \equal{#1}{B} }%%%
{B case}%%%
{%
\ifthenelse{ \equal{#1}{C} }%%%
{C case}%%%
{%
\GenericError{[mycode] }{Bad input}%
{[mycode] Only A B or C\MessageBreak allowed}%
{read the doc}%%%
}%%
}%%
%%
}%%
}
\begin{document}
\mymacro{X} % error
\end{document}
生产
! Bad input.
[mycode] Only A B or C
[mycode] allowed
Type H <return> for immediate help.
...
l.27 \mymacro{X}
% error
? h
read the doc
?
答案2
您还可以使用expl3
(您已经随 一起加载fontspec
)。大小写切换更简单,还可以定义错误消息。
\documentclass{article}
\usepackage{xparse}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage[spelling=new]{german}
\ExplSyntaxOn
\NewDocumentCommand{\mymacro}{m}
{
\str_case:nnF { #1 }
{
{A}{\mymacroA}
{B}{\mymacroB}
{C}{\mymacroC}
}
{
\msg_error:nnn { buhtz/mymacro } { wrong-value } { #1 }
}
}
\msg_new:nnnn { buhtz/mymacro } { wrong-value }
{
Bad~value~'#1'
}
{
You~can~only~use~A,~B~or~C,~you~had~#1
}
\ExplSyntaxOff
\newcommand{\mymacroA}{Typed A}
\newcommand{\mymacroB}{Typed B}
\newcommand{\mymacroC}{Typed C}
\begin{document}
\mymacro{A}
\mymacro{B}
\mymacro{C}
\mymacro{X} % error
\end{document}
以下是互动环节的相关部分
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! buhtz/mymacro error: "wrong-value"
!
! Bad value 'X'
!
! See the buhtz/mymacro documentation for further information.
!
! For immediate help type H <return>.
!...............................................
l.44 \mymacro{X}
% error
? h
|'''''''''''''''''''''''''''''''''''''''''''''''
| You can only use A, B or C, you had X
|...............................................
?
请注意,错误消息管理分为两部分:在宏中我们使用预定义的错误消息,通过“模块”和“名称”调用。定义中最多允许四个参数。
避免加载xltxtra
(以前曾推荐过,但现在不再提供任何基本功能);只需加载fontspec
。