Text FormattingOptional argument beginning with repeated characters

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Optional argument beginning with repeated characters

Post by woj-k »

Hello everyone, this is my first post on this forum.

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}
The first two instances of \xmpl work fine, the other produces erroneous output -- apparently the macro defined this way cannot handle an optional argument that begins with several identical characters. Interestingly,

Code: Select all

\xmpl[{}aardvark]{aardvark}
behaves correctly, but inputting the same empty group into the macro definition changes nothing.

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.
Last edited by woj-k on Mon Aug 02, 2010 9:32 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Optional argument beginning with repeated characters

Post by Juanjo »

Modify the definition of \xmpl as follows:

Code: Select all

\newcommand{\xmpl}[2][]{%
   {\bfseries\sffamily #2}%
   \def\temp{#1}%
   \ifx\temp\empty\else\space`#1'\fi}
It is worthy to start using the xparse package, which allows the definition of very complex commands. This package has been developped as part of the LaTeX3 Project. Read this, for example. Your minimal example could be rewritten as follows:

Code: Select all

\documentclass[a4paper]{article}
\usepackage{xparse}

\NewDocumentCommand\xmpl{om}{%
   {\bfseries\sffamily #2}%
   \IfNoValueTF{#1}{}{\space`#1'}}
   
\begin{document}
\xmpl{example1}

\xmpl[translation2]{example2}

\xmpl[aardvark]{aardvark}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Re: Optional argument beginning with repeated characters

Post by woj-k »

Oh, perfect! Thank you for the help, this is exactly what I need. Thanks for pointing me towards xparse as well --- good to know another powerful package. I'll read up on it right away.

Thank you for spending your time on this,
woj-k
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Optional argument beginning with repeated characters

Post by localghost »

Now that the problem is solved, please mark the topic accordingly as s in the Board Rules (to be read before posting).


Best regards and welcome to the board
Thorsten
Post Reply