2009-07-11 27 views
7

Trong T E, làm cách nào để tách chuỗi như \mana{X1 2W/UB R /G} thành các phần và đưa chúng sang macro khác (trong trường hợp này) \m{X}\m{12}\m{W/U}\m{B}\m{R/G}), nhóm theo quy tắc rất đơn giản, cụ thể là: a) các chữ số liên tiếp tạo thành một nhóm, b) dấu gạch chéo tạo một nhóm gồm hai ký tự lân cận, c) dấu cách sẽ bị tước hoàn toàn?TeX: phân tích cú pháp chuỗi và khoảng trống trong khoảng trắng

Tôi đã thử gói substr nhưng không quá hữu ích, chỉ cho phép một người tìm thấy một số chất nền nhất định. Các vòng viết tay như

\def\lcpass#1.{} 
\def\lcloop#1#2.{% 
    \ifempty{#2}% 
     #1% 
     \let\continue=\lcpass% 
    \else% 
     \let\continue=\lcloop% 
    \fi% 
    \continue#2.} 

\def\lastchar#1{\lcloop#1.} % returns the last character of a string 

không hoạt động khi chuỗi kết thúc trong khoảng trắng, tôi cũng không thành công với \futurelet.

Nói chung, cách tiếp cận tác vụ phân tích chuỗi trong T E X là gì? Ví dụ: gói texmate cho phép một người viết những thứ như |1 e4 e5 Nf3 Nc6| và tự động rút các vị trí cờ tướng tương ứng. Nó làm như thế nào? Tôi có thể đọc gì về việc lặp qua các ký tự trong một chuỗi và các số khác TE X như thế này?

Trả lời

3

Dưới đây là những gì tôi cuối cùng đã làm để nhập MTG mana trong TeX/LaTeX:


% http://www.tug.org/TUGboat/Articles/tb28-1/tb88glister.pdf 

\newif\[email protected]@digit 

\def\@[email protected]#1{% lame, I know, but nothing else worked 
    \@[email protected]% 
    \def\@[email protected]{0}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{1}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{2}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{3}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{4}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{5}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{6}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{7}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{8}\ifx\@[email protected]#1\@[email protected]\fi% 
    \def\@[email protected]{9}\ifx\@[email protected]#1\@[email protected]\fi% 
    } 


\catcode`\^^G=12 

\newcommand*{\@[email protected]}[2]{% 
    \let\@[email protected]=#1% 
    \def\@[email protected]{#2}% 
    \ifx\@[email protected]\@empty% 
    \else% 
     \@@[email protected]#2^^G% 
    \fi% 
} 

\def\@@[email protected]#1#2^^G{% 
    \def\@[email protected]{#1}% 
    \def\@[email protected]{#2}% 
    \ifx\@[email protected]\@empty% 
     \let\@[email protected]=\@gobble% 
    \else% 
     \@[email protected]{#1}% 
     \ifx\@[email protected]\@empty% 
      \let\@[email protected]=\@gobble% 
     \else% 
      \let\@[email protected]=\@@[email protected]% 
     \fi% 
    \fi% 
    \@[email protected]#2^^G% 
} 

\catcode`\^^G=15 

% these are meant to be redefined 
\def\[email protected]#1{(\MakeUppercase{#1})} 
\def\[email protected]@colored#1{\[email protected]{#1}} 
\def\[email protected]@colorless#1#2{\[email protected]{#1#2}} 
\def\[email protected]@hybrid#1#2{\[email protected]{#1/#2}} 

\gdef\@[email protected]{.} 
\gdef\@[email protected]{/} 
\let\@[email protected]=\@[email protected] 
\let\@[email protected]=\@[email protected] 

\def\@[email protected]#1{% 
    \gdef\@[email protected]{#1}% 
    \ifx\@[email protected]\@[email protected]% 
    \else% 
     \ifx\@[email protected]\@[email protected]% 
      \[email protected]@hybrid{\@[email protected]}{\@[email protected]}% 
      \let\@[email protected]=\@[email protected]% 
      \let\@[email protected]=\@[email protected]% 
     \else% 
      \@[email protected]\@[email protected]\[email protected]@digit% 
       \@[email protected]\@[email protected]\[email protected]@digit% 
        \ifx\@[email protected]\@[email protected]% 
         \[email protected]@colorless{}{\@[email protected]}% 
        \else% 
         \[email protected]@colorless{\@[email protected]}{\@[email protected]}% 
         \let\@[email protected]=\@[email protected]% 
        \fi% 
       \else% 
        \[email protected]@colorless{}{\@[email protected]}% 
       \fi% 
      \else% 
       \[email protected]@colored{\@[email protected]}% 
      \fi% 
     \fi% 
    \fi% 
    \let\@[email protected]=\@[email protected]% 
    \let\@[email protected]=\@[email protected]% 
} 

\newcommand{\mana}[1]{% 
\let\@[email protected]=\@[email protected]% 
\let\@[email protected]=\@[email protected]% 
\@[email protected]{\@[email protected]}{#1\@[email protected]\@[email protected]}} 
+0

gì sai với câu trả lời của tôi không? Nó trông nhỏ hơn. –

+0

Không có gì sai, nhưng tôi đã có thể tìm ra giải pháp này trước khi tôi có cơ hội nhìn vào bạn. –

3

Như thường lệ, một câu hỏi đặt ra là một nửa giải pháp. Dưới đây là những gì tôi tìm thấy sau khi viết bài trên:

http://www.tug.org/TUGboat/Articles/tb28-1/tb88glister.pdf

“Một tài sản bất hạnh của \allchars là nó loại bỏ tất cả các không gian trong chuỗi ban đầu,” nó nói. Ha ha.

6
\def\m#1{\par$m$({\tt #1})}% Any macros 
\def\removespaces{\catcode`\ =9 }% Ignore all spaces` 

\let\manaNext\relax % aux def 
\let\manaLastChar\relax % aux def 
\newtoks\manaToks % aux toks 
\newif\ifDigitProcessing 

\def\mana#{\afterassignment \manaA \let\next= }% always next = { 
\def\manaA{\bgroup \removespaces \let\manaNext\manaB \manaNext}% algorithm init: ignore spaces 
\def\manaB{\futurelet\next\manaC}% algorithm start 
\def\manaC{\ifx\next\egroup \def\nnext{\manaFlush\aftergroup\manaNext}\else\let\nnext\manaD\fi\nnext}% check for \egroup 
\def\manaD{\ifx\next/\let\nnext\manaSlash\else \ifcat\next 1\let\nnext\manaDigit \else \let\nnext\manaE \fi\fi \nnext}% cases 
\def\manaE#1{\manaFlush\DigitProcessingfalse\let\manaLastChar\next\manaNext}% Letters A-Z and a-z case 
\def\manaFlush{\ifx\manaLastChar\relax\else\m{\manaLastChar}\fi\let\manaLastChar\relax 
       \ifDigitProcessing\expandafter\m\expandafter{\the\manaToks}\fi\manaToks{}}% transform to \m{...} 
\def\manaSlash#1#2{\m{\manaLastChar/#2}\let\manaLastChar\relax\manaNext}%#1=/, #2=next letter 
\def\manaDigit#1{\ifDigitProcessing\else\manaFlush\fi 
     \manaToks=\expandafter{\the\manaToks#1}\DigitProcessingtrue\manaNext}% 0-9 case 

\hrule\medskip 
\mana{X1 2W/UB R /G} 
\medskip\hrule\medskip 

\mana{X1 2W/UB s/SS 14 1 R /G XZ} 
\medskip\hrule\medskip 
Các vấn đề liên quan