
ショートカットを作りたい
\href{tel:0123456789}{01\,23\,45\,67\,89}
\StrSubstitute
パッケージからxstring
そのまま使えます
\StrSubstitute{01 23 45 67 89}{ }{\,}
の2番目の引数の中にあるhref
。しかし、
\StrSubstitute{01 23 45 67 89}{ }{}
最初の引数では機能しません。
これはマクロの展開順序の問題だと理解しています。しかし、LaTeX を最初に\StrSubstitute
で解析できる文字列に展開するにはどうすればよいでしょうか\href
?
私が実際に達成したいことの最小限の例を以下に示します。
\documentclass{minimal}
\usepackage{xstring}
\usepackage{hyperref}
\newcommand\phone[1]{\href{tel:\StrSubstitute{#1}{ }{}}{\StrSubstitute{#1}{ }{\,}}}
\begin{document}
\href{tel:0123456789}{01\,23\,45\,67\,89}
\href{\StrSubstitute{01 23 45 67 89}{ }{}}{\StrSubstitute{01 23 45 67 89}{ }{\,}}
\phone{01 23 45 67 89}
\end{document}
編集: 国によって数値形式の規則が異なるため、ペアワイズ パターンは重要ではありません。私が本当にしたいのは、スペース (およびおそらく他のもの) の置換/削除だけです。
答え1
文字列置換を展開する初めそれを引数に保存して、hyperref
の\href
:
\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}
\newcommand\phone[1]{%
\StrSubstitute{#1}{ }{}[\firstarg]% Store first substitution in \firstarg
\StrSubstitute{#1}{ }{\,}[\secondarg]% Store second substitution in \secondarg
\href{tel:\firstarg}{\secondarg}% Use stored arguments in \href
}
\begin{document}
\href{tel:0123456789}{01\,23\,45\,67\,89}
\phone{01 23 45 67 89}
\end{document}
答え2
コマンドxstring
は拡張可能ではないため、通常は他のコマンド内でインラインで使用することはできません。ここでは、単純な拡張可能な置換を使用できます。
\documentclass{minimal}
\usepackage{hyperref}
\makeatletter
\def\zza#1 {#1\zza}
\def\zzb#1 {#1\,\zzb}
\newcommand\phone[1]{{\def\!##1{}\def\$##1##2{}\href{tel:\zza#1\! }{\zzb#1\$ }}}
\makeatother
\begin{document}
\href{tel:0123456789}{01\,23\,45\,67\,89}
\phone{01 23 45 67 89}
\end{document}
答え3
これらの場所では使用できません\StrSubstitute
。置換後の文字列が生成されるのではなく、その文字列を生成するためのかなり複雑な一連の命令が生成されるからです。
数字のペアの間にスペースを入力する必要がない、より複雑なソリューションです。そのため、スペースを忘れた場合でも機能します。
\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand{\phone}{m}
{
\dlichti_phone:n { #1 }
}
\tl_new:N \l_dlichti_phone_href_tl
\tl_new:N \l_dlichti_phone_print_tl
\cs_new_protected:Nn \dlichti_phone:n
{
\tl_set:Nx \l_dlichti_phone_href_tl { #1 }
% remove all spaces
\tl_replace_all:Nnn \l_dlichti_phone_href_tl { ~ } { }
% save a copy
\tl_set_eq:NN \l_dlichti_phone_print_tl \l_dlichti_phone_href_tl
% insert a thin space between any pair of digits
\regex_replace_all:nnN
{ ([0-9][0-9]) } % two digits followed by another digit
{ \1\c{,} } % the same with \, in between
\l_dlichti_phone_print_tl
% remove the trailing \,
\regex_replace_once:nnN { \c{,} \Z } { } \l_dlichti_phone_print_tl
\dlichti_phone_href:VVV
\c_colon_str
\l_dlichti_phone_href_tl
\l_dlichti_phone_print_tl
}
\cs_new_protected:Nn \dlichti_phone_href:nnn
{
\href{tel#1#2}{#3}
}
\cs_generate_variant:Nn \dlichti_phone_href:nnn { VVV }
\ExplSyntaxOff
\begin{document}
\phone{01 23 45 67 89}
\phone{0123456789}
\end{document}