KTUG 한국 텍 사용자 그룹

Menu

KTUG :: Q&A 마당

\(backslash)는 regex의 escape character입니다. 즉 예를 들어 \a를 매치하려면 \\a로 해야 합니다. 그리고 interface3에 잘 나와 있는 바와 같이 \\a는 문자 \와 문자 a를 의미하지 TeX의 control sequence를 의미하지 않습니다. TeX cs는 반드시 \c{...}로 쓰라고 하고 있죠. 텍 매크로 \in 은 의미상 {control sequence: i n}인 것이지 {characters: \\ i n}이 아니기 때문입니다.
  (그래서 백슬래시 두 개로 이루어진 명령 \\은 \c{\\}로 매치해야 할 겁니다.)

regex만을 사용해서 예시된 명령을 완성해본다면 (여러 방법이 있을 것이므로 그냥 참고만 하십시오), 다음처럼 하면 대략 의도대로 동작할 듯도 합니다. 핵심 아이디어는 인자로 들어온 매크로에서 이름 부분(\를 제외한)을 추려내고 나중에 \c를 덧붙여준다는 것입니다.

\documentclass{article}
\ExplSyntaxOn
\tl_new:N \l__delim_tl
\regex_new:N \l__delim_regex
\cs_generate_variant:Nn \regex_set:Nn { NV }

\bool_new:N \g_mycheck_bool

\NewDocumentCommand \SetDelim { m }
  {
%%% is #1 a control sequence?
    \int_compare:nTF { \tl_count:n { #1 } > 1 }
      {%% just the last token is to be taken
        \tl_set:Nn \l_tmptmp_tl { #1 }
        \tl_reverse:N \l_tmptmp_tl
        \tl_set:Nx \l_tmptmp_tl { \tl_head:N \l_tmptmp_tl }
      }
      {
        \tl_set:Nn \l_tmptmp_tl { #1 }   %% \tl_set_eq:NN is not a good choice.
	                                 %% because we will expand once this macro.
      }
	
      \exp_last_unbraced:No
      \token_if_cs:NTF \l_tmptmp_tl
        { %% #1 is a macro
          \bool_gset_true:N \g_mycheck_bool
          \tl_gset:Nx \l__delim_tl { {	%% the double braces are requred to use \c
             \exp_last_unbraced:Nf \use_i:nnn \cs_split_function:N #1
            } }
        }
	{ %% #1 is not a macro
          \tl_gset:Nn \l__delim_tl { #1 }
          \bool_gset_false:N \g_mycheck_bool
	}
  }

\NewDocumentCommand \SplitText { m }
  {
    \bool_if:NT \g_mycheck_bool {
      \tl_put_left:Nn \l__delim_tl { \c }
    }

    \regex_set:NV \l__delim_regex \l__delim_tl
    \regex_split:NnN \l__delim_regex { #1 } \l_tmpa_seq
    \seq_map_inline:Nn \l_tmpa_seq {
      \fbox{ \ttfamily ##1 }
    }
  }
\ExplSyntaxOff

\begin{document}

\SetDelim{splithere}
\SplitText{a{}splithere{}A}

\SetDelim{\splitthere}
\SplitText{a\splitthere B}

\end{document}

 

KTUG 한국 텍 사용자 그룹