From d751f070113da57a9193c0d189ede00811abc7d7 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 31 May 2018 16:28:46 +0200 Subject: [PATCH] Added facilities for defining macros with optional arguments --- ucwmac.tex | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ucwmac.tex b/ucwmac.tex index 9c99369..f49d765 100644 --- a/ucwmac.tex +++ b/ucwmac.tex @@ -94,6 +94,31 @@ \setmargins{1truein} \setpaper{a4} +%%% Macros with optional arguments %%% + +% After \def\a{\withoptarg\b}, the macro \a behaves in this way: +% \a[arg] does \def\optarg{arg} and then it expands \b +% \a does \let\optarg=\relax and then it expands \b +\def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt} +\def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi} +\def\xoptwith[#1]{\def\optarg{#1}\xoptcall} + +% A shortcut for defining macros with optional arguments: +% \optdef\macro behaves as \def\domacro, while \macro itself is defined +% as a wrapper calling \domacro using \withoptarg. +\def\optdef#1{% + \edef\xoptname{\expandafter\eatbackslash\string#1}% + \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}% + \expandafter\def\csname do\xoptname\endcsname +} + +% Trick: \eatbackslash eats the next backslash of category 12 +\begingroup\lccode`\+=`\\ +\lowercase{\endgroup\def\eatbackslash+{}} + +% Expand to the optional argument if it exists +\def\optargorempty{\ifx\optarg\relax\else\optarg\fi} + %%% Placing material at specified coordinates %%% % Set all dimensions of a given box register to zero -- 2.39.2