为自定义命令和条件配置错误

为自定义命令和条件配置错误

有没有办法允许自定义命令发送指出错误的日志条目?

我想创建一个命令\difficulty来转换填充或空心星号的前四个整数。代码如下:

\newcommand{\difficulty}[1]{
    \ifnum#1=1
        \ding{77}\ding{73}\ding{73}\ding{73}
    \else
        \ifnum#1=2
            \ding{77}\ding{77}\ding{73}\ding{73}
        \else
            \ifnum#1=3
                \ding{77}\ding{77}\ding{77}\ding{73}
            \else
                \ifnum#1=4
                    \ding{77}\ding{77}\ding{77}\ding{77}
                \else
                    undefined
                \fi
            \fi
        \fi
    \fi}

但是,如果参数不是 {1,2,3,4},我想通过日志控制台中的消息发出警报。到目前为止,我所做的就是如果参数不是数字,则发送错误(感谢\ifnum)。我完全不知道如何与控制台交互并创建警报。

这里是 MWE:

\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{pifont}

\newcommand{\difficulty}[1]{
    \ifnum#1=1
    \ding{77}\ding{73}\ding{73}\ding{73}
    \else
    \ifnum#1=2
    \ding{77}\ding{77}\ding{73}\ding{73}
    \else
    \ifnum#1=3
    \ding{77}\ding{77}\ding{77}\ding{73}
    \else
    \ifnum#1=4
    \ding{77}\ding{77}\ding{77}\ding{77}
    \else
    undefined
    \fi
    \fi
    \fi
    \fi}

\begin{document}
    Test for 1
    \difficulty{1}


    Test for 2
    \difficulty{2}

    Test for 3
    \difficulty{3}

    Test for 4
    \difficulty{4}

    Test for 25
    \difficulty{25}

    Test for 'random'
    \difficulty{random}

\end{document}

答案1

\documentclass[11pt,a4paper]{book}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{pifont}

\makeatletter
\newcommand*\isnum[1]{%
  \if!\ifnum9<1#1!\else_\fi\expandafter\@firstoftwo
    \else\expandafter\@secondoftwo\fi}%
\makeatother

\newcommand{\difficulty}[1]{%
  \isnum{#1}{%
    \ifcase#1
    \or\ding{77}\ding{73}\ding{73}\ding{73}
    \or\ding{77}\ding{77}\ding{73}\ding{73}
    \or\ding{77}\ding{77}\ding{77}\ding{73}
    \or\ding{77}\ding{77}\ding{77}\ding{77}
    \else
      \typeout{difficulty: the number is not in 1..4 -> #1}%
    \fi
  }{\typeout{difficulty: the argument is not defined -> #1}}%
}

\begin{document}
    Test for 1
    \difficulty{1}

    Test for 2
    \difficulty{2}

    Test for 3
    \difficulty{3}

    Test for 4
    \difficulty{4}

    Test for 25
    \difficulty{25}

    Test for 'random'
    \difficulty{random}

\end{document}

写入控制台:

difficulty: the number is not in 1..4 -> 25
difficulty: the argument is not defined -> random

输出为:

在此处输入图片描述

相关内容