Skip to content

Instantly share code, notes, and snippets.

@kenoss
Created May 15, 2013 13:19
Show Gist options
  • Save kenoss/5583939 to your computer and use it in GitHub Desktop.
Save kenoss/5583939 to your computer and use it in GitHub Desktop.
TeXで括弧の対応をとる, 上手くいかない例.
% -*- coding: euc-jp -*-
\documentclass[10pt,draft]{jsarticle}
%\usepackage{amsmath,amssymb}
%\usepackage{kmathmacro}
%\usepackage{etex}
%\usepackage{etoolbox}
\makeatletter
\def\adicsp{%
\futurelet\@let@token\adicsp@aux
}
\def\adicsp@aux{%
\ifx\@let@token(
\let\@next\adicsp@call@getcorrparen
\else
\let\@next\adicspof
\fi
\@next
}
\def\adicsp@call@getcorrparen{%
\getcorrparen\adicsp@recieve
}
\def\adicsp@recieve{%
\gcp@@result^{\mathrm{ad}}%
}
\def\adicspof#1{%
#1^{\mathrm{ad}}%
}
\newcount\gcp@@count
\def\getcorrparen#1{%
\gcp@begin#1%
}
\def\gcp@begin#1{%
\begingroup
\let\gcp@return#1
\gcp@@count = 0
\def\gcp@@res{}%
%% この部分をかえると動作が変わる(当たり前)
% \catcode`\ =12
\gcp@loop%
}
\def\gcp@const@open{(}
\def\gcp@const@close{)}
\def\gcp@counting{%
\ifx\@tempa\gcp@const@close
\advance \gcp@@count by -1
\else\ifx\@tempa\gcp@const@open
\advance \gcp@@count by 1
\fi\fi
}
\def\gcp@loop#1{%
\def\@tempa{#1}
%\show\@tempa
%\showthe\gcp@@count
\gcp@counting
\toks0\expandafter{\gcp@@res}%
% \toks2\expandafter{\@tempa}
% \toks2\expandafter{#1}%
\toks2{#1}%
\edef\gcp@@res{\the\toks0 \the\toks2}%
%\showthe\gcp@@count
\show\gcp@@res
\ifnum\gcp@@count>0
\let\@next\gcp@loop
\else
\let\@next\gcp@end
\fi
\@next
}
\def\gcp@end{%
\catcode`\ =10
% \expandafter\gdef\expandafter\gcp@@result{\gcp@@res}
\toks0\expandafter{\gcp@@res}
\global\edef\gcp@@result{\the\toks0}
\global\let\@next\gcp@return
\endgroup
\@next
}
\def\res#1 to #2;{#1|_{#2}}
\begin{document}
$\adicsp M$
$\adicsp(M)$ % OK
$\adicsp(A(B)C)$ % 括弧の対応は取れている
$\adicsp(A \adicsp(B) C)$ % これも問題はない
$\adicsp(A \adicsp B C)$ % 空白のcatcodeを12にすると意図しない動作になる
%$\adicsp(\res f to U ;)$ % 空白のcatcodeが10でも12でも駄目
%$\adicsp(\map f from X to Y ;) to Z ;$ % 多分同じ理由で駄目
\end{document}
\makeatother
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment