1

Is there a way to allow custom command to send a log entry who pointed to an error ?

I want to create the command \difficulty who convert the four first integers in filled or emptied stars. Here is the code :

\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}

However, i want to alert if the argument is not {1,2,3,4} by a message in the log-console. All i achieved untill now is to send an error if the argument is not a number (thanks to \ifnum). I'm totally lost to interact with the console and to create an alert.

Here a 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
    Not directly related: Try \ifcase#1 <case for 0>\or<case for 1>\or...\else<other cases>\fi. – Phelype Oleinik Jun 1 at 17:20
4
\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}

writes to the console:

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

The output is:

enter image description here

  • You've created \isnum but i do not understand what is the goal of this command. Could you expand it ? The construction with \or is also better. EDIT : After some test, the code does not work. He send no error neither with 25 nor random. EDIT 2 : Ok, it is written in the .log file directly. My goal was to alert the user that there is an error to compile. (red line in the bottom window of texstudio/texmaker). I'm on Texstudio if it is important. – Piroooh Jun 2 at 17:25
  • it tests if the argument is at least a number: 1a0 for example is not a number. – user187802 Jun 2 at 18:08
  • Thanks, clearer to me ! Do you know if it is possible to alert the user in the consol directly if there is an error ? Something like "line 18 error : 'random' is not a correct difficulty option". Otherwise, i think the question is solved ! – Piroooh Jun 5 at 15:25
  • 1
    \errmessage{#1 is not a number!} – user187802 Jun 5 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.