TeX処理系の気に食わない点(0) — エラー時の挙動

明治維新で新政府がTeXを旧来の陋習として弾圧したとき、TeX使いたちはわざとTeXの使い勝手を悪くすることで難を逃れたと伝えられています。 — TeXしぐさ

↑「適当にでっち上げた嘘です」と注記しようと思ったが元ネタも適当にでっち上げた嘘だった

TeXの気に食わない点…はきっと皆さんいろいろあると思うが、ここではTeX処理系の挙動に絞った話をする。ここに書いた挙動に関しては、TeXpdfTeXXeTeXも大差はないようである。

エラー時の挙動

結果だけ知りたい人は下の方のまとめを見てね。

TeXはデフォルトで、エラーが起こった時にユーザーの入力を求める。例えば、\hogeという未定義のコマンドを含むファイルをコンパイルしてみよう。

$ cat test.tex⏎
\hoge
$ tex test.tex⏎
This is TeX, Version 3.14159265 (TeX Live 2014) (preloaded format=tex)
(./test.tex
! Undefined control sequence.
l.1 \hoge
         
? ←なんでいちいちユーザーに応答を求めるんだ。一人じゃ何もできない子供か。⏎
Type <return> to proceed, S to scroll future error messages,
R to run without stopping, Q to run quietly,
I to insert something, E to edit your file,
1 or ... or 9 to ignore the next 1 to 9 tokens of input,
H for help, X to quit.
? X⏎
No pages of output.
Transcript written on test.log.
$ 

このように、TeXはいろいろ選択肢を提示してくるが、正直言ってTeXのコンパイルを終了し、元々使っていたエディタに戻ってファイルを修正する」以外の行動(選択肢としては、上の実行例のように X⏎ で終了)を取る人がいるのか疑問である。

しかし、texはこれだけ広く使われているプログラムなのだから、きっと何かこの挙動を変えるオプションがあるはずである。tex -helpを実行してみよう。

$ tex -help⏎
Usage: tex [OPTION]... [TEXNAME[.tex]] [COMMANDS]
   or: tex [OPTION]... \FIRST-LINE
   or: tex [OPTION]... &FMT ARGS
  Run TeX on TEXNAME, usually creating TEXNAME.dvi.
(略)

  If no arguments or options are specified, prompt for input.

-enc                    enable encTeX extensions such as \mubyte
[-no]-file-line-error   disable/enable file:line:error style messages
-fmt=FMTNAME            use FMTNAME instead of program name or a %& line
-halt-on-error          stop processing at the first error
-ini                    be initex, for dumping formats; this is implicitly
                          true if the program name is `initex'
-interaction=STRING     set interaction mode (STRING=batchmode/nonstopmode/
                          scrollmode/errorstopmode)
-ipc                    send DVI output to a socket as well as the usual
                          output file
(略)

Email bug reports to tex-k@tug.org.
$ 

長々とオプションが出てきたが、関係ありそうなのは

-halt-on-error          stop processing at the first error

-interaction=STRING     set interaction mode (STRING=batchmode/nonstopmode/
                          scrollmode/errorstopmode)

である。では -halt-on-error を指定してみよう。

$ tex -halt-on-error test.tex⏎
This is TeX, Version 3.14159265 (TeX Live 2014) (preloaded format=tex)
(./test.tex
! Undefined control sequence.
l.1 \hoge
         
No pages of output.
Transcript written on test.log.
$ 

そうだ、それでいい。これで万事解決。TeX処理系も大人になったものだ。

…と、そうは問屋が卸さない。「存在しないファイルを\inputさせる」とどうなるか見てみよう。

$ cat test2.tex⏎
\input hoge.tex
$ tex -halt-on-error test2.tex⏎
This is TeX, Version 3.14159265 (TeX Live 2014) (preloaded format=tex)
(./test2.tex
! I can't find file `hoge.tex'.
l.1 \input hoge.tex
                   
(Press Enter to retry, or Control-D to exit)
Please type another input file name: ^D
! Emergency stop.
l.1 \input hoge.tex
                   
No pages of output.
Transcript written on test2.log.
$ 

なんてことだ、-halt-on-errorを指定したのにエラーで止まりやがった。TeX処理系はまだ親離れできない子供だった。

では今度は-interactionを指定してみよう。だがちょっと待ってほしい。-interactionの引数には何を指定すれば良いのだ?tex -help の内容を思い出してみよう。

-interaction=STRING     set interaction mode (STRING=batchmode/nonstopmode/
                          scrollmode/errorstopmode)

これによると、“batchmode”/“nonstopmode”/“scrollmode”/“errorstopmode” の4つのうちいずれかを指定すればよいらしい。だが、それぞれのオプションの意味が書いてない。

これらのオプションは実はTeXのinteraction mode assignmentコマンドの\batchmode \nonstopmode \scrollmode \errorstopmodeに対応していて、The TeXbookとかTeX by Topicを読めばそれらの意味が書いてある。最初の実行例で言うと

Type <return> to proceed, S to scroll future error messages←\scrollmode,
R to run without stopping←\nonstopmode, Q to run quietly←\batchmode,
I to insert something, E to edit your file,
1 or ... or 9 to ignore the next 1 to 9 tokens of input,
H for help, X to quit.

という対応になる。\errorstopmodeは最初の実行例のように「ユーザーに指示を仰ぐ」モードとなる。

ググった感じではこのへん-interactionの挙動についてよくまとまっている感じだった。

まとめ

エラー時の挙動については、このへんがよくまとまっていて、要するに

-halt-on-error -interaction=nonstopmode -file-line-error

を指定しておけばいいらしい。(-file-line-errorはエラーメッセージに

./test.tex:1: Undefined control sequence.

のようにファイル名と行番号を追加してくれる)

本当はTeX処理系の気に食わない挙動についてもう1個書こうと思ったが、エラー時の挙動について思ったより長くなったので、記事を分けてまた今度書くことにする。


TeX処理系の気に食わない点(0) — エラー時の挙動」への4件のフィードバック

  1. @kaizen_nagoya

    LaTeXで書いたファイルの不具合が取れないことがしばしばあり、資料を整理しはじめました。URLを記載させてください。続きを期待しています。

    返信
  2. honntex

    エラーの挙動については、
    おそらくUNIX時代の名残りだと思います。
    時間でマシンの使用料がかかったものですから、出来るだけ処理の回数を減らすために、即時修正が出来たのでしょう。
    本当にそれが理由かは分かりませんが、TEXブックに料金うんぬんの記述があります。

    返信
  3. ピンバック: TeX の実行あれこれ | 雑記帳

  4. ピンバック: TeX 実行の自動化ツールを作った (ClutTeX) | 雑記帳

honntex へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です