2009-07-11 10 views
7

T E Xでは、\mana{X1 2W/UB R /G}などの文字列を分割して別のマクロにどのように分割することができますか(この場合、マクロ呼び出しを次のように置き換えます\m{X}\m{12}\m{W/U}\m{B}\m{R/G})、非常に単純なルールでグループ分けします:a)連続した数字がグループを形成します。b)スラッシュが2つの近くの文字のグループを作成します。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|のようなものを書くことができ、対応するチェスの位置を自動的に描画します。それはどうですか?文字列やその他の文字をループすることについては何を読むことができますか? 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\@mt[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

私の答えの何が問題?それは小さく見える。 –

+0

何も間違っていませんが、あなたのことを見る機会が来る前に、この解決策を見つけ出すことができました。 –

3

よくあることですが、うまくいく質問は解決策の半分です。ここで私は上記の記事を書いた後、見つけたものです:

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

\allcharsの一つ残念な性質は、それが元の文字列内のすべてのスペースを破棄していることで、」そ​​れは言います。ハハ。

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