I'm currently writing a language handbook. Needless to say, there is some macro-defining involved -- for instance, I needed a macro which takes two arguments (one of which is optional); the mandatory argument should be printed in a bold sans-serif font, and (optionally) followed by the other argument in quotation marks separated by a space. Below is an MWE with what I considered the obvious solution, and illustrations showing when it breaks down.
Code: Select all
\documentclass[a4paper]{article}
\newcommand{\xmpl}[2][]{%
{\bfseries\sffamily #2}%
\ifx#1\empty\else\space`#1'\fi}
\begin{document}
\xmpl{example1}
\xmpl[translation2]{example2}
\xmpl[aardvark]{aardvark}
\end{document}
Code: Select all
\xmpl[{}aardvark]{aardvark}
I've attempted to write a similar macro using \def, but I couldn't implement the "optionalness" of the argument to work correctly by reverse-engineering the \newcommand macro. I'm not well phrased with "The TeXbook" contents quite enough yet to work out, which element of \newcommand makes this error occur. I've spent a few weeks on this already and its diverting me from my work, because I'm spending more time over (La)TeX books instead of my own. Can anyone offer an explanation and, possibly, a solution?
I use MiKTeX 2.8 and Vista.