
我目前正在使用宏,
\def\<#1: #2: #3\>{\langle #1\;:\if#2\empty\else\;#2\;\fi:\;#3\rangle}
取自:http://ctan.org/pkg/tex-ewd。
我想做另一個類似的宏,
\def\<#1 if #2 else #3\>{ \langle #1\; \lhd #2\; \rhd \;#3\rangle }
但這後一種情況與前一種情況發生衝突。看來我只能選其一了。任何有關此事的幫助將不勝感激!
謝謝你!
附言。我對多個分隔符號或具有這種結構的巨集了解不多。非常歡迎任何幫助或了解更多相關資訊的指導!
答案1
你這是在找恥辱。一個巨集應該做一工作。然而,它就在這裡。 ConTeXt 應該允許使用\numexpr
,但可能不需要設定字體來使用\lhd
和\rhd
\def\<#1\>{\moseslookforif#1if\moseslookforif}
\def\moseslookforif#1if#2\moseslookforif{%
\ifx\hfuzz#2\hfuzz
% no if in the argument
\mosescolon#1\mosescolon
\else
\mosesifelse#1if#2\mosesifelse
\fi
}
\def\mosescolon#1: #2: #3\mosescolon{%
\langle #1:\ifx\hfuzz#2\hfuzz\else#2\fi:#3\rangle
}
\def\mosesifelse#1 if #2 else #3if\mosesifelse{%
\langle #1 \lhd #2 \rhd #3\rangle
}
%%% Code possibly to be omitted, if \lhd and \rhd are already available
\font\tenlasy=lasy10
\font\sevenlasy=lasy7
\font\fivelasy=lasy5
\newfam\lasyfam
\textfont\lasyfam=\tenlasy
\scriptfont\lasyfam=\sevenlasy
\scriptscriptfont\lasyfam=\fivelasy
\mathchardef\lhd=\numexpr2*"1000+\lasyfam*"100+"01\relax
\mathchardef\rhd=\numexpr2*"1000+\lasyfam*"100+"03\relax
%%% end of code to possibly omit
% the example
$\<a : b : c\>$
$\<x if y else z\>$
\bye
請注意,測試\if#2\empty
是錯誤的。yy
例如,如果巨集的第二個參數是 ,它將傳回 true ,這當然是不想要的。
\;
我刪除了原始巨集中引入的多餘空格。
答案2
您承認您是初學者,因此我們將從分析您的建議開始。當你說
\def\<#1: #2: #3\>{\langle #1\;:\if#2\empty\else\;#2\;\fi:\;#3\rangle}
您正在定義一個名為 的巨集\<
,這表示您的 .tex 檔案必須包含此標記才能呼叫該巨集。此外,您定義的語法表示您的 .tex 輸入必須使用以下(非常嚴格的語法)呼叫巨集:\<FIRSTARG:SPACE SECONDARG:SPACE THIRDARG\>
。如果省略空格,編譯將失敗並出現錯誤。如果任何參數本身包含空格或冒號,那麼同樣可能會產生錯誤(或列出錯誤的參數定義)。
所以當你能這樣定義宏,使用起來似乎不太方便。更有可能的是,您想要輸出以某種方式查看,而不是要求鍵入輸入以某種方式出現。為 3 參數輸入定義此類巨集(在 LaTex 中)的正常方法是 \newcommand\macroname[3]{...macros to produce the desired output using inputs #1, #2, and #3...}
.那麼調用形式就是\macroname{FIRSTARG}{SECONDARG}{THIRDARG}
。透過這種方式,大括號分隔輸入,現在可以包含冒號和空格而不會產生歧義,
因此,如果我定義了\newcommand\colonangle[3]{\langle #1\;:\if#2\empty\else\;#2\;\fi:\;#3\rangle}
,那麼我可以使用語法來獲得您想要的第一個巨集輸出,例如\colonangle{x}{y}{z}
。
現在來看第二個例子。它不僅還創建一個名為的巨集(與先前\<
的巨集名稱重複,但現在需要不同的輸入語法),它還要求您if
在else
輸入文件。相反,我提出了第二個巨集定義,以避免混淆,使用更標準的輸入語法。
\documentclass{article}
\usepackage{amssymb}
\newcommand\colonangle[3]{\langle #1\;:\if#2\empty\else\;#2\;\fi:\;#3\rangle}
\newcommand\arrowangle[3]{ \langle #1\; \lhd #2\; \rhd \;#3\rangle }
\begin{document}
$\colonangle{A}{B}{C} \ne \arrowangle{A}{B}{C}$
\end{document}