惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
美团技术团队
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
量子位
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
博客园 - 叶小钗
月光博客
月光博客
P
Proofpoint News Feed
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog

XeTeX and LaTeX

Author per chapter (a.k.a. Proceedings) Subscript and superscript in text mode Typesetting tilde or backslash Phantomas command First gloss line in italics (gb4e) Table lines starting with square bracket Filetype recognition in Vim Vim for XeTeX/LaTeX First word of a paragraph Tables Block comment Line spacing in tables Assign another catcode Verbatim text as command argument LaTeX’s new era started: XeTeX
Newcommand with an optional argument
noreply@blogger.com (Anonymous) · 2008-03-05 · via XeTeX and LaTeX

I don’t know why this feature is not implemented in LaTeX. You can very easily define your own commands, but they may take only a fixed number of arguments (up to 9). Bad luck, because the first command I needed to write for my dissertation had to have an optional argument:

Linguists traditionally write words and sounds from a language of interest in italics, e.g. the N|uu word ainki. Often it is followed by an enquoted translation, e.g. ainki ‘father’, xainki ‘mother’, n|ai ‘see’, and kx’ain ‘laugh’ all contain the diphthong ai.

So I wanted to write me a command \nuu by which I could write either just the N|uu word in italics or the word in italics followed by the enquoted translation. I would use it either just as \nuu{ainki} for ainki or as \nuu[father]{ainki} for ainki ‘father’.

Later, when I learn how to make an index of all N|uu words written in my document, I probably will just add an indexing command into the \nuu command definition. For now the optional argument was trouble enough. I did not expect that it will involve hardcore TeX programming.

Fortunately good people from the xetex@tug.org mailing list have suggested some solutions for this and the most bullet-proof and versatile was this (thanks to Morten Høgholm and Ross Moore):


\documentclass{article}

\makeatletter
\long\def\tlist@if@empty@nTF #1{%
\expandafter\ifx\expandafter\\\detokenize{#1}\\%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

\providecommand*\nuu[2][]{%
\textit{#2}%
\tlist@if@empty@nTF{#1}{}{ `#1'}% only the false code executed
}
\makeatother

\begin{document}
\nuu{ainki} \nuu[mother]{xainki} \nuu[goat]{mudi}
\end{document}

I use \providecommand instead of \newcommand. I feel safer. “It works like \newcommand, but if the command is already defined, LaTeX2e will silently ignore it.” (Oetiker et al., lshort.pdf, p. 109.)