2

I have made a macro that takes certain arguments. However, I would like it to output an error when it encounters an unexpected argument. Something like this:

enter image description here

MWE:

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

Which produces the output:

enter image description here

In this case, I would like it to get an errormessage for the \pubtitle{invalidArgument} that sort of looks something like this:

pubtitle command Warning: No supported entry field 'invalidArgument' for bibliography. 
Perhaps you meant '\pubtitle{custom}'?

Sidenote: I want to use this command in the bibliography (using biblatex) like this: \printbibliography[heading=pubtype,type=book,title=\pubtitle{book}] That is the reason the warning says 'entry field for bibliography', because that is what this command is used for.

1 Answer 1

2

You need to nest \ifthenelse, which is a pain.

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

On the console

! 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}'

Output

enter image description here

It becomes a real pain if you want several more cases. Here's a version with case switch.

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

Beware that your \large declaration is not bound in a group…

2
  • Thanks! How can you add two optional parameters to custom? I would like something like \pubtitle[symbol,title]{custom} (how do you do this in ExplSyntax?)
    – Vebjorn
    Commented Oct 21, 2022 at 6:37
  • Will something like this work for making \pubtitle[symbol,title]{custom}? stackoverflow.com/a/2918815/20088792
    – Vebjorn
    Commented Oct 21, 2022 at 8:36

You must log in to answer this question.

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