我制作了一个接受特定参数的宏。但是,我希望它在遇到意外参数时输出错误。如下所示:
梅威瑟:
\documentclass{article}
\usepackage{xcolor}
\usepackage{fontawesome}
\usepackage{ifthen}
\definecolor{icons}{HTML}{46A247}
\makeatletter
\newcommand{\@symbolheading}[2]{\large\textcolor{icons}{#1}\hspace{10pt}\textbf{#2}}
\newcommand{\pubtitle}[1]{%
\ifthenelse{\equal{#1}{book}}{\@symbolheading{\faBook}{Book}}{}
\ifthenelse{\equal{#1}{article}}{\@symbolheading{\faFileText}{Article}}{}
}
\makeatother
\begin{document}
\pubtitle{book}
\pubtitle{article}
\pubtitle{invalidArgument}
\end{document}
输出结果如下:
在这种情况下,我希望它收到\pubtitle{invalidArgument}
类似这样的错误消息:
pubtitle command Warning: No supported entry field 'invalidArgument' for bibliography.
Perhaps you meant '\pubtitle{custom}'?
边注:我想在参考书目中(使用 biblatex)使用此命令,如下所示:\printbibliography[heading=pubtype,type=book,title=\pubtitle{book}]
这就是警告说“参考书目输入字段”的原因,因为这就是此命令的用途。
答案1
您需要嵌套\ifthenelse
,这很痛苦。
\documentclass{article}
\usepackage{xcolor}
\usepackage{fontawesome}
\usepackage{ifthen}
\definecolor{icons}{HTML}{46A247}
\makeatletter
\newcommand{\@symbolheading}[2]{\large\textcolor{icons}{#1}\hspace{10pt}\textbf{#2}}
\newcommand{\pubtitle}[1]{%
\ifthenelse{\equal{#1}{book}}{\@symbolheading{\faBook}{Book}}{%
\ifthenelse{\equal{#1}{article}}{\@symbolheading{\faFileText}{Article}}{%
\PackageError{pubtitle}{%
Unsupported entry field '#1' \MessageBreak for bibliography}{%
Perhaps you meant '\string\pubtitle{custom}'%
}%
}%
}%
}
\makeatother
\begin{document}
\pubtitle{book}
\pubtitle{article}
\pubtitle{invalidArgument}
\end{document}
在控制台上
! Package pubtitle Error: Unsupported entry field 'invalidArgument'
(pubtitle) for bibliography.
See the pubtitle package documentation for explanation.
Type H <return> for immediate help.
...
l.30 \pubtitle{invalidArgument}
? h
Perhaps you meant '\pubtitle{custom}'
输出
如果你想要更多案例,那就太麻烦了。这是一个带有案例切换的版本。
\documentclass{article}
\usepackage{xcolor}
\usepackage{fontawesome}
\definecolor{icons}{HTML}{46A247}
\ExplSyntaxOn
\cs_new_protected:Nn \pubtitle_symbolheading:nn
{
\group_begin:
\large\textcolor{icons}{#1}\hspace{10pt}\textbf{#2}
\group_end:
}
\NewDocumentCommand{\pubtitle}{m}
{
\pubtitle_pubtitle:n { #1 }
}
\cs_new_protected:Nn \pubtitle_pubtitle:n
{
\str_case:nnF { #1 }
{
{book}{\pubtitle_symbolheading:nn{\faBook}{Book}}
{article}{\pubtitle_symbolheading:nn{\faFileText}{Article}}
% other cases
}
{% none of the above
\PackageError{pubtitle}
{
Unsupported ~ entry ~ field ~ '#1' ~ \MessageBreak for bibliography}
{
Perhaps ~ you ~ meant ~ '\string\pubtitle{custom}'
}
}
}
\ExplSyntaxOff
\begin{document}
\pubtitle{book}
\pubtitle{article}
\pubtitle{invalidArgument}
\end{document}
请注意,您的\large
声明不受组约束……