I have been experimenting with using the xparse
package for custom document macros with a Markdown-like syntax. For example, I used to write \cut{x}{P}{Q}
to produce . With the help of
xparse
, I would like to instead write \cut(x)(P | Q)
-- I find this Markdown-like syntax much easier to read in the document's source.
(In case you're not convinced of the benefit, also compare \send{y,x'}{x}{y,x'}{P}{Q}
with \send (y,x')(x<y,x'> | P | Q)
as a means of producing .)
The code that I'm currently using to define \cut
is:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\cut}{r() >{\SplitArgument{1}{|}}r()}{%
(\nu #1)(\use_i:nn#2 \mid \use_ii:nn#2)%
}
\ExplSyntaxOff
\begin{document}
Example 1: $\cut(x)(P | Q)$ % Works great!
% Example 2: $\cut(x)(P | \cut(y)(Q_1 | Q_2))$ % Fails with error
Example 3: $\cut(x)(P | {\cut(y)(Q_1 | Q_2)})$ % Works, but slightly annoying
\end{document}
When \cut
s are not nested (as in Example 1 above), then everything works perfectly. However, I sometimes need to nest \cut
s (as in Example 2 above). Unfortunately, I then get an xparse/split-excess-tokens
error: "Too many |
tokens when trying to split argument."
Question: Is there any way to force xparse
to treat r()
as groups when splitting arguments so that this error is avoided? Alternatively, can I turn off or reduce this error to a warning by setting some xparse
option?
Of course, I can add a group explicitly myself (as in Example 3 above), but this is somewhat annoying. Another compromise is to make the second argument to \cut
an m
-type argument and write \cut(x){P | \cut(y){Q_1 | Q_2}}
, but I'd rather avoid this if possible.
\def\cut(#1)(#2|#3){(\nu#1)(#2\mid#3)}
which allows you to use$\cut(x)(P | \cut(y)(Q_1 | Q_2))$
. Spaces in the arguments#1
and#2
are not really a problem here. This doesn't usexparse
and therefore doesn't answer the question, per se. – Werner Nov 1 '12 at 19:33;-)
However, the problem is that in the nested call, the second argument to\cut
has two|
tokens and LaTeX can't know which one to select as the splitting marker. If you use braces, this won't happen, because TeX never chooses something as an argument if it results in unbalanced braces. – egreg Nov 1 '12 at 20:32xparse
/TeX "never choose something as an argument if it results in unbalanced" parentheses? In other words, can I make parentheses be treated as if they were braces? – Henry DeYoung Nov 1 '12 at 21:33