\href 내부의 매크로 확장

\href 내부의 매크로 확장

바로가기를 만들고 싶습니다.

\href{tel:0123456789}{01\,23\,45\,67\,89}

\StrSubstitute패키지 에서 xstring다음과 같이 사용할 수 있습니다 .

\StrSubstitute{01 23 45 67 89}{ }{\,}

에 대한 두 번째 인수 내부 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}

여기에 이미지 설명을 입력하세요

관련 정보