The problem is that your newer version doesn’t include KaTeX’s stylesheet. The old one only worked by coincidence—there’s a cell where you use Observable’s built-in tex template literal, which by side-effect loads KaTeX’s stylesheet. But since you are invoking KaTeX explicitly, it’s better to load the KaTeX stylesheet explicitly, too.
Here’s a suggestion for you to merge:
I also simplified how you’re invoking katex.render using selection.each. You should avoid selecting by id in Observable (at least, from the entire document); see Tom’s re-selecting elements antipattern for explanation.