0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで作る青チャート風例題の作り方

Last updated at Posted at 2025-09-16

目的

高校〜大学初年レベルの数学問題を、日本語テキスト+数式(mathtext) を組み合わせて、美しく「青チャート風の例題枠」を描画します。
LaTeX を完全にインストールする必要はなく、matplotlib 内蔵の mathtext を利用するため、Google Colab や Jupyter でそのまま動きます。


実装コード


!apt-get -y install fonts-ipaexfont

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.font_manager as fm
from matplotlib import rcParams

# ================= フォント設定 =================
font_path = "/usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf"
jp_font = fm.FontProperties(fname=font_path)

rcParams["text.usetex"] = False  # LaTeX は使わず mathtext のみ

title_text = "部分積分と女子枠"
example_number = "例題90"

# --- 問題文を日本語+mathtext で分けて記述 ---
problem_text = (
    "(1) 減衰項と三角関数の微分積分\n"
    "    (a) $f(t) = e^{-at}\\sin(bt)$ を微分せよ。\n"
    "    (b) $\\int e^{-at}\\sin(bt)\\,dt$ を積分せよ。\n\n"
    "(2) 女子枠特別問題\n"
    "    女性限定試験の意義を「性差による不利益や制約からの解放」\n"
    "    という観点で100文字以内で説明せよ。"
)

# ================= 描画 =================
fig, ax = plt.subplots(figsize=(8, 3))
ax.axis("off")

# 外枠
frame_x, frame_y, frame_w, frame_h = 0.05, 0.1, 0.9, 0.8
rect = patches.Rectangle((frame_x, frame_y), frame_w, frame_h,
                         linewidth=2, edgecolor="red", facecolor="white")
ax.add_patch(rect)

# 左の赤帯
red_band = patches.Rectangle((frame_x, 0.9), 0.1, 0.1,
                             linewidth=0, facecolor="red")
ax.add_patch(red_band)

# 左帯の文字
ax.text(frame_x+0.005, 0.93, "基本", color="white", fontsize=12,
        fontweight="bold", va="center", fontproperties=jp_font)

# 例題番号
ax.text(0.17, 0.93, example_number, color="black", fontsize=12,
        fontweight="bold", va="center", fontproperties=jp_font)

# タイトル
ax.text(0.35, 0.93, title_text, color="black", fontsize=12,
        va="center", fontproperties=jp_font)

# 右上の丸数字
ax.text(0.92, 0.93, "①②③④⑤", color="red", fontsize=12,
        ha="right", va="center", fontproperties=jp_font)

# 問題文
ax.text(frame_x+0.02, frame_y+frame_h-0.10, problem_text,
        fontsize=11, va="top", ha="left", wrap=True,
        fontproperties=jp_font, clip_on=True)

plt.show()

✨ 出力例

  • 上部に赤帯+「基本」+「例題番号」+タイトル
  • 右上に丸数字
  • 内部に日本語問題文+LaTeX風数式
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?