2

I simply want to print out some warning and error messages in some cases. I only find solutions using TeX-code (makeatletter...) or inside package code (\PackageError). For the latter one I wasn't able to find a documentation.

Is there another solution?

\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
  • do you want \errmessage{only A, B or C are allowed}
    – touhami
    Commented Jul 15, 2016 at 13:54

2 Answers 2

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

produces

! 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
? 
1

You can also use expl3 (that you already load along with fontspec). Case switching is simpler and also defining error messages.

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

Here's the relevant part of an interactive session

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! 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
|...............................................
? 

Note that error message management is split into two parts: in the macro we use a predefined error message, calling it by “module” and “name”. Up to four arguments are allowed in the definition.

Avoid loading xltxtra (which used to be recommended some time ago, but now provides no essential feature); just load fontspec.

3
  • Why should I avaoid loading xltxtra? I see no reason why I shouldn't.
    – buhtz
    Commented Jul 15, 2016 at 17:44
  • 1
    @buhtz When your commands \textsuperscript or \textsubscript will do funny things, you'll know why I recommend avoiding xltxtra.
    – egreg
    Commented Jul 15, 2016 at 17:46
  • I don't know these commands. ;)
    – buhtz
    Commented Jul 16, 2016 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .