2009-07-11 56 views
7

在叔ë X,它是如何可以將字符串,分割爲\mana{X1 2W/UB R /G}成零件並把它們饋送到另一個宏(在這種情況下,通過像替換宏調用\m{X}\m{12}\m{W/U}\m{B}\m{R/G}),按照非常簡單的規則進行分組,即:a)連續的數字組成一個組,b)斜線創建一組兩個附近的字符,c)空格將被完全剝離?的TeX:字符串解析和空白剝

我試過substr包,但它並沒有太大的幫助,只允許找到某些子字符串。手寫的循環如

\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 

失敗時的字符串中的空格結束工作,也不是我成功的\futurelet

一般來說,如何處理解析T E X中的字符串的任務?例如,texmate包允許你寫一些東西,如|1 e4 e5 Nf3 Nc6|,並自動繪製相應的國際象棋位置。它是如何做到的?有什麼可以閱讀關於字符串中的字符循環和其他T E X這樣的黑客?

回答

3

這是我最終沒有在的TeX/LaTeX的進入MTG法力:


% 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][email protected]% 
       \@[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

什麼毛病我的答案嗎?它看起來更小。 – 2009-08-27 08:28:32

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