マクロツイーター

はてダから移行した記事の表示が崩れてますが、そのうちに直せればいいのに(えっ)

展開限定文脈でエラーを出す

LuaTeX の \directlua は展開可能である。だから、TeX のコードでは「展開限定」の文脈では不可能なことであっても、Lua コードでは可能な場合がある。典型的な例がレジスタへの代入である。


\newcount\xx@count
\xx@count=3
\edef\xx@test{\the\xx@count/% \edef の中は展開限定
\directlua{ tex.count["xx@count"] = 5 }% Lua 中で \xx@count=5 を実行
\the\xx@count} %=> \xx@test は「3/5」になる

「展開限定文脈」の中でエラーを出したいことは時々ある。ところが、\errmessage は展開可能でないので、普通の TeX では無理である。しかし LuaTeX では Lua コードを利用してこれができる。


\edef\xx@test{ABC%
\directlua{ tex.error("Error occurred in edef") }% tex.error() でエラー通知
DEF}
(これを実行すると以下のエラーが出る)
! Error occurred in edef.
l.4 ...tlua{ tex.error("Error occurred in edef") }
%
?

これだけでは何も面白くないので、LaTeX\PackageError\GenericErrror に相当するものを作ってみた。

次の命令を用意している。

  • \ixGenericError{<#1>}{<#2>}{<#3>}{<#4>}\GenericError と同様。
  • \ixPackageError{<#1>}{<#2>}{<#3>}{<#4>}\PackageError と同様。
  • \ixMessageBreak: 上の2つの命令の中では \MessageBreak の代わりにこれを使う。
  • \ixPlainError{<メイン>}{<ヘルプ>}: 本来の TeX と同様の単純な通知形式。

何故、\GenericError が「面白い」のかはきちんと説明する必要があると思うが、今は時間がないので止めておく。

一応、サンプルを示しておく。

[greektrans.sty]


\RequirePackage{ixerrtrick}
%% \GreekToEnglish{}
% Expands to an English translation of the Greek text .
\newcommand\GreekToEnglish[1]{%
\PT@notyet\GreekToEnglish % TODO: implement
It's all Greek to me.}
\def\PT@notyet#1{%
\ixPackageError{greektrans}{\string#1 not yet implemented}%
{Would you please implement this macro?}}

[test.tex]


% 文字コードは一応 UTF-8
\documentclass{article}
\usepackage{greektrans}
\edef\test{Test:
\GreekToEnglish{Εἶναι γιὰ μένα κινέζικα.}}
% ... 以下略

実行結果:


! Package greektrans Error: \GreekToEnglish not yet implemented.

See the greektrans package documentation for explanation.
Type H for immediate help.
...

l.5 ... ναι γιὰ μένα κινέζικα.}
}
? h
Would you please implement this macro?
?