%20%EB%8C%80%20xparse(l3)%EC%9D%98%20%ED%9A%A8%EC%9C%A8%EC%84%B1.png)
LaTeX2e
나는 문자열 처리가 사용 xstring
하거나 expl3
사용 중일 때 수행될 수 있음을 이해합니다 xparse
(원래는2013년 스레드 "토큰화 및 구문 분석") 한 문서에서(아마도 많은 컴파일과 많은 문서에 걸쳐) 상대적으로 간단한 문자열 구문 분석 매크로를 수백 번 실행할 예정이며, 어떤 방법이 패키지를 로드하는 데 고정된 시간을 할인하여 더 시간적 및/또는 계산적으로 효율적인지 알고 싶습니다. .
의 코드 에 대한 간략한 검토를 기반으로 한 근사치는 xstring
여기 3e 팀에서 가장 환영받을 것입니다. 정확한 결과에 가까운 것은 필요하지 않습니다. 이것은 나 자신의 호기심을 위한 것이며 어딘가에 있는 벽돌 벽을 긁어내려고 하지 않도록 하기 위한 것입니다.
또한 이 응용 프로그램이 2e와 3e 알고리즘을 비교하는 데 유용한지에 대한 의견에도 관심이 있습니다.
이것은 제가 사용하고 있는 매크로입니다 xstring
. 이는 핵심 단어와 문구에 대한 모든 언급의 첫 글자(이 데모에서는 처음 두 글자)를 매우 밝게 강조 표시하는 것입니다. 완전히 강조 표시하면 페이지가 복잡해집니다.
\documentclass{article}
\usepackage{xstring}
%%% capitalize first n letters of each word %%%
% apply operator#3 to string#1 with word separators #2
\newcommand{\Splitstrop}[3][ ]{%
\providecommand\csA{}%
\providecommand\csB{}%
\StrCut{#3}{#1}\csA\csB%
#2{\csA}%
\ifx\csB\empty\else{#1}\Splitstrop[#1]{#2}{\csB}\fi\relax}
% apply operation#3 on first #2 letters of string #1
\newcommand{\Leftstrop}[3][1]{#2{\StrLeft{#3}{#1}}\StrGobbleLeft{#3}{#1}\relax}
\newcommand{\Kw}[1]{\textsc{#1}} %first mention of keyword/phrase
\newcommand{\Kwd}[1]{\Leftstrop[2]{\Kw}{#1}} %single-word 2nd mention: apply \Kw to first two letters of the word (I plan to sc just the first letter, but this is more illustrative)
\newcommand{\Kwds}[1]{\Splitstrop[ ]{\Kwd}{#1}} % keyphrase 2nd mention: apply Kwd to each word (space is word separator)
\begin{document}
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the \Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1): don't say ``We define the \Kwd{embiggen} \Kwd{operator} as the \Kwd{operator} that \Kwd{embiggens}.''
\end{document}
답변1
나는 다음과 같은 예를 시도했습니다 l3benchmark
.
\documentclass{article}
\usepackage{xstring,l3benchmark}
%%% capitalize first n letters of each word %%%
% apply operator#3 to string#1 with word separators #2
\newcommand{\Splitstrop}[3][ ]{%
\StrCut{#3}{#1}\csA\csB
#2{\csA}%
\ifx\csB\empty\else{#1}\Splitstrop[#1]{#2}{\csB}\fi\relax
}
% apply operation#3 on first #2 letters of string #1
\newcommand{\Leftstrop}[3][1]{#2{\StrLeft{#3}{#1}}\StrGobbleLeft{#3}{#1}\relax}
\newcommand{\Kw}[1]{\textsc{#1}} %first mention of keyword/phrase
\newcommand{\Kwd}[1]{\Leftstrop[2]{\Kw}{#1}} %single-word 2nd mention: apply \Kw to first two letters of the word (I plan to sc just the first letter, but this is more illustrative)
\newcommand{\Kwds}[1]{\Splitstrop[ ]{\Kwd}{#1}} % keyphrase 2nd mention: apply Kwd to each word (space is word separator)
\ExplSyntaxOn
\cs_set_eq:NN \benchmark \benchmark:n
\ExplSyntaxOff
\begin{document}
\benchmark{
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the
\Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1):
don't say ``We define the \Kwds{embiggen operator} as the \Kwd{operator}
that \Kwd{embiggens}.''
}
\end{document}
그리고 expl3
버전
\documentclass{article}
\usepackage{xparse,l3benchmark}
\ExplSyntaxOn
\NewDocumentCommand{\Kw}{m}
{
\kompootor_textsc:n { #1 }
}
\NewDocumentCommand{\Kwd}{m}
{
\kompootor_split:Nnn \kompootor_textsc:n { 2 } { #1 }
}
\NewDocumentCommand{\Kwds}{m}
{
\kompootor_splitstrop:nNn { ~ } \kompootor_textsc:n { #1 }
}
\seq_new:N \l__kompootor_splitstrop_in_seq
\seq_new:N \l__kompootor_splitstrop_out_seq
\cs_new_protected:Nn \kompootor_splitstrop:nNn
{
\seq_set_split:Nnn \l__kompootor_splitstrop_in_seq { #1 } { #3 }
\seq_set_map:NNn \l__kompootor_splitstrop_out_seq \l__kompootor_splitstrop_in_seq
{ \kompootor_leftstrop:Nn #2 { ##1 } }
\seq_use:Nn \l__kompootor_splitstrop_out_seq { #1 }
}
\cs_new_protected:Nn \kompootor_leftstrop:Nn
{
\kompootor_split:Nnn #1 { 2 } { #2 }
}
\cs_new:Nn \kompootor_split:Nnn
{
#1 { \tl_range:nnn { #3 } { 1 } { #2 } }
\tl_range:nnn { #3 } { #2+1 } { -1 }
}
\cs_new_protected:Nn \kompootor_textsc:n { \textsc { #1 } }
\NewDocumentCommand{\benchmark}{+m}{\benchmark:n{#1}}
\ExplSyntaxOff
\begin{document}
\benchmark{
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the
\Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1):
don't say ``We define the \Kwds{embiggen operator} as the \Kwd{operator}
that \Kwd{embiggens}.''
}
\end{document}
결과는
0.00338 seconds (1.1e4 ops)
xstring
구현을 위해
0.00187 seconds (6.29e3 ops)
구현 을 위해 expl3
.
출력은 동일합니다( 제거 \benchmark
).