1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TeXで四則演算する方法

1
Last updated at Posted at 2026-05-10

こんにちは、らーてくすです。
今回は初記事ですが、どうにかTeX言語GW特別キャンペーンに間に合わせたく製作しました。
というわけで内容に入っていきましょう。
TeXはチューリング完全とされています。
チューリング完全ならば、「あらゆる計算を記述・実行できる能力」を持ちます。
CやC++と言ったプログラミング言語はチューリング完全です。
プログラミング言語で計算ができるように、TeXも電卓として使うことができます。
それを用いて実際に計算をしていきましょう。

事前準備

環境: TeX Live 2025, Git Bash
ターミナル内で対話方式を用いて計算していきます。

対話方式に持ち込む方法

lartex /  $ etex
This is pdfTeX, Version 3.141592653-2.6-1.40.28 (TeX Live 2025) (preloaded format=etex)
 restricted \write18 enabled.
**\relax
entering extended mode

*

ターミナルで、eTeXと入力し、\relaxと入力してください。

eTeXを起動した際に現れる
entering extended modeは、
eTeXにより、拡張機能(大量のレジスタなど)が使える状態になったことを示しています。

TeXeTeXについて

TeXを電卓として使う際、レジスタという概念を用います。
なぜ、TeXでなく、eTeXを用いるのでしょうか。
TeXのレジスタ数は256しかなく、\advanceなどの基本的な、難解な計算方法しかないからです。
それに比べてeTeXは、レジスタ数が32768個あり、\numexprといった直感的な計算方法もあるからです。
レジスタとは、プログラミング言語でいうところの 「変数」 にあたります。
TeXにおいては数値を一時的に保存しておくための「番号付きの箱」だと考えてください。
LaTeXも内部はeTeXが利用されています。
今回はTeXeTeX、両者を用いて計算していきましょう。

レジスタの一種である\newcountを今回は頻繁に使います。
\newcountは、\countレジスタを確保するための命令で、整数を扱うための 「カウントレジスタ」を新しく割り当てる命令です。
\newcountはページ番号やカウンタ等で一般的には使われます。

TeXで四則計算

**\relax
entering extended mode
*\newcount\A
*\newcount\B
*\newcount\result
*\A=1
*\B=3
*\result=\A
*\advance\result by \B
*\message{Result is \the\result}
Result is 4
*\bye
No pages of output.
Transcript written on texput.log.

TeXで、ABresultと三つのカウントを定義します。
Aに1を、Bに3を代入します。
resultA、すなわち1を代入します。
resultの値をB、すなわち3を加算させます。
つまり、ここでresultの値が4となります。
\message{}を用いることで、ターミナル内に文字列を表示させます。
\the\resultと書くことで、\resultの数値を取り出します。
もし、\result と書いてしまうと、数値ではなく『レジスタそのもの』を指してしまい、正しく表示されません。
\byeで、TeXを終了させます。

**\relax
entering extended mode
*\newcount\A
*\newcount\B
*\newcount\result
*\A=1
*\B=3
*\result=\A
*\advance\result by -\B
*\message{Result is \the\result}
Result is -2
*\bye
No pages of output.
Transcript written on texput.log.

TeXで、ABresultと三つのカウントを定義します。
Aに1を、Bに3を代入します。
resultA、すなわち1を代入します。
resultの値をB、すなわち3を減算させます。
つまり、ここでresultの値が-2となります。

**\relax
entering extended mode
*\newcount\A
*\newcount\B
*\newcount\result
*\A=5
*\B=10
*\result=\A
*\multiply\result by \B
*\message{Result is \the\result}
Result is 50
*\bye
No pages of output.
Transcript written on texput.log.

TeXで、ABresultと三つのカウントを定義します。
Aに5を、Bに10を代入します。
resultA、すなわち5を代入します。
resultの値をB、すなわち10を乗算させます。
つまり、ここでresultの値が50となります。

**\relax
entering extended mode
*\newcount\A
*\newcount\B
*\newcount\result
*\A=30
*\B=5
*\result=\A
*\divide\result by \B
*\message{Result is \the\result}
Result is 6
*\bye
No pages of output.
Transcript written on texput.log.

TeXで、ABresultと三つのカウントを定義します。
Aに30を、Bに5を代入します。
resultA、すなわち30を代入します。
resultの値をB、すなわち5を除算させます。
つまり、ここでresultの値が6となります。
この場合は切り捨て1が適用されます

eTeXで四則計算

**\relax
entering extended mode
*\newcount\result \result=\numexpr 5+3
*\message{Result is \the\result}
Result is 8
*\bye
No pages of output.
Transcript written on texput.log.

TeXでカウント、resultを定義します。
\resultに、eTeXの計算機能を利用可能にする\numexpr
利用して、5+3の計算結果を代入します。
よって、\resultには8が代入されたので、
Result is 8となります。

**\relax
entering extended mode
*\newcount\A \newcount \B \newcount\result \A=3 \B=5
*\result=\numexpr \A * \B + 3 / ( 3 - 1)
*\message{Result is \the\result}
Result is 17
*\bye
No pages of output.
Transcript written on texput.log.

TeXでカウント、resultを定義します。
四則計算の基本である、+ - * /を利用できます。
また、この場合は四捨五入2が適用されます。
$ A = 3, B = 5 $
$ A \times B + \frac{3}{ 3 - 1 } $
をこの式で計算します。
よって、
$ 3 \times 5 + 1.5 $
四捨五入より、
$ 15 + 2 = 17 $
よって、答えは17です。

おわりに

今回はTeXの基本命令とeTeXの拡張機能を使った四則演算を紹介しました。
「なぜわざわざTeXで計算を?」と思うかもしれません。
しかし、これらを知っておくと、自作のマクロで自動的に図形を描いたり、
ページ番号に応じた複雑な処理を自動化したりと、組版の自由度が飛躍的に上がります。
TeXはただの文書作成ツールではなく、強力なプログラミング言語でもあります。
最後まで読んでいただきありがとうございました!

注意と参考

  1. 0の方向への丸め(Truncate)とされる

  2. 正確には「絶対値の四捨五入(Round away from zero)」です。$1.5$ は $2$ に、$-1.5$ は $-2$ になります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?