문자열로 된 인자 두 개를 받아 비교한다. 두 인자가 같으면 0, 앞의 것이 더 크면(순서가 나중이면) 1, 뒤의 것이 더 크면 -1을 돌려준다. 다음 예는 -1을 출력한다.
\pdfstrcmp{aaa}{bbb}
이를 이용해 아래와 같이 문자열 소팅을 구현할 수 있다. 필자는 알고리듬에 대해선 잘 모른다. 따라서 코드가 효율적이지는 않을 것이다. pdflatex, xelatex, lualatex에서 모두 작동한다.
\documentclass{article} \usepackage{kotex} \makeatletter \def\sort#1{\toks@{}\count@=0 \dosort#1{@@@}} \def\dosort#1#2{% \ifnum\pdfstrcmp{#2}{@@@}=0 % 끝에 도달 \toks@\expandafter{\the\toks@{#1}}% \ifnum\count@>0 \def\temp{\expandafter\sort\expandafter{\the\toks@}}% 한번 더 돌자 \else \def\temp{\expandafter\print\the\toks@{@@@}}% 최종출력 \fi \else \ifnum\pdfstrcmp{#1}{#2}>0 % #1이 #2보다 크면 순서를 바꾼다 \advance\count@ by1 \toks@\expandafter{\the\toks@{#2}}% \def\temp{\dosort{#1}}% \else \toks@\expandafter{\the\toks@{#1}}% \def\temp{\dosort{#2}}% \fi \fi \temp } \def\print#1{\ifnum\pdfstrcmp{#1}{@@@}=0 \else #1 \expandafter\print\fi} \makeatother \begin{document} \sort{{yyy}{zzz}{xxx}} \sort{{마}{바}{가}{다}{사}{나}{라}} \end{document}
KTUG 한국 텍 사용자 그룹