Pandoc sets some template variables automatically: $math$ if there's math, $csl-refs$ if there are citeproc references, and so on. In Raw mode these are set wrong because by the time the raw blocks are imported in the master document Pandoc doesn't see the citeproc references and the maths.
Workaround: the template should include needed LaTeX code unconditionally. Downside: will load mathjax only if not needed.
Workaround 2: make our own duplicates of Pandoc's internal template variables, and pass them from sources to main?