![正则表达式替换用作命令/函数/环境参数的标记列表中的所有内容](https://linux22.com/image/351082/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%9B%BF%E6%8D%A2%E7%94%A8%E4%BD%9C%E5%91%BD%E4%BB%A4%2F%E5%87%BD%E6%95%B0%2F%E7%8E%AF%E5%A2%83%E5%8F%82%E6%95%B0%E7%9A%84%E6%A0%87%E8%AE%B0%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E5%86%85%E5%AE%B9.png)
我正在开发一个带有命令的包,该命令会创建一个新的tikzpicture
。我想将tikzpicture
参数列表传递给该命令。例如,对于我的单参数命令,我可能会传递scale=1.5,line width=20pt,ultra thick
给它。接下来,有一个替换所有正则表达式调用。对于这个 MWE,我将其简化为用字符替换空格~
。
当我尝试使用经过正则表达式修改的标记列表并将其放入的可选参数列表中时,就会出现问题tikzpicture
。由于我尚未更好地理解的原因,\tl_use:N
在此设置中调用会产生错误,其中整个标记列表被视为单个pgf/tikz
键。例如,scale=1.5,line width=20pt,ultra thick
作为参数将生成此错误:
! Package pgfkeys Error: I do not know the key
'/tikz/scale=1.5,line~width=20pt,ultra~thick' and I am
going to ignore it. Perhaps you misspelled it.
我假设这是一个 catcode 问题?无论如何,我对 latex3 还很陌生,可能遗漏了某些内容或以错误的方式处理了这个问题。以下是显示该问题的 MWE:
\documentclass{article}
\usepackage{expl3}%
\usepackage{l3regex}%
\usepackage{xstring}%
\usepackage{xparse}%
\usepackage{tikz}%
\DeclareDocumentCommand{\mycommand}{ m }
{%
\tl_new:N \l_tikzvowel_fix_spaces_tl
\tl_set:Nn \l_tikzvowel_fix_spaces_tl {#1}
\regex_replace_all:nnN { \s } { \~ } \l_tikzvowel_fix_spaces_tl
\begin{tikzpicture}[#2]% <--- this compiles fine
%tikz stuff
\end{tikzpicture}
\begin{tikzpicture}[\tl_use:N \l_tikzvowel_fix_spaces_tl]% <--- this causes the error
%tikz stuff
\end{tikzpicture}
}
\begin{document}
\mycommand{scale=1.5,line width=20pt,ultra thick}
\end{document}
调用\tl_show_analysis:N \l_tikzvowel_fix_spaces_tl
令牌列表可得到:
The token list \l_tikzvowel_fix_spaces_tl contains the tokens:
> s (the letter s)
> c (the letter c)
> a (the letter a)
> l (the letter l)
> e (the letter e)
> = (the character =)
> 1 (the character 1)
> . (the character .)
> 5 (the character 5)
> , (the character ,)
> l (the letter l)
> i (the letter i)
> n (the letter n)
> e (the letter e)
> (blank space )
> w (the letter w)
> i (the letter i)
> d (the letter d)
> t (the letter t)
> h (the letter h)
> = (the character =)
> 2 (the character 2)
> 0 (the character 0)
> p (the letter p)
> t (the letter t)
> , (the character ,)
> u (the letter u)
> l (the letter l)
> t (the letter t)
> r (the letter r)
> a (the letter a)
> (blank space )
> t (the letter t)
> h (the letter h)
> i (the letter i)
> c (the letter c)
> k (the letter k).
<recently read> }
我在这里做错了什么? 对于这种情况,标准的 latex3 方法是什么? 虽然这种特殊情况涉及tikzpicture
环境选项,但我对可以应用于任何命令或环境参数的解决方案感兴趣。
答案1
这是为 keyval 参数保存 token 的“常见”问题:keyval 方法工作非常困难不是扩展它们的参数,这样你的设置就不会被看到。因此你需要强制扩展,例如
\use:x { \exp_not:N \begin{tikzpicture}
[\exp_not:V \l_tikzvowel_fix_spaces_tl] }
(\begin
引擎不够强大所以我们需要避免过早扩张)。