Created
April 5, 2025 10:33
-
-
Save zr-tex8r/758f1ea85f3687ac7bae6636e4ae8315 to your computer and use it in GitHub Desktop.
TeX:TeX言語で整数が奇数であるかを判定する(和文組版編)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% plain pTeX文書 | |
% 準備 | |
\newif\ifIsOdd | |
\newcount\myCount | |
\newbox\myBox | |
\font\myFont=jis | |
%% \CheckOdd<整数n>: nが奇数であるかを判定し, 結果を | |
% スイッチ \ifIsOdd に返す. | |
\def\CheckOdd#1{% | |
\begingroup | |
\myFont % "普通の"和文フォントを指定 | |
% 行長が2文字の環境でn文字の"春"からなる段落を組む. | |
\setbox0\vbox{% | |
\hsize2zw | |
\noindent % 段落下げを消す | |
\myCount=#1\relax | |
\loop\ifnum\myCount>0 % n回ループ | |
春% | |
\advance\myCount-1 | |
\repeat | |
}% | |
% box0の中の最終行の水平ボックスを \myBox に取り出す. | |
\setbox0\vbox{% 出力は不要なのでボックス代入とする | |
\unvbox0 % 中身を放出する | |
\global\setbox\myBox\lastbox % 最後のものを抽出 | |
}% | |
% 最終行の文字だけを \myBox に取り出す | |
\global\setbox\myBox\hbox{% | |
\unhbox\myBox | |
\unskip\unskip % 段落構築で入った水平空きを除去 | |
}% | |
\endgroup | |
\IsOddfalse | |
\ifdim\wd\myBox=1zw % 最終行が1文字ならnは奇数 | |
\IsOddtrue | |
\fi | |
} | |
%----------------------------------------------------------- | |
% 1から100までの整数についてテストする. | |
\myCount=0 \loop\ifnum\myCount<100 | |
\advance\myCount1 | |
\the\myCount は | |
\CheckOdd{\myCount}% | |
\ifIsOdd 奇数\else 偶数\fi | |
です。\endgraf | |
\repeat | |
\bye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment