You can only use literal numbers in the {n}
part and, anyway, an integer cannot be used to get a literal number.
You have to fully expand the numeric expression to a decimal number, but you also have to ensure not expanding too much; the best is to do the calculation before passing the argument:
\documentclass[a5paper]{article}
\usepackage[ngerman]{babel}
\usepackage{expl3, l3regex, xparse}
\usepackage{xcolor}
\ExplSyntaxOn
\tl_new:N \l_flor_redhighlight_tl
\NewDocumentCommand \redhighlight { O{1} m }
{
\flor_redhighlight:fn { \int_to_arabic:n { #1 - 1 } } { #2 }
}
\cs_new_protected:Npn \flor_redhighlight:nn #1 #2
{
\tl_set:Nn \l_flor_redhighlight_tl { #2 }
\regex_replace_all:nnN
{
% note: I am using \"?\w to match German Umlaut's
(\"?\b\w)((?:\"?\w){#1})\b
}
{
\c{textcolor}\cB\{red\cE\}\cB\{\1\2\cE\}
}
\l_flor_redhighlight_tl
\tl_use:N \l_flor_redhighlight_tl
}
\cs_generate_variant:Nn \flor_redhighlight:nn { f }
\ExplSyntaxOff
\begin{document}
\redhighlight{As a default, only one letter words
are highlighted in red.}
\redhighlight[2]{By providing an optional integer
value, one can state the length
of words to be highlighted.}
\redhighlight[3]{By providing an optional integer
value, one can state the length
of words to be highlighted. F"ur}
\end{document}
Note that is preferred that \NewDocumentCommand
passes control to an internal function, if the code is not really simple. In this case it's even essential! You can appreciate the power of “generating variants”.
Functions and variables should have a common prefix to avoid conflicts as much as possible. Also it's better \textcolor{red}{stuff}
to {\color{red}stuff}
.
Some explanations
What happens with this code? The main internal function \flor_redhighlight:nn
expects, as its first argument, an explicit number to be used in a quantifier. However, the quantifier should be one less than the stated number, so passing [2]
to \redhighlight
really highlights two letter words and not three letter ones.
So the argument is passed in the form \int_to_arabic:n { #1 - 1 }
to the variant \flor_redhighlight:fn
, which essentially does
\flor_redhighlight:nn {<full expansion of #1>} { #2 }
One could have defined the variant with x
instead of f
and the result would have been the same. The difference is that x
uses internally an \edef
, while f
works by pure expansion without resorting to \edef
.
The last line should replace -ā
by ā
, -i
by i
, -u
by u
and so on.
To break it up:
\-
represents the character -
.
- Next, everything wrapped in parenthesis
(...)
is the part of the string that should be use for the replacement.
- Then follows a group wrapped in square brackets
[...]
, which essentially means "one of these".
- Following a repetition marker
{1}
meaning the character or group before "exactly once".
- As for the replacement,
\1
selects the first selection of the string, that is, the first part wrapped in parenthesis (...)
, which is [āiueoīū]{1}
in this case.
So, it means replace a -
followed by one of āiueoīū
, but only exactly one character of these, by this very character. Essentially, it removes the -
.
See the documentation of the l3regex
package which is currently included in the doc of the LaTeX3 interfaces (chapter 8).
Best Answer
You're setting the sequence in a table cell and try to use it in another one.
Besides, you want to use the last item in the sequence.
The first problem can be solved by expanding the contents prior to using it. The other one is solved by using item –1
How and why does it work? First of all, your command
\regex_extract_once:nnNTF
sets the sequence in a table cell, so as soon as TeX processes&
(when typesetting), the value would be lost.However, if we build the whole table row before processing it, the value would not be lost (expansion takes place at a different level than typesetting, so TeX will not “see” the
&
, as it's just expanding macros and still not building the table. Yes, tables are quite hard to manage.