An Unexpected Error in LaTeX with \vec Following an _

LaTeX
quarto
debug
diary
Author

Guest_1013

Published

2026-06-20

Problem

I was trying to render this formula to a pdf file with quarto:

\psi_\vec{k} (\vec{r}) = \frac{1}{\sqrt{N}} \sum_\vec{r} \exp(i\vec{k} \cdot \vec{R}) \phi(\vec{r} - \vec{R})

Then I got this error message during rendering:

ERROR:
compilation failed- error
Missing { inserted.
<to be read again>
\Umathaccent
l.281 \psi _\vec
              {k} (\vec{r}) = \frac{1}{\sqrt{N}} \sum_\vec{r} \exp(i\vec{k} ...


see [filename].log for more information.
WARN: Error encountered when rendering files

Solution

After analyzing the error log (with DeepSeek), I got to know that:

  • In my environment (LuaLaTeX + unicode-math), \vec is a math accent command corresponding to \Umathaccent;
  • If I directly use it after _ without any decoration, the parser cannot determine the boundary of the parameters;
  • So I have to use a pair of {} to enclose the \vec command, like \psi_{\vec{k}}.

I actually don’t exactly know what are these about, but the solution is clear. After using {}s, it passed the rendering.

Tests in KaTeX on This Page

\psi_\vec{k} (\vec{r}) = \frac{1}{\sqrt{N}} \sum_\vec{r} \exp(i\vec{k} \cdot \vec{R}) \phi(\vec{r} - \vec{R})

\psi_{\vec{k}} (\vec{r}) = \frac{1}{\sqrt{N}} \sum_{\vec{r}} \exp(i\vec{k} \cdot \vec{R}) \phi(\vec{r} - \vec{R})

So if I use the unchanged formula directly in KaTeX on this page, it will only present a red version of the source code; I assume it’s a sign of the same issue, but without any explicit compilation errors, since it’s only rendered on loading.