From 58f23a63c470a6e6b44ffd489ca14686a07ac5a5 Mon Sep 17 00:00:00 2001 From: "Erick C. Jones Jr." <44578073+yamierick@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:30:53 -0500 Subject: [PATCH] Notebooks: escape prices in markdown, repair two eaten escapes, check both Colab and Jupyter pair unescaped dollar signs as inline LaTeX. Eleven prices in markdown (notebooks 01, 04, 12, 13) are now written \$; in 13's last exercise two of them on one line were already rendering as math. Raised by the ESM chat, which found the same in its own notebooks. seed_simplex.py had lost two characters to escapes: "\binom" became a backspace + "inom" and "\times" a tab + "imes", so simplex_by_hand cell 9 showed garbage. Repaired in the seed and the notebook. check_notebooks.py gains two rules: no control character in any cell (no tab in markdown), and no unescaped price in markdown. Both were watched to fail on the pre-fix notebooks (all five files, every price) and pass after. Markdown-only edits, made identically in seed and notebook; outputs untouched. check_seeds, check_notebooks, ruff and pytest (215) clean. Co-Authored-By: Claude Opus 5.5 --- .../01_lp_formulation/goal_programming.ipynb | 2 +- .../simplex_by_hand.ipynb | 2 +- .../transportation.ipynb | 2 +- .../dispatch_to_pypsa.ipynb | 14 +++++------ .../sourcing_and_resilience.ipynb | 4 ++-- tools/check_notebooks.py | 23 +++++++++++++++++++ tools/seeds/seed_energy.py | 14 +++++------ tools/seeds/seed_goal.py | 2 +- tools/seeds/seed_simplex.py | 2 +- tools/seeds/seed_sourcing.py | 4 ++-- 10 files changed, 46 insertions(+), 23 deletions(-) diff --git a/notebooks/01_lp_formulation/goal_programming.ipynb b/notebooks/01_lp_formulation/goal_programming.ipynb index 70ab33f..a3dc044 100644 --- a/notebooks/01_lp_formulation/goal_programming.ipynb +++ b/notebooks/01_lp_formulation/goal_programming.ipynb @@ -11,7 +11,7 @@ "reaches a known number of people, costs a known amount, and ties up a known amount of the agency's\n", "staff. There are ten person-minutes of staff time, and the contract caps radio at six minutes.\n", "\n", - "Management has two targets: reach at least 45 million people, and spend no more than $100 thousand. Call\n", + "Management has two targets: reach at least 45 million people, and spend no more than \\$100 thousand. Call\n", "them **goals** rather than constraints, and check first whether any plan can meet both — a\n", "constraint that cannot be met makes the whole problem infeasible, and goal programming exists for\n", "that case: *by how little must each target be missed, in order of importance?*\n", diff --git a/notebooks/02_simplex_and_bases/simplex_by_hand.ipynb b/notebooks/02_simplex_and_bases/simplex_by_hand.ipynb index 5b69bf3..1dc75db 100644 --- a/notebooks/02_simplex_and_bases/simplex_by_hand.ipynb +++ b/notebooks/02_simplex_and_bases/simplex_by_hand.ipynb @@ -246,7 +246,7 @@ "metadata": {}, "source": [ "Enumerating is fine for six. With 50 variables and 30 constraints there are 80 columns and\n", - "$\binom{80}{30}$ choices, about $9 \times 10^{21}$.\n", + "$\\binom{80}{30}$ choices, about $9 \\times 10^{21}$.\n", "The simplex method visits a handful of them, always moving to a neighbour that is no worse.\n", "\n", "## The tableau\n", diff --git a/notebooks/04_networks_and_transport/transportation.ipynb b/notebooks/04_networks_and_transport/transportation.ipynb index dbcb6d0..aa898b2 100644 --- a/notebooks/04_networks_and_transport/transportation.ipynb +++ b/notebooks/04_networks_and_transport/transportation.ipynb @@ -830,7 +830,7 @@ "## A second instance, where the units are the trap\n", "\n", "Three distribution centres, five dealers, distances in miles. Its data are in **units** while trucks\n", - "carry **18** at a time and cost **$25 per mile** — so a factor of 18 and a factor of 25 have to\n", + "carry **18** at a time and cost **\\$25 per mile** — so a factor of 18 and a factor of 25 have to\n", "appear somewhere, and where they appear is a choice.\n", "\n", "The version this came from divided by 18 inside the constraint expressions, as a bare number. Here\n", diff --git a/notebooks/12_energy_systems_pypsa/dispatch_to_pypsa.ipynb b/notebooks/12_energy_systems_pypsa/dispatch_to_pypsa.ipynb index 6bd0281..a4b2557 100644 --- a/notebooks/12_energy_systems_pypsa/dispatch_to_pypsa.ipynb +++ b/notebooks/12_energy_systems_pypsa/dispatch_to_pypsa.ipynb @@ -164,8 +164,8 @@ "\n", "| technology | available this hour | cost to run |\n", "|---|---|---|\n", - "| solar | 40 MW | $0.00 /MWh |\n", - "| gas | 500 MW | $22.14 /MWh |\n", + "| solar | 40 MW | \\$0.00 /MWh |\n", + "| gas | 500 MW | \\$22.14 /MWh |\n", "\n", "**Solve it before you run anything below**: which do you run, how much of each, and what does the\n", "hour cost? Then write it as a linear program — one variable per technology, a balance row, a\n", @@ -740,7 +740,7 @@ "source": [ "Read the table before moving on. The battery column is negative when it charges. Find the hours it\n", "charges in and what the price is then; the hours it discharges into; and the one hour the peaker\n", - "runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's $22.14, and the\n", + "runs. Then look at the price in hour 18, and in hours 20 to 22: each is above gas's \\$22.14, and the\n", "only thing in this system that costs more than gas to run is the peaker, which is off in all four.\n", "What is setting the price there?" ] @@ -1019,7 +1019,7 @@ "## The envelope calculation, and where it goes wrong\n", "\n", "Work out by hand why the model would not touch free fuel: one MW of solar makes so many MWh a day,\n", - "each displacing gas at $22.14, and that is worth so much a year. Compare it with the capital cost the\n", + "each displacing gas at \\$22.14, and that is worth so much a year. Compare it with the capital cost the\n", "table charges." ] }, @@ -1425,7 +1425,7 @@ "> **2.** Cut solar to 60 MW. Does the battery still charge at midday? Where does its energy come\n", "> from now, and what does that do to the evening prices?\n", ">\n", - "> **3.** Raise the peaker's cost to $300. Predict first: does the schedule change, the prices, the\n", + "> **3.** Raise the peaker's cost to \\$300. Predict first: does the schedule change, the prices, the\n", "> day cost, or all three?" ] }, @@ -1664,13 +1664,13 @@ "\n", "## Where to take this next\n", "\n", - "- Add a coal plant — 200 MW available at $19.90/MWh — to *both* one-hour models, and confirm they\n", + "- Add a coal plant — 200 MW available at \\$19.90/MWh — to *both* one-hour models, and confirm they\n", " still agree. Predict, before running, whether the marginal price goes up or down, and why.\n", "- Set the battery's power to zero in the day model and re-solve. How much of the day's cost was the\n", " battery saving, and in which hours?\n", "- Repeat the capital-cost sweep for the peaker instead of solar. Does the model value it above or\n", " below its energy displacement, and why is the answer the opposite sign to solar's?\n", - "- Cap solar's build at 50 MW and re-run the sweep at $60,000/yr. The model builds all 50. Which is\n", + "- Cap solar's build at 50 MW and re-run the sweep at \\$60,000/yr. The model builds all 50. Which is\n", " binding — the cap or the price — and how would you tell?" ] } diff --git a/notebooks/13_supply_chain/sourcing_and_resilience.ipynb b/notebooks/13_supply_chain/sourcing_and_resilience.ipynb index 2ba5ab0..8b0f145 100644 --- a/notebooks/13_supply_chain/sourcing_and_resilience.ipynb +++ b/notebooks/13_supply_chain/sourcing_and_resilience.ipynb @@ -484,7 +484,7 @@ "source": [ "The zero describes one side only. A shadow price is a derivative, and here the cost has a **kink**:\n", "upward it is flat, because there is nothing left that is worth refining once demand is met; downward\n", - "it is $3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the\n", + "it is \\$3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the\n", "right-hand slope. A different solver, or the same one with a different method, may report the left —\n", "both are correct, and neither is *the* shadow price, because at a kink there isn't one.\n", "\n", @@ -833,7 +833,7 @@ "\n", "- Put the share cap on the *processors* instead of the mines. Which cap is more expensive to satisfy,\n", " and what does that say about where the bottleneck in this industry sits?\n", - "- The domestic mine ships to China at $8.0/kt and to the domestic processor at $3.0. Find the ore\n", + "- The domestic mine ships to China at \\$8.0/kt and to the domestic processor at \\$3.0. Find the ore\n", " tariff at which it enters the uncapped solution on price alone — without re-solving, from the\n", " reduced costs.\n", "- The middle-layer structure above — the balance row saying a processor cannot ship what it did not\n", diff --git a/tools/check_notebooks.py b/tools/check_notebooks.py index 7bfb2b8..6175f30 100644 --- a/tools/check_notebooks.py +++ b/tools/check_notebooks.py @@ -33,6 +33,11 @@ written in the same cell as the plot (standard Part 3, accessible) setup cell tag the setup cell clones orteach.paths.TAG, the same release the package fetches tables from + control characters none in any cell source, and no tab in markdown: the trace of + a "\\b" or "\\t" a seed's string ate ("$\\binom" became + backspace + "inom") + currency dollars a price in markdown is written "\\$22.14", not "$22.14": two on + one line, or one beside real math, pair up and render as LaTeX """ from __future__ import annotations @@ -60,6 +65,14 @@ CODE_SPAN_RE = re.compile(r"`[^`]*`|\$\$.*?\$\$|\$[^$\n]*\$", re.S) EXERCISES_RE = re.compile(r"^#+\s*where to take this next.*", re.I | re.M | re.S) LICENCE_RE = re.compile(r"LicenseID to value|Academic license|Restricted license") +CONTROL_RE = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f]") +# An unescaped "$" opening a number that reads as money: the number ends the line, or is +# followed by punctuation and a space, by a word ("$3 a kilotonne"), or by a per-unit slash +# ("$0.00 /MWh", "$8.0/kt"). Math such as "$2 \times 2$", "$1/\sqrt{n}$", "$1/n$", "$40x_1$" +# or "$0.02$" continues with a backslash, an operator, a letter glued on, a one-letter +# denominator, or the closing "$". +CURRENCY_RE = re.compile(r"(? **2.** Cut solar to 60 MW. Does the battery still charge at midday? Where does its energy come > from now, and what does that do to the evening prices? > -> **3.** Raise the peaker's cost to $300. Predict first: does the schedule change, the prices, the +> **3.** Raise the peaker's cost to \$300. Predict first: does the schedule change, the prices, the > day cost, or all three? """) code(r''' @@ -762,13 +762,13 @@ def explore_build(solar_capex, battery_mw): ## Where to take this next -- Add a coal plant — 200 MW available at $19.90/MWh — to *both* one-hour models, and confirm they +- Add a coal plant — 200 MW available at \$19.90/MWh — to *both* one-hour models, and confirm they still agree. Predict, before running, whether the marginal price goes up or down, and why. - Set the battery's power to zero in the day model and re-solve. How much of the day's cost was the battery saving, and in which hours? - Repeat the capital-cost sweep for the peaker instead of solar. Does the model value it above or below its energy displacement, and why is the answer the opposite sign to solar's? -- Cap solar's build at 50 MW and re-run the sweep at $60,000/yr. The model builds all 50. Which is +- Cap solar's build at 50 MW and re-run the sweep at \$60,000/yr. The model builds all 50. Which is binding — the cap or the price — and how would you tell? """) diff --git a/tools/seeds/seed_goal.py b/tools/seeds/seed_goal.py index 85dc816..4a7aa2a 100644 --- a/tools/seeds/seed_goal.py +++ b/tools/seeds/seed_goal.py @@ -23,7 +23,7 @@ def code(t): reaches a known number of people, costs a known amount, and ties up a known amount of the agency's staff. There are ten person-minutes of staff time, and the contract caps radio at six minutes. -Management has two targets: reach at least 45 million people, and spend no more than $100 thousand. Call +Management has two targets: reach at least 45 million people, and spend no more than \$100 thousand. Call them **goals** rather than constraints, and check first whether any plan can meet both — a constraint that cannot be met makes the whole problem infeasible, and goal programming exists for that case: *by how little must each target be missed, in order of importance?* diff --git a/tools/seeds/seed_simplex.py b/tools/seeds/seed_simplex.py index 6083f86..14601ce 100644 --- a/tools/seeds/seed_simplex.py +++ b/tools/seeds/seed_simplex.py @@ -140,7 +140,7 @@ def code(t): md(r""" Enumerating is fine for six. With 50 variables and 30 constraints there are 80 columns and -$inom{80}{30}$ choices, about $9 imes 10^{21}$. +$\binom{80}{30}$ choices, about $9 \times 10^{21}$. The simplex method visits a handful of them, always moving to a neighbour that is no worse. ## The tableau diff --git a/tools/seeds/seed_sourcing.py b/tools/seeds/seed_sourcing.py index ce7ab63..af85ef2 100644 --- a/tools/seeds/seed_sourcing.py +++ b/tools/seeds/seed_sourcing.py @@ -228,7 +228,7 @@ def code(t): md(r""" The zero describes one side only. A shadow price is a derivative, and here the cost has a **kink**: upward it is flat, because there is nothing left that is worth refining once demand is met; downward -it is $3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the +it is \$3 a kilotonne, because the ore displaced has to travel a dearer route. The solver reported the right-hand slope. A different solver, or the same one with a different method, may report the left — both are correct, and neither is *the* shadow price, because at a kink there isn't one. @@ -395,7 +395,7 @@ def code(t): - Put the share cap on the *processors* instead of the mines. Which cap is more expensive to satisfy, and what does that say about where the bottleneck in this industry sits? -- The domestic mine ships to China at $8.0/kt and to the domestic processor at $3.0. Find the ore +- The domestic mine ships to China at \$8.0/kt and to the domestic processor at \$3.0. Find the ore tariff at which it enters the uncapped solution on price alone — without re-solving, from the reduced costs. - The middle-layer structure above — the balance row saying a processor cannot ship what it did not